1 package org.apache.maven.plugin.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.Properties;
26
27 import org.apache.maven.shared.invoker.InvocationRequest;
28 import org.codehaus.plexus.util.StringUtils;
29
30
31
32
33
34
35
36 class InvokerProperties
37 {
38
39 private enum InvocationProperty
40 {
41 PROJECT( "invoker.project" ),
42 GOALS( "invoker.goals" ),
43 PROFILES( "invoker.profiles" ),
44 MAVEN_OPTS( "invoker.mavenOpts" ),
45 FAILURE_BEHAVIOR( "invoker.failureBehavior" ),
46 NON_RECURSIVE( "invoker.nonRecursive" ),
47 OFFLINE( "invoker.offline" ),
48 SYSTEM_PROPERTIES_FILE( "invoker.systemPropertiesFile" ),
49 DEBUG( "invoker.debug" );
50
51 private final String key;
52
53 private InvocationProperty( final String s )
54 {
55 this.key = s;
56 }
57
58 @Override
59 public String toString()
60 {
61 return key;
62 }
63 }
64
65
66
67
68 private final Properties properties;
69
70
71
72
73
74
75
76 public InvokerProperties( Properties properties )
77 {
78 this.properties = ( properties != null ) ? properties : new Properties();
79 }
80
81
82
83
84
85
86 public Properties getProperties()
87 {
88 return this.properties;
89 }
90
91
92
93
94
95
96 public String getJobName()
97 {
98 return this.properties.getProperty( "invoker.name", "" );
99 }
100
101
102
103
104
105
106 public String getJobDescription()
107 {
108 return this.properties.getProperty( "invoker.description", "" );
109 }
110
111
112
113
114
115
116 public String getJreVersion()
117 {
118 return this.properties.getProperty( "invoker.java.version", "" );
119 }
120
121
122
123
124
125
126
127 public String getMavenVersion()
128 {
129 return this.properties.getProperty( "invoker.maven.version", "" );
130 }
131
132
133
134
135
136
137 public String getOsFamily()
138 {
139 return this.properties.getProperty( "invoker.os.family", "" );
140 }
141
142
143
144
145
146
147
148 public boolean isInvocationDefined( int index )
149 {
150 for ( InvocationProperty prop : InvocationProperty.values() )
151 {
152 if ( properties.getProperty( prop.toString() + '.' + index ) != null )
153 {
154 return true;
155 }
156 }
157 return false;
158 }
159
160
161
162
163
164
165
166
167 public void configureInvocation( InvocationRequest request, int index )
168 {
169 String project = get( InvocationProperty.PROJECT, index );
170 if ( project != null )
171 {
172 File file = new File( request.getBaseDirectory(), project );
173 if ( file.isFile() )
174 {
175 request.setBaseDirectory( file.getParentFile() );
176 request.setPomFile( file );
177 }
178 else
179 {
180 request.setBaseDirectory( file );
181 request.setPomFile( null );
182 }
183 }
184
185 String goals = get( InvocationProperty.GOALS, index );
186 if ( goals != null )
187 {
188 request.setGoals( new ArrayList<String>( Arrays.asList( StringUtils.split( goals, ", \t\n\r\f" ) ) ) );
189 }
190
191 String profiles = get( InvocationProperty.PROFILES, index );
192 if ( profiles != null )
193 {
194
195 request.setProfiles( new ArrayList<String>( Arrays.asList( StringUtils.split( profiles, ", \t\n\r\f" ) ) ) );
196
197 }
198
199 String mvnOpts = get( InvocationProperty.MAVEN_OPTS, index );
200 if ( mvnOpts != null )
201 {
202 request.setMavenOpts( mvnOpts );
203 }
204
205 String failureBehavior = get( InvocationProperty.FAILURE_BEHAVIOR, index );
206 if ( failureBehavior != null )
207 {
208 request.setFailureBehavior( failureBehavior );
209 }
210
211 String nonRecursive = get( InvocationProperty.NON_RECURSIVE, index );
212 if ( nonRecursive != null )
213 {
214 request.setRecursive( !Boolean.valueOf( nonRecursive ) );
215 }
216
217 String offline = get( InvocationProperty.OFFLINE, index );
218 if ( offline != null )
219 {
220 request.setOffline( Boolean.valueOf( offline ) );
221 }
222
223 String debug = get( InvocationProperty.DEBUG, index );
224 if ( debug != null )
225 {
226 request.setDebug( Boolean.valueOf( debug ) );
227 }
228 }
229
230
231
232
233
234
235
236
237
238 public boolean isExpectedResult( int exitCode, int index )
239 {
240 boolean nonZeroExit = "failure".equalsIgnoreCase( get( "invoker.buildResult", index ) );
241 return ( exitCode != 0 ) == nonZeroExit;
242 }
243
244
245
246
247
248
249
250 public String getSystemPropertiesFile( int index )
251 {
252 return get( InvocationProperty.SYSTEM_PROPERTIES_FILE, index );
253 }
254
255
256
257
258
259
260
261
262
263
264
265 String get( String key, int index )
266 {
267 if ( index < 0 )
268 {
269 throw new IllegalArgumentException( "invalid invocation index: " + index );
270 }
271
272 String value = properties.getProperty( key + '.' + index );
273 if ( value == null )
274 {
275 value = properties.getProperty( key );
276 }
277 return value;
278 }
279
280 private String get( InvocationProperty prop, int index )
281 {
282 return get( prop.toString(), index );
283 }
284 }