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  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.io.FileOutputStream;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.ArrayList;
29  import java.util.Map;
30  import java.util.regex.Pattern;
31  import java.util.regex.Matcher;
32  import java.util.Date;
33  import java.text.SimpleDateFormat;
34  
35  import org.codehaus.plexus.util.StringUtils;
36  import org.apache.commons.io.IOUtils;
37  
38  import org.apache.maven.plugins.annotations.LifecyclePhase;
39  import org.apache.maven.plugins.annotations.Mojo;
40  import org.apache.maven.plugins.annotations.Parameter;
41  
42  /**
43   * Goal which build VisualStudio solutions.
44   *
45   * @author Franck Bonin 
46   */
47  @Mojo( name = "msbuild", defaultPhase = LifecyclePhase.COMPILE )
48  public class VisualStudioMojo extends AbstractLaunchMojo
49  {
50  
51      /**
52       * Directory location of visual studio solution default set to baseDir
53       * 
54       * @since 0.0.5
55       */
56      @Parameter( property = "visualstudio.solutionDir" )
57      private String solutionDir;
58      
59      protected String getSolutionDir()
60      {
61          if ( null == solutionDir )
62          {
63              solutionDir = new String( basedir.getAbsolutePath() );
64          }
65          return solutionDir;
66      }
67      
68      /**
69       * Visual studio solution file name.
70       * 
71       * @since 0.0.5
72       */
73      @Parameter( property = "visualstudio.solutionFileName", required = true )
74      private String solutionFileName;
75      
76      /**
77       * Build type [ rebuild | clean | build | ... ]
78       * 
79       * @since 0.0.5
80       */
81      @Parameter( property = "visualstudio.buildType", defaultValue = "build" )
82      private String buildType;
83      
84      /**
85       * Build config [ debug | release | ... ]
86       * 
87       * @since 0.0.5
88       */
89      @Parameter( property = "visualstudio.buildConfig", defaultValue = "release" )
90      private String buildConfig;
91      
92      /**
93       * target platform  [ win32 | ... ]
94       * 
95       * @since 0.0.5
96       */
97      @Parameter( property = "visualstudio.targetPlatform", defaultValue = "win32" )
98      private String targetPlatform;
99      
100     /**
101      * target architecture  [ x86 | amd64 | ia64 | x86_amd64 | x86_ia64 ]
102      * 
103      * @since 0.0.5
104      */
105     @Parameter( property = "visualstudio.targetArchitecture", defaultValue = "x86" )
106     private String targetArchitecture;
107     
108     /**
109      * Build version in visual studio format [ x.y.z.t ]
110      * 
111      * @since 0.0.5
112      */
113     @Parameter( property = "visualstudio.buildVersion", defaultValue = "0.0.0.1" )
114     private String buildVersion;
115     
116     protected String visualStudioBuildVersion()
117     {
118         Pattern p = Pattern.compile( "(\\d+\\.)+\\d+" );
119         Matcher m = p.matcher( buildVersion );
120         if ( m.find() ) 
121         {
122             getLog().debug( "Visual Studio compatible buildVersion match is " + m.group() );
123             return m.group();
124         }
125         else
126         {
127             getLog().debug( "Visual Studio 'as is' buildVersion is " + buildVersion );
128             return buildVersion;
129         }
130     }
131     
132     protected String buildVersionExtension()
133     {
134         Pattern p = Pattern.compile( "(\\d+\\.)+\\d+((-[^-\\s]+)+)" );
135         Matcher m = p.matcher( buildVersion );
136         if ( m.matches() && m.groupCount() >= 3 ) 
137         {
138             String versionExtension = m.group( 2 );
139             getLog().debug( "Visual Studio compatible Extension version is " + versionExtension );
140             if ( versionExtension.contains( "SNAPSHOT" ) )
141             {
142                 return versionExtension.replaceAll( "SNAPSHOT",
143                     ( new SimpleDateFormat( "yyyyMMdd.HHmmss" ) ).format( new Date() ) );
144             }
145             else
146             {
147                 return versionExtension;
148             }
149         }
150         else
151         {
152             getLog().debug( "Visual Studio compatible Extension version is empty" );
153             return "\"\\0\""; // this \0 will go through batch script, env var and C++/RCC preprocessor
154         }
155     }
156     
157     /**
158      * Additional compiler options (without any global quotation)
159      * 
160      * @since 0.0.5
161      */
162     @Parameter( property = "visualstudio.compilerOptions", defaultValue = "" )
163     private String compilerOptions;
164     
165     @Override
166     protected String getCommandArgs()
167     {
168         return null;
169     }
170     
171     @Override
172     protected List<String> getArgsList()
173     {
174         ArrayList<String> args = new ArrayList<String>();
175         
176         args.add( getSolutionDir() );
177         args.add( solutionFileName );
178         args.add( buildType );
179         args.add( buildConfig );
180         args.add( targetPlatform );
181         args.add( targetArchitecture );
182         args.add( visualStudioBuildVersion() );
183         args.add( buildVersionExtension() );
184         if ( StringUtils.isNotEmpty( compilerOptions ) )
185         {
186             args.add( "\"" + compilerOptions + "\"" );
187         }
188         
189         return args;
190     }
191     
192     @Override
193     protected String getExecutable()
194     {
195         InputStream batchScriptStream = getClass().getResourceAsStream( "/build.bat" );
196         try
197         {    
198             File batchFile = File.createTempFile( "build", ".bat" );
199             batchFile.deleteOnExit();
200             OutputStream outStream = new FileOutputStream( batchFile );
201             
202             IOUtils.copy( batchScriptStream, outStream );
203             
204             getLog().debug( "msbuild batch script is located at :" + batchFile.getAbsolutePath() );
205                 
206             return batchFile.getAbsolutePath();
207         }
208         catch ( IOException e )
209         {
210             getLog().error( "Temporary batch script can't be created" );
211             return "echo";
212         }
213     }
214 
215     /**
216      * Environment variables to pass to the msbuild program.
217      * 
218      * @since 0.0.5
219      */
220     @Parameter()
221     private Map environmentVariables = new HashMap();
222     
223     @Override
224     protected Map getMoreEnvironmentVariables()
225     {
226         return environmentVariables;
227     }
228 
229     @Override
230     protected List<String> getSuccesCode()
231     {
232         return null;
233     }
234     
235     @Override
236     protected File getWorkingDir()
237     {
238         return new File( solutionDir );
239     }
240 
241     @Override
242     public boolean isSkip()
243     {
244         return false;
245     }
246 
247 }