1 package org.apache.maven.plugins.invoker;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import org.apache.maven.shared.invoker.InvocationRequest;
33 import org.apache.maven.shared.invoker.InvocationRequest.ReactorFailureBehavior;
34 import org.codehaus.plexus.util.StringUtils;
35
36
37
38
39
40
41 class InvokerProperties
42 {
43 private static final String SELECTOR_PREFIX = "selector.";
44
45 private enum InvocationProperty
46 {
47 PROJECT( "invoker.project" ),
48 GOALS( "invoker.goals" ),
49 PROFILES( "invoker.profiles" ),
50 MAVEN_OPTS( "invoker.mavenOpts" ),
51 FAILURE_BEHAVIOR( "invoker.failureBehavior" ),
52 NON_RECURSIVE( "invoker.nonRecursive" ),
53 OFFLINE( "invoker.offline" ),
54 SYSTEM_PROPERTIES_FILE( "invoker.systemPropertiesFile" ),
55 DEBUG( "invoker.debug" ),
56 SETTINGS_FILE ( "invoker.settingsFile" ),
57 TIMEOUT_IN_SECONDS ( "invoker.timeoutInSeconds" );
58
59 private final String key;
60
61 InvocationProperty( final String s )
62 {
63 this.key = s;
64 }
65
66 @Override
67 public String toString()
68 {
69 return key;
70 }
71 }
72
73 private enum SelectorProperty
74 {
75 JAVA_VERSION( ".java.version" ),
76 MAVEN_VERSION( ".maven.version" ),
77 OS_FAMLY( ".os.family" );
78
79 private final String suffix;
80
81 SelectorProperty( String suffix )
82 {
83 this.suffix = suffix;
84 }
85
86 @Override
87 public String toString()
88 {
89 return suffix;
90 }
91 }
92
93
94
95
96 private final Properties properties;
97
98
99
100
101
102
103
104 InvokerProperties( Properties properties )
105 {
106 this.properties = ( properties != null ) ? properties : new Properties();
107 }
108
109
110
111
112
113
114 public Properties getProperties()
115 {
116 return this.properties;
117 }
118
119
120
121
122
123
124 public String getJobName()
125 {
126 return this.properties.getProperty( "invoker.name", "" );
127 }
128
129
130
131
132
133
134 public String getJobDescription()
135 {
136 return this.properties.getProperty( "invoker.description", "" );
137 }
138
139
140
141
142
143
144 public String getJreVersion()
145 {
146 return this.properties.getProperty( "invoker.java.version", "" );
147 }
148
149
150
151
152
153
154 public String getJreVersion( int index )
155 {
156 return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.JAVA_VERSION.suffix,
157 getJreVersion() );
158 }
159
160
161
162
163
164
165
166 public String getMavenVersion()
167 {
168 return this.properties.getProperty( "invoker.maven.version", "" );
169 }
170
171
172
173
174
175
176
177 public String getMavenVersion( int index )
178 {
179 return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.MAVEN_VERSION.suffix,
180 getMavenVersion() );
181 }
182
183
184
185
186
187
188 public String getOsFamily()
189 {
190 return this.properties.getProperty( "invoker.os.family", "" );
191 }
192
193
194
195
196
197
198
199
200 public String getOsFamily( int index )
201 {
202 return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.OS_FAMLY.suffix,
203 getOsFamily() );
204 }
205
206 public Collection<InvokerToolchain> getToolchains()
207 {
208 return getToolchains( Pattern.compile( "invoker\\.toolchain\\.([^\\.]+)\\.(.+)" ) );
209 }
210
211 public Collection<InvokerToolchain> getToolchains( int index )
212 {
213 return getToolchains( Pattern.compile( "selector\\." + index + "\\.invoker\\.toolchain\\.([^\\.]+)\\.(.+)" ) );
214 }
215
216 private Collection<InvokerToolchain> getToolchains( Pattern p )
217 {
218 Map<String, InvokerToolchain> toolchains = new HashMap<>();
219 for ( Map.Entry<Object, Object> entry : this.properties.entrySet() )
220 {
221 Matcher m = p.matcher( entry.getKey().toString() );
222 if ( m.matches() )
223 {
224 String type = m.group( 1 );
225 String providesKey = m.group( 2 );
226 String providesValue = entry.getValue().toString();
227
228 InvokerToolchain tc = toolchains.get( type );
229 if ( tc == null )
230 {
231 tc = new InvokerToolchain( type );
232 toolchains.put( type, tc );
233 }
234 tc.addProvides( providesKey, providesValue );
235 }
236 }
237 return toolchains.values();
238 }
239
240
241
242
243
244
245
246 public boolean isInvocationDefined( int index )
247 {
248 for ( InvocationProperty prop : InvocationProperty.values() )
249 {
250 if ( properties.getProperty( prop.toString() + '.' + index ) != null )
251 {
252 return true;
253 }
254 }
255 return false;
256 }
257
258
259
260
261
262
263
264
265 public boolean isSelectorDefined( int index )
266 {
267 for ( SelectorProperty prop : SelectorProperty.values() )
268 {
269 if ( properties.getProperty( SELECTOR_PREFIX + index + prop.suffix ) != null )
270 {
271 return true;
272 }
273 }
274 return false;
275 }
276
277
278
279
280
281
282
283
284 public void configureInvocation( InvocationRequest request, int index )
285 {
286 String project = get( InvocationProperty.PROJECT, index );
287 if ( project != null )
288 {
289 File file = new File( request.getBaseDirectory(), project );
290 if ( file.isFile() )
291 {
292 request.setBaseDirectory( file.getParentFile() );
293 request.setPomFile( file );
294 }
295 else
296 {
297 request.setBaseDirectory( file );
298 request.setPomFile( null );
299 }
300 }
301
302 String goals = get( InvocationProperty.GOALS, index );
303 if ( goals != null )
304 {
305 request.setGoals( new ArrayList<String>( Arrays.asList( StringUtils.split( goals, ", \t\n\r\f" ) ) ) );
306 }
307
308 String profiles = get( InvocationProperty.PROFILES, index );
309 if ( profiles != null )
310 {
311
312 request.setProfiles( new ArrayList<String>( Arrays.asList( StringUtils.split( profiles,
313 ", \t\n\r\f" ) ) ) );
314
315 }
316
317 String mvnOpts = get( InvocationProperty.MAVEN_OPTS, index );
318 if ( mvnOpts != null )
319 {
320 request.setMavenOpts( mvnOpts );
321 }
322
323 String failureBehavior = get( InvocationProperty.FAILURE_BEHAVIOR, index );
324 if ( failureBehavior != null )
325 {
326 ReactorFailureBehavior valueOf =
327 InvocationRequest.ReactorFailureBehavior.valueOfByLongOption( failureBehavior );
328 request.setReactorFailureBehavior( valueOf );
329 }
330
331 String nonRecursive = get( InvocationProperty.NON_RECURSIVE, index );
332 if ( nonRecursive != null )
333 {
334 request.setRecursive( !Boolean.valueOf( nonRecursive ) );
335 }
336
337 String offline = get( InvocationProperty.OFFLINE, index );
338 if ( offline != null )
339 {
340 request.setOffline( Boolean.valueOf( offline ) );
341 }
342
343 String debug = get( InvocationProperty.DEBUG, index );
344 if ( debug != null )
345 {
346 request.setDebug( Boolean.valueOf( debug ) );
347 }
348 }
349
350
351
352
353
354
355
356
357
358 public boolean isExpectedResult( int exitCode, int index )
359 {
360 boolean nonZeroExit = "failure".equalsIgnoreCase( get( "invoker.buildResult", index ) );
361 return ( exitCode != 0 ) == nonZeroExit;
362 }
363
364
365
366
367
368
369
370 public String getSystemPropertiesFile( int index )
371 {
372 return get( InvocationProperty.SYSTEM_PROPERTIES_FILE, index );
373 }
374
375
376
377
378
379
380
381 public String getSettingsFile( int index )
382 {
383 return get( InvocationProperty.SETTINGS_FILE, index );
384 }
385
386
387
388
389
390
391 public int getTimeoutInSeconds( int index )
392 {
393 String timeoutInSecondsStr = get( InvocationProperty.TIMEOUT_IN_SECONDS, index );
394 if ( StringUtils.isEmpty( timeoutInSecondsStr ) )
395 {
396 return -1;
397 }
398
399 return Integer.parseInt( timeoutInSecondsStr );
400 }
401
402
403
404
405
406
407
408
409
410
411 String get( String key, int index )
412 {
413 if ( index < 0 )
414 {
415 throw new IllegalArgumentException( "invalid invocation index: " + index );
416 }
417
418 String value = properties.getProperty( key + '.' + index );
419 if ( value == null )
420 {
421 value = properties.getProperty( key );
422 }
423 return value;
424 }
425
426 private String get( InvocationProperty prop, int index )
427 {
428 return get( prop.toString(), index );
429 }
430 }