View Javadoc
1   package org.codehaus.mojo.natives.plugin;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
9    * associated documentation files (the "Software"), to deal in the Software without restriction,
10   * including without limitation the rights to use, copy, modify, merge, publish, distribute,
11   * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in all copies or
15   * substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
18   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20   * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22   */
23  
24  import java.io.File;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Component;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.codehaus.mojo.natives.NativeBuildException;
32  import org.codehaus.mojo.natives.linker.Manifest;
33  import org.codehaus.mojo.natives.linker.ManifestConfiguration;
34  import org.codehaus.mojo.natives.manager.ManifestManager;
35  import org.codehaus.mojo.natives.manager.NoSuchNativeProviderException;
36  
37  /**
38   * Embeds a Visual Studio manifest file into a generated executable
39   * @since 1.0-alpha4
40   */
41  @Mojo(name = "manifest", defaultPhase = LifecyclePhase.PACKAGE)
42  public class NativeManifestMojo
43      extends AbstractNativeMojo
44  {
45      /**
46       * Manifest Provider.
47       * @since 1.0-alpha4
48       */
49      @Parameter(defaultValue = "msvc", required = true)
50      private String provider;
51  
52      /**
53       * Manifest extension
54       * @since 1.0-alpha-4
55       */
56      @Parameter(defaultValue = "manifest", required = true)
57      private String manifestExtension;
58  
59      /**
60       * Enable this option to speed up linkage for large project with no dependencies changes
61       * @since 1.0-alpha-8
62       */
63      @Parameter(defaultValue = "false")
64      private boolean checkStaleLinkage;
65  
66      /**
67       * Internal - To look up manifest implementation
68       * @since 1.0-alpha-4
69       */
70      @Component
71      private ManifestManager manager;
72  
73      public void execute()
74          throws MojoExecutionException
75      {
76          File linkerOutputFile = (File) this.getPluginContext().get( LINKER_OUTPUT_PATH );
77  
78          if ( !linkerOutputFile.exists() )
79          {
80              // @TODO ERROR?
81              return;
82          }
83  
84          File linkerManifestFile = new File( linkerOutputFile.getAbsolutePath() + "." + manifestExtension );
85  
86          if ( !linkerManifestFile.exists() )
87          {
88              // no need to inject manifest file into the executable
89              return;
90          }
91  
92          // @Todo check for stale output, and skip if needed
93          try
94          {
95              ManifestConfiguration config = new ManifestConfiguration();
96  
97              config.setEnvFactory( this.getEnvFactory() );
98              config.setWorkingDirectory( this.workingDirectory );
99              config.setInputFile( linkerOutputFile );
100             config.setManifestFile( linkerManifestFile );
101 
102             Manifest Manifest = this.getManifest();
103 
104             Manifest.run( config );
105         }
106         catch ( NativeBuildException e )
107         {
108             throw new MojoExecutionException( "Error executing Manifest.", e );
109         }
110 
111     }
112 
113     private Manifest getManifest()
114         throws MojoExecutionException
115     {
116         Manifest Manifest;
117 
118         try
119         {
120             Manifest = this.manager.getManifest( this.provider );
121         }
122         catch ( NoSuchNativeProviderException pe )
123         {
124             throw new MojoExecutionException( pe.getMessage() );
125         }
126 
127         return Manifest;
128     }
129 
130 }