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
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.annotations.Component;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.mojo.natives.EnvFactory;
37  import org.codehaus.mojo.natives.NativeBuildException;
38  import org.codehaus.mojo.natives.manager.EnvFactoryManager;
39  
40  public abstract class AbstractNativeMojo
41      extends AbstractMojo
42  {
43      public static final String LINKER_INPUT_LIST_NAME = "NativeLinkerInputListName";
44  
45      public static final String LINKER_OUTPUT_PATH = "NativeLinkerOutputPath";
46  
47      public static final String INCZIP_FOUND = "IncZipFound";
48  
49      public static final String INCZIP_TYPE = "inczip";
50  
51      protected static final List EMPTY_FILE_LIST = new ArrayList();
52  
53      @Parameter(defaultValue = "${project}", readonly = true, required = true)
54      protected MavenProject project;
55  
56      /**
57       * user directory when external tools( ie compiler/linker ) are invoked
58       */
59      @Parameter(defaultValue = "${basedir}", required = true)
60      protected File workingDirectory;
61  
62      /**
63       * Specifies a fully qualified class name implementing the org.codehaus.mojo.natives.EnvFactory interface. The class
64       * creates a set environment variables to be used with the command line.
65       */
66      @Parameter
67      private String envFactoryName;
68  
69      @Component
70      protected EnvFactoryManager envFactoryManager;
71  
72      /**
73       * Directory to unpack .inczip dependency files to be included as system include path
74       */
75      @Parameter(defaultValue = "${project.build.directory}/native/include")
76      protected File dependencyIncludeDirectory;
77  
78      protected static String[] removeEmptyOptions( List args )
79      {
80          return NativeMojoUtils.trimParams( args );
81      }
82  
83      protected List getAllCompilersOutputFileList()
84      {
85          List list = (List) this.getPluginContext().get( AbstractNativeMojo.LINKER_INPUT_LIST_NAME );
86  
87          if ( list == null )
88          {
89              list = new ArrayList();
90  
91              this.getPluginContext().put( AbstractNativeMojo.LINKER_INPUT_LIST_NAME, list );
92          }
93  
94          return list;
95  
96      }
97  
98      protected void saveCompilerOutputFilePaths( List filePaths )
99          throws MojoExecutionException
100     {
101         List allCompilerOutputFileList = getAllCompilersOutputFileList();
102 
103         for ( int i = 0; i < filePaths.size(); ++i )
104         {
105             File file = (File) filePaths.get( i );
106             allCompilerOutputFileList.add( file );
107         }
108     }
109 
110     /**
111      * Internal for unit test only
112      */
113 
114     protected MavenProject getProject()
115     {
116         return this.project;
117     }
118 
119     private EnvFactory envFactory = null;
120 
121     protected EnvFactory getEnvFactory()
122         throws MojoExecutionException
123     {
124         if ( envFactory == null && envFactoryName != null )
125         {
126             try
127             {
128                 envFactory = this.envFactoryManager.getEnvFactory( this.envFactoryName );
129             }
130             catch ( NativeBuildException e )
131             {
132                 throw new MojoExecutionException( e.getMessage(), e );
133             }
134         }
135 
136         return envFactory;
137     }
138 
139 }