1 | |
package org.apache.maven.cli; |
2 | |
|
3 | |
import org.apache.commons.cli.CommandLine; |
4 | |
import org.apache.commons.cli.CommandLineParser; |
5 | |
import org.apache.commons.cli.GnuParser; |
6 | |
import org.apache.commons.cli.HelpFormatter; |
7 | |
import org.apache.commons.cli.OptionBuilder; |
8 | |
import org.apache.commons.cli.Options; |
9 | |
import org.apache.commons.cli.ParseException; |
10 | |
|
11 | |
import java.util.ArrayList; |
12 | |
import java.util.List; |
13 | |
|
14 | |
public class CLIManager |
15 | |
{ |
16 | |
|
17 | |
public static final char ALTERNATE_POM_FILE = 'f'; |
18 | |
|
19 | |
public static final char BATCH_MODE = 'B'; |
20 | |
|
21 | |
public static final char SET_SYSTEM_PROPERTY = 'D'; |
22 | |
|
23 | |
public static final char OFFLINE = 'o'; |
24 | |
|
25 | |
public static final char REACTOR = 'r'; |
26 | |
|
27 | |
public static final char QUIET = 'q'; |
28 | |
|
29 | |
public static final char DEBUG = 'X'; |
30 | |
|
31 | |
public static final char ERRORS = 'e'; |
32 | |
|
33 | |
public static final char HELP = 'h'; |
34 | |
|
35 | |
public static final char VERSION = 'v'; |
36 | |
|
37 | |
public static final char SHOW_VERSION = 'V'; |
38 | |
|
39 | |
private Options options; |
40 | |
|
41 | |
public static final char NON_RECURSIVE = 'N'; |
42 | |
|
43 | |
public static final char UPDATE_SNAPSHOTS = 'U'; |
44 | |
|
45 | |
public static final char ACTIVATE_PROFILES = 'P'; |
46 | |
|
47 | |
public static final String FORCE_PLUGIN_UPDATES = "cpu"; |
48 | |
|
49 | |
public static final String FORCE_PLUGIN_UPDATES2 = "up"; |
50 | |
|
51 | |
public static final String SUPPRESS_PLUGIN_UPDATES = "npu"; |
52 | |
|
53 | |
public static final String SUPPRESS_PLUGIN_REGISTRY = "npr"; |
54 | |
|
55 | |
public static final char CHECKSUM_FAILURE_POLICY = 'C'; |
56 | |
|
57 | |
public static final char CHECKSUM_WARNING_POLICY = 'c'; |
58 | |
|
59 | |
public static final char ALTERNATE_USER_SETTINGS = 's'; |
60 | |
|
61 | |
public static final String FAIL_FAST = "ff"; |
62 | |
|
63 | |
public static final String FAIL_AT_END = "fae"; |
64 | |
|
65 | |
public static final String FAIL_NEVER = "fn"; |
66 | |
|
67 | |
public CLIManager() |
68 | 0 | { |
69 | 0 | options = new Options(); |
70 | |
|
71 | 0 | options.addOption( OptionBuilder.withLongOpt( "file" ) |
72 | |
.hasArg() |
73 | |
.withDescription( "Force the use of an alternate POM file." ) |
74 | |
.create( ALTERNATE_POM_FILE ) ); |
75 | |
|
76 | 0 | options.addOption( OptionBuilder.withLongOpt( "define" ) |
77 | |
.hasArg() |
78 | |
.withDescription( "Define a system property" ) |
79 | |
.create( SET_SYSTEM_PROPERTY ) ); |
80 | |
|
81 | 0 | options.addOption( OptionBuilder.withLongOpt( "offline" ).withDescription( "Work offline" ).create( OFFLINE ) ); |
82 | |
|
83 | 0 | options.addOption( OptionBuilder.withLongOpt( "help" ) |
84 | |
.withDescription( "Display help information" ) |
85 | |
.create( HELP ) ); |
86 | |
|
87 | 0 | options.addOption( OptionBuilder.withLongOpt( "version" ) |
88 | |
.withDescription( "Display version information" ) |
89 | |
.create( VERSION ) ); |
90 | |
|
91 | 0 | options.addOption( OptionBuilder.withLongOpt( "quiet" ) |
92 | |
.withDescription( "Quiet output - only show errors" ) |
93 | |
.create( QUIET ) ); |
94 | |
|
95 | 0 | options.addOption( OptionBuilder.withLongOpt( "debug" ) |
96 | |
.withDescription( "Produce execution debug output" ) |
97 | |
.create( DEBUG ) ); |
98 | |
|
99 | 0 | options.addOption( OptionBuilder.withLongOpt( "errors" ) |
100 | |
.withDescription( "Produce execution error messages" ) |
101 | |
.create( ERRORS ) ); |
102 | |
|
103 | 0 | options.addOption( OptionBuilder.withLongOpt( "reactor" ) |
104 | |
.withDescription( "Dynamically build reactor from subdirectories" ) |
105 | |
.create( REACTOR ) ); |
106 | |
|
107 | 0 | options.addOption( OptionBuilder.withLongOpt( "non-recursive" ) |
108 | |
.withDescription( "Do not recurse into sub-projects" ) |
109 | |
.create( NON_RECURSIVE ) ); |
110 | |
|
111 | 0 | options.addOption( OptionBuilder.withLongOpt( "update-snapshots" ) |
112 | |
.withDescription( |
113 | |
"Forces a check for updated releases and snapshots on remote repositories" ) |
114 | |
.create( UPDATE_SNAPSHOTS ) ); |
115 | |
|
116 | 0 | options.addOption( OptionBuilder.withLongOpt( "activate-profiles" ) |
117 | |
.withDescription( "Comma-delimited list of profiles to activate" ) |
118 | |
.hasArg() |
119 | |
.create( ACTIVATE_PROFILES ) ); |
120 | |
|
121 | 0 | options.addOption( OptionBuilder.withLongOpt( "batch-mode" ) |
122 | |
.withDescription( "Run in non-interactive (batch) mode" ) |
123 | |
.create( BATCH_MODE ) ); |
124 | |
|
125 | 0 | options.addOption( OptionBuilder.withLongOpt( "check-plugin-updates" ) |
126 | |
.withDescription( "Force upToDate check for any relevant registered plugins" ) |
127 | |
.create( FORCE_PLUGIN_UPDATES ) ); |
128 | |
|
129 | 0 | options.addOption( OptionBuilder.withLongOpt( "update-plugins" ) |
130 | |
.withDescription( "Synonym for " + FORCE_PLUGIN_UPDATES ) |
131 | |
.create( FORCE_PLUGIN_UPDATES2 ) ); |
132 | |
|
133 | 0 | options.addOption( OptionBuilder.withLongOpt( "no-plugin-updates" ) |
134 | |
.withDescription( "Suppress upToDate check for any relevant registered plugins" ) |
135 | |
.create( SUPPRESS_PLUGIN_UPDATES ) ); |
136 | |
|
137 | 0 | options.addOption( OptionBuilder.withLongOpt( "no-plugin-registry" ) |
138 | |
.withDescription( "Don't use ~/.m2/plugin-registry.xml for plugin versions" ) |
139 | |
.create( SUPPRESS_PLUGIN_REGISTRY ) ); |
140 | |
|
141 | 0 | options.addOption( OptionBuilder.withLongOpt( "strict-checksums" ) |
142 | |
.withDescription( "Fail the build if checksums don't match" ) |
143 | |
.create( CHECKSUM_FAILURE_POLICY ) ); |
144 | |
|
145 | 0 | options.addOption( OptionBuilder.withLongOpt( "lax-checksums" ) |
146 | |
.withDescription( "Warn if checksums don't match" ) |
147 | |
.create( CHECKSUM_WARNING_POLICY ) ); |
148 | |
|
149 | 0 | options.addOption( OptionBuilder.withLongOpt( "settings" ) |
150 | |
.withDescription( "Alternate path for the user settings file" ) |
151 | |
.hasArg() |
152 | |
.create( ALTERNATE_USER_SETTINGS ) ); |
153 | |
|
154 | 0 | options.addOption( OptionBuilder.withLongOpt( "fail-fast" ) |
155 | |
.withDescription( "Stop at first failure in reactorized builds" ) |
156 | |
.create( FAIL_FAST ) ); |
157 | |
|
158 | 0 | options.addOption( OptionBuilder.withLongOpt( "fail-at-end" ) |
159 | |
.withDescription( |
160 | |
"Only fail the build afterwards; allow all non-impacted builds to continue" ) |
161 | |
.create( FAIL_AT_END ) ); |
162 | |
|
163 | 0 | options.addOption( OptionBuilder.withLongOpt( "fail-never" ) |
164 | |
.withDescription( "NEVER fail the build, regardless of project result" ) |
165 | |
.create( FAIL_NEVER ) ); |
166 | |
|
167 | 0 | options.addOption( OptionBuilder.withLongOpt( "show-version" ) |
168 | |
.withDescription( "Display version information WITHOUT stopping build" ) |
169 | |
.create( SHOW_VERSION ) ); |
170 | 0 | } |
171 | |
|
172 | |
public CommandLine parse( String[] args ) |
173 | |
throws ParseException |
174 | |
{ |
175 | |
|
176 | 0 | String[] cleanArgs = cleanArgs( args ); |
177 | |
|
178 | 0 | CommandLineParser parser = new GnuParser(); |
179 | 0 | return parser.parse( options, cleanArgs ); |
180 | |
} |
181 | |
|
182 | |
private String[] cleanArgs( String[] args ) |
183 | |
{ |
184 | 0 | List cleaned = new ArrayList(); |
185 | |
|
186 | 0 | StringBuffer currentArg = null; |
187 | |
|
188 | 0 | for ( int i = 0; i < args.length; i++ ) |
189 | |
{ |
190 | 0 | String arg = args[i]; |
191 | |
|
192 | |
|
193 | |
|
194 | 0 | boolean addedToBuffer = false; |
195 | |
|
196 | 0 | if ( arg.startsWith( "\"" ) ) |
197 | |
{ |
198 | |
|
199 | |
|
200 | 0 | if ( currentArg != null ) |
201 | |
{ |
202 | |
|
203 | 0 | cleaned.add( currentArg.toString() ); |
204 | |
} |
205 | |
|
206 | |
|
207 | 0 | currentArg = new StringBuffer( arg.substring( 1 ) ); |
208 | 0 | addedToBuffer = true; |
209 | |
} |
210 | |
|
211 | |
|
212 | 0 | if ( arg.endsWith( "\"" ) ) |
213 | |
{ |
214 | 0 | String cleanArgPart = arg.substring( 0, arg.length() - 1 ); |
215 | |
|
216 | |
|
217 | 0 | if ( currentArg != null ) |
218 | |
{ |
219 | |
|
220 | 0 | if ( addedToBuffer ) |
221 | |
{ |
222 | |
|
223 | 0 | currentArg.setLength( currentArg.length() - 1 ); |
224 | |
} |
225 | |
|
226 | |
else |
227 | |
{ |
228 | |
|
229 | |
|
230 | 0 | currentArg.append( ' ' ).append( cleanArgPart ); |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | 0 | cleaned.add( currentArg.toString() ); |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | |
|
241 | |
|
242 | 0 | cleaned.add( cleanArgPart ); |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | 0 | currentArg = null; |
248 | 0 | continue; |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | 0 | if ( !addedToBuffer ) |
256 | |
{ |
257 | |
|
258 | 0 | if ( currentArg != null ) |
259 | |
{ |
260 | |
|
261 | 0 | currentArg.append( ' ' ).append( arg ); |
262 | |
} |
263 | |
|
264 | |
else |
265 | |
{ |
266 | |
|
267 | 0 | cleaned.add( arg ); |
268 | |
} |
269 | |
} |
270 | |
} |
271 | |
|
272 | |
|
273 | 0 | if ( currentArg != null ) |
274 | |
{ |
275 | |
|
276 | 0 | cleaned.add( currentArg.toString() ); |
277 | |
} |
278 | |
|
279 | 0 | int cleanedSz = cleaned.size(); |
280 | 0 | String[] cleanArgs = null; |
281 | |
|
282 | 0 | if ( cleanedSz == 0 ) |
283 | |
{ |
284 | |
|
285 | 0 | cleanArgs = args; |
286 | |
} |
287 | |
else |
288 | |
{ |
289 | |
|
290 | 0 | cleanArgs = (String[]) cleaned.toArray( new String[cleanedSz] ); |
291 | |
} |
292 | |
|
293 | 0 | return cleanArgs; |
294 | |
} |
295 | |
|
296 | |
public void displayHelp() |
297 | |
{ |
298 | 0 | System.out.println(); |
299 | |
|
300 | 0 | HelpFormatter formatter = new HelpFormatter(); |
301 | 0 | formatter.printHelp( "mvn [options] [<goal(s)>] [<phase(s)>]", "\nOptions:", options, "\n" ); |
302 | 0 | } |
303 | |
|
304 | |
} |