View Javadoc
1   package org.apache.maven.plugin.cxx;
2   
3   /*
4    * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés.
5    * Author(s) : Franck Bonin, Neticoa SAS France
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   *
19   */
20  
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  
35  /**
36   * Goal which Launch an external executable.
37   *
38   * @author Franck Bonin 
39   */
40  @Mojo( name = "launch" )
41  public class LaunchMojo extends AbstractLaunchMojo
42  {
43  
44      /**
45       * Can be of type <code>&lt;argument&gt;</code> or <code>&lt;classpath&gt;</code> Can be overriden using "cxx.args"
46       * env. variable
47       * 
48       * @since 0.0.4
49       */
50      @Parameter()
51      private List<String> arguments;
52      
53      
54      @Override
55      protected List<String> getArgsList()
56      {
57          return arguments;
58      }
59  
60      /**
61       * Arguments for the executed program
62       * 
63       */
64      @Parameter( property = "launch.args" )
65      private String commandArgs;
66      
67      @Override
68      protected String getCommandArgs()
69      {
70          return commandArgs;
71      }
72  
73      /**
74       * The executable. Can be a full path or a the name executable. In the latter case, the executable must be in the
75       * PATH for the execution to work.
76       * 
77       * @since 0.0.4
78       */
79      @Parameter( property = "launch.executable", required = true )
80      private String executable;
81      
82      @Override
83      protected String getExecutable()
84      {
85          return executable;
86      }
87  
88      /**
89       * Environment variables to pass to the executed program.
90       * 
91       * @since 0.0.4
92       */
93      @Parameter()
94      private Map environmentVariables = new HashMap();
95      
96      @Override
97      protected Map getMoreEnvironmentVariables()
98      {
99          return environmentVariables;
100     }
101 
102     /**
103      * Exit codes to be resolved as successful execution for non-compliant applications (applications not returning 0
104      * for success).
105      * 
106      * @since 0.0.4
107      */
108     @Parameter()
109     private List<String> successCodes;
110     
111     @Override
112     protected List<String> getSuccesCode()
113     {
114         return successCodes;
115     }
116 
117     /**
118      * The current working directory. Optional. If not specified, basedir will be used.
119      * 
120      * @since 0.0.4
121      */
122     @Parameter( property = "launch.workingdir" )
123     private File workingDir;
124     
125     @Override
126     protected File getWorkingDir()
127     {
128         if ( null == workingDir )
129         {
130             workingDir = new File( basedir.getPath() );
131         }
132         return workingDir;
133     }
134 
135     /**
136      * Os for which command shall be executed
137      * os name match is java.lang.System.getProperty( "os.name" )
138      * 
139      * @since 0.0.4
140      */
141     @Parameter()
142     private List includeOS;
143     
144     /**
145      * Os for which command shall not be executed
146      * os name match is java.lang.System.getProperty( "os.name" )
147      *  
148      * @since 0.0.4
149      */
150     @Parameter()
151     private List excludeOS;
152     
153     @Override
154     protected boolean isSkip()
155     {
156         String sOsName = System.getProperty( "os.name" );
157         sOsName = sOsName.replace( " ", "" );
158         getLog().info( "os.name is \"" + sOsName + "\"" );
159         if ( null == excludeOS && null == includeOS )
160         {
161             return false;
162         }
163         else if ( null == includeOS )
164         {
165             return excludeOS.contains( sOsName );
166         }
167         else if ( null == excludeOS )
168         {
169             return !includeOS.contains( sOsName );
170         }
171         else
172         {
173             return ( excludeOS.contains( sOsName ) || !includeOS.contains( sOsName ) );
174         }
175     }
176     
177     /**
178      * The OutputStreamOut Location.
179      * 
180      * @since 0.0.5
181      */
182     @Parameter( property = "launch.outputStreamOut", defaultValue = "" )
183     private File outputStreamOut;
184     
185     @Override
186     protected OutputStream getOutputStreamOut()
187     {
188         String sOutputStreamOut = new String();
189         if ( null != outputStreamOut && !outputStreamOut.toString().isEmpty() )
190         {
191             if ( outputStreamOut.isAbsolute() )
192             {
193                 sOutputStreamOut = outputStreamOut.getPath();
194             }
195             else
196             {
197                 sOutputStreamOut = basedir.getAbsolutePath() + File.separator + outputStreamOut.getPath();
198             }
199             
200             getLog().info( "Launch output location " + sOutputStreamOut );
201              
202             OutputStream output = super.getOutputStreamOut();
203             File file = new File( sOutputStreamOut );
204             try
205             {
206                 new File( file.getParent() ).mkdirs();
207                 file.createNewFile();
208                 output = new FileOutputStream( file );
209             }
210             catch ( IOException e )
211             {
212                 getLog().error( "Launch report redirected to stout since " + sOutputStreamOut + " can't be opened" );
213             }
214             return output;
215         }
216         else
217         {
218             return super.getOutputStreamOut();
219         }
220     }
221     
222     /**
223      * The OutputStreamErr Location.
224      * 
225      * @since 0.0.5
226      */
227     @Parameter( property = "launch.outputStreamErr", defaultValue = "" )
228     private File outputStreamErr;
229     
230     @Override
231     protected OutputStream getOutputStreamErr()
232     {
233         String sOutputStreamErr = new String();
234         if ( null != outputStreamErr && !outputStreamErr.toString().isEmpty() )
235         {
236             if ( outputStreamErr.isAbsolute() )
237             {
238                 sOutputStreamErr = outputStreamErr.getPath();
239             }
240             else
241             {
242                 sOutputStreamErr = basedir.getAbsolutePath() + File.separator + outputStreamErr.getPath();
243             }
244             
245             getLog().info( "Launch erroutput location " + sOutputStreamErr );
246              
247             OutputStream output = super.getOutputStreamErr();
248             File file = new File( sOutputStreamErr );
249             try
250             {
251                 new File( file.getParent() ).mkdirs();
252                 file.createNewFile();
253                 output = new FileOutputStream( file );
254             }
255             catch ( IOException e )
256             {
257                 getLog().error( "Launch report redirected to stout since " + sOutputStreamErr + " can't be opened" );
258             }
259             return output;
260         }
261         else
262         {
263             return super.getOutputStreamErr();
264         }
265     }
266     
267     /**
268      * The InputStream Location.
269      * 
270      * @since 0.0.5
271      */
272     @Parameter( property = "launch.inputStream", defaultValue = "" )
273     private File inputStream;
274     
275     @Override
276     protected InputStream getInputStream()
277     {
278         String sInputStream = new String();
279         if ( null != inputStream && !inputStream.toString().isEmpty() )
280         {
281             if ( inputStream.isAbsolute() )
282             {
283                 sInputStream = inputStream.getPath();
284             }
285             else
286             {
287                 sInputStream = basedir.getAbsolutePath() + File.separator + inputStream.getPath();
288             }
289             
290             getLog().info( "Launch input location " + sInputStream );
291              
292             InputStream input = super.getInputStream();
293             File file = new File( sInputStream );
294             try
295             {
296                 new File( file.getParent() ).mkdirs();
297                 file.createNewFile();
298                 input = new FileInputStream( file );
299             }
300             catch ( IOException e )
301             {
302                 getLog().error( "Launch report redirected to stout since " + sInputStream + " can't be opened" );
303             }
304             return input;
305         }
306         else
307         {
308             return super.getInputStream();
309         }
310     }
311 }