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 org.apache.maven.plugin.MojoExecutionException;
25  import org.codehaus.mojo.natives.NativeBuildException;
26  import org.codehaus.mojo.natives.NativeSources;
27  import org.codehaus.mojo.natives.compiler.ResourceCompiler;
28  import org.codehaus.mojo.natives.compiler.ResourceCompilerConfiguration;
29  import org.codehaus.mojo.natives.manager.NoSuchNativeProviderException;
30  import org.codehaus.mojo.natives.manager.ResourceCompilerManager;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  import java.io.File;
34  import java.util.List;
35  import org.apache.maven.plugins.annotations.Component;
36  import org.apache.maven.plugins.annotations.LifecyclePhase;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  
40  /**
41   * Compile Windows resource files
42   */
43  @Mojo(name = "resource-compile", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
44  public class NativeResourceCompileMojo
45      extends AbstractNativeMojo
46  {
47  
48      /**
49       * Compiler Provider Type
50       * @since 1.0-alpha-2
51       */
52      @Parameter(defaultValue = "msvc", required = true)
53      private String provider;
54  
55      /**
56       * Use this field to override provider specific resource compiler executable
57       * @since 1.0-alpha-2
58       */
59      @Parameter
60      private String resourceCompilerExecutable;
61  
62      /**
63       * Resource compiler options
64       * @since 1.0-alpha-2
65       */
66      @Parameter
67      private List resourceCompilerOptions;
68  
69      /**
70       * Array of NativeSources containing include directories and source files
71       * @since 1.0-alpha-8
72       */
73      @Parameter
74      private NativeSources[] resources;
75  
76      /**
77       * @since 1.0-alpha-2
78       */
79      @Parameter(defaultValue = "${project.build.directory}", required = true)
80      protected File resourceCompilerOutputDirectory;
81  
82      /**
83       * Internal
84       * @since 1.0-alpha-2
85       */
86      @Component
87      private ResourceCompilerManager manager;
88  
89      public void execute()
90          throws MojoExecutionException
91      {
92  
93          if ( !this.resourceCompilerOutputDirectory.exists() )
94          {
95              this.resourceCompilerOutputDirectory.mkdirs();
96          }
97  
98          FileUtils.mkdir( project.getBuild().getDirectory() );
99  
100         ResourceCompiler compiler = this.getResourceCompiler();
101 
102         ResourceCompilerConfiguration config = new ResourceCompilerConfiguration();
103         config.setExecutable( this.resourceCompilerExecutable );
104         config.setWorkingDirectory( this.workingDirectory );
105         config.setOptions( NativeMojoUtils.trimParams( this.resourceCompilerOptions ) );
106         config.setOutputDirectory( this.resourceCompilerOutputDirectory );
107         config.setEnvFactory( this.getEnvFactory() );
108 
109         try
110         {
111             List resourceOutputFiles;
112             resourceOutputFiles = compiler.compile( config, this.resources );
113 
114             this.saveCompilerOutputFilePaths( resourceOutputFiles );
115         }
116         catch ( NativeBuildException e )
117         {
118             throw new MojoExecutionException( e.getMessage(), e );
119         }
120 
121     }
122 
123     private ResourceCompiler getResourceCompiler()
124         throws MojoExecutionException
125     {
126         ResourceCompiler rc;
127 
128         try
129         {
130             rc = this.manager.getResourceCompiler( this.provider );
131 
132         }
133         catch ( NoSuchNativeProviderException pe )
134         {
135             throw new MojoExecutionException( pe.getMessage() );
136         }
137 
138         return rc;
139     }
140 }