1 package org.apache.maven.plugin.cxx;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47 @Mojo( name = "msbuild", defaultPhase = LifecyclePhase.COMPILE )
48 public class VisualStudioMojo extends AbstractLaunchMojo
49 {
50
51
52
53
54
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
70
71
72
73 @Parameter( property = "visualstudio.solutionFileName", required = true )
74 private String solutionFileName;
75
76
77
78
79
80
81 @Parameter( property = "visualstudio.buildType", defaultValue = "build" )
82 private String buildType;
83
84
85
86
87
88
89 @Parameter( property = "visualstudio.buildConfig", defaultValue = "release" )
90 private String buildConfig;
91
92
93
94
95
96
97 @Parameter( property = "visualstudio.targetPlatform", defaultValue = "win32" )
98 private String targetPlatform;
99
100
101
102
103
104
105 @Parameter( property = "visualstudio.targetArchitecture", defaultValue = "x86" )
106 private String targetArchitecture;
107
108
109
110
111
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\"";
154 }
155 }
156
157
158
159
160
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
217
218
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 }