1 package org.apache.maven.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.PrintStream;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.cli.CommandLine;
28 import org.apache.commons.cli.CommandLineParser;
29 import org.apache.commons.cli.GnuParser;
30 import org.apache.commons.cli.HelpFormatter;
31 import org.apache.commons.cli.OptionBuilder;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.ParseException;
34
35
36
37
38 public class CLIManager
39 {
40 public static final char ALTERNATE_POM_FILE = 'f';
41
42 public static final char BATCH_MODE = 'B';
43
44 public static final char SET_SYSTEM_PROPERTY = 'D';
45
46 public static final char OFFLINE = 'o';
47
48 public static final char QUIET = 'q';
49
50 public static final char DEBUG = 'X';
51
52 public static final char ERRORS = 'e';
53
54 public static final char HELP = 'h';
55
56 public static final char VERSION = 'v';
57
58 public static final char SHOW_VERSION = 'V';
59
60 public static final char NON_RECURSIVE = 'N';
61
62 public static final char UPDATE_SNAPSHOTS = 'U';
63
64 public static final char ACTIVATE_PROFILES = 'P';
65
66 public static final String SUPRESS_SNAPSHOT_UPDATES = "nsu";
67
68 public static final char CHECKSUM_FAILURE_POLICY = 'C';
69
70 public static final char CHECKSUM_WARNING_POLICY = 'c';
71
72 public static final char ALTERNATE_USER_SETTINGS = 's';
73
74 public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
75
76 public static final char ALTERNATE_USER_TOOLCHAINS = 't';
77
78 public static final String ALTERNATE_GLOBAL_TOOLCHAINS = "gt";
79
80 public static final String FAIL_FAST = "ff";
81
82 public static final String FAIL_AT_END = "fae";
83
84 public static final String FAIL_NEVER = "fn";
85
86 public static final String RESUME_FROM = "rf";
87
88 public static final String PROJECT_LIST = "pl";
89
90 public static final String ALSO_MAKE = "am";
91
92 public static final String ALSO_MAKE_DEPENDENTS = "amd";
93
94 public static final String LOG_FILE = "l";
95
96 public static final String ENCRYPT_MASTER_PASSWORD = "emp";
97
98 public static final String ENCRYPT_PASSWORD = "ep";
99
100 public static final String THREADS = "T";
101
102 public static final String LEGACY_LOCAL_REPOSITORY = "llr";
103
104 public static final String BUILDER = "b";
105
106 protected Options options;
107
108 @SuppressWarnings( { "static-access", "checkstyle:linelength" } )
109 public CLIManager()
110 {
111 options = new Options();
112 options.addOption( OptionBuilder.withLongOpt( "help" ).withDescription( "Display help information" ).create( HELP ) );
113 options.addOption( OptionBuilder.withLongOpt( "file" ).hasArg().withDescription( "Force the use of an alternate POM file (or directory with pom.xml)." ).create( ALTERNATE_POM_FILE ) );
114 options.addOption( OptionBuilder.withLongOpt( "define" ).hasArg().withDescription( "Define a system property" ).create( SET_SYSTEM_PROPERTY ) );
115 options.addOption( OptionBuilder.withLongOpt( "offline" ).withDescription( "Work offline" ).create( OFFLINE ) );
116 options.addOption( OptionBuilder.withLongOpt( "version" ).withDescription( "Display version information" ).create( VERSION ) );
117 options.addOption( OptionBuilder.withLongOpt( "quiet" ).withDescription( "Quiet output - only show errors" ).create( QUIET ) );
118 options.addOption( OptionBuilder.withLongOpt( "debug" ).withDescription( "Produce execution debug output" ).create( DEBUG ) );
119 options.addOption( OptionBuilder.withLongOpt( "errors" ).withDescription( "Produce execution error messages" ).create( ERRORS ) );
120 options.addOption( OptionBuilder.withLongOpt( "non-recursive" ).withDescription( "Do not recurse into sub-projects" ).create( NON_RECURSIVE ) );
121 options.addOption( OptionBuilder.withLongOpt( "update-snapshots" ).withDescription( "Forces a check for missing releases and updated snapshots on remote repositories" ).create( UPDATE_SNAPSHOTS ) );
122 options.addOption( OptionBuilder.withLongOpt( "activate-profiles" ).withDescription( "Comma-delimited list of profiles to activate" ).hasArg().create( ACTIVATE_PROFILES ) );
123 options.addOption( OptionBuilder.withLongOpt( "batch-mode" ).withDescription( "Run in non-interactive (batch) mode" ).create( BATCH_MODE ) );
124 options.addOption( OptionBuilder.withLongOpt( "no-snapshot-updates" ).withDescription( "Suppress SNAPSHOT updates" ).create( SUPRESS_SNAPSHOT_UPDATES ) );
125 options.addOption( OptionBuilder.withLongOpt( "strict-checksums" ).withDescription( "Fail the build if checksums don't match" ).create( CHECKSUM_FAILURE_POLICY ) );
126 options.addOption( OptionBuilder.withLongOpt( "lax-checksums" ).withDescription( "Warn if checksums don't match" ).create( CHECKSUM_WARNING_POLICY ) );
127 options.addOption( OptionBuilder.withLongOpt( "settings" ).withDescription( "Alternate path for the user settings file" ).hasArg().create( ALTERNATE_USER_SETTINGS ) );
128 options.addOption( OptionBuilder.withLongOpt( "global-settings" ).withDescription( "Alternate path for the global settings file" ).hasArg().create( ALTERNATE_GLOBAL_SETTINGS ) );
129 options.addOption( OptionBuilder.withLongOpt( "toolchains" ).withDescription( "Alternate path for the user toolchains file" ).hasArg().create( ALTERNATE_USER_TOOLCHAINS ) );
130 options.addOption( OptionBuilder.withLongOpt( "global-toolchains" ).withDescription( "Alternate path for the global toolchains file" ).hasArg().create( ALTERNATE_GLOBAL_TOOLCHAINS ) );
131 options.addOption( OptionBuilder.withLongOpt( "fail-fast" ).withDescription( "Stop at first failure in reactorized builds" ).create( FAIL_FAST ) );
132 options.addOption( OptionBuilder.withLongOpt( "fail-at-end" ).withDescription( "Only fail the build afterwards; allow all non-impacted builds to continue" ).create( FAIL_AT_END ) );
133 options.addOption( OptionBuilder.withLongOpt( "fail-never" ).withDescription( "NEVER fail the build, regardless of project result" ).create( FAIL_NEVER ) );
134 options.addOption( OptionBuilder.withLongOpt( "resume-from" ).hasArg().withDescription( "Resume reactor from specified project" ).create( RESUME_FROM ) );
135 options.addOption( OptionBuilder.withLongOpt( "projects" ).withDescription( "Comma-delimited list of specified reactor projects to build instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path." ).hasArg().create( PROJECT_LIST ) );
136 options.addOption( OptionBuilder.withLongOpt( "also-make" ).withDescription( "If project list is specified, also build projects required by the list" ).create( ALSO_MAKE ) );
137 options.addOption( OptionBuilder.withLongOpt( "also-make-dependents" ).withDescription( "If project list is specified, also build projects that depend on projects on the list" ).create( ALSO_MAKE_DEPENDENTS ) );
138 options.addOption( OptionBuilder.withLongOpt( "log-file" ).hasArg().withDescription( "Log file to where all build output will go." ).create( LOG_FILE ) );
139 options.addOption( OptionBuilder.withLongOpt( "show-version" ).withDescription( "Display version information WITHOUT stopping build" ).create( SHOW_VERSION ) );
140 options.addOption( OptionBuilder.withLongOpt( "encrypt-master-password" ).hasOptionalArg().withDescription( "Encrypt master security password" ).create( ENCRYPT_MASTER_PASSWORD ) );
141 options.addOption( OptionBuilder.withLongOpt( "encrypt-password" ).hasOptionalArg().withDescription( "Encrypt server password" ).create( ENCRYPT_PASSWORD ) );
142 options.addOption( OptionBuilder.withLongOpt( "threads" ).hasArg().withDescription( "Thread count, for instance 2.0C where C is core multiplied" ).create( THREADS ) );
143 options.addOption( OptionBuilder.withLongOpt( "legacy-local-repository" ).withDescription( "Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be activated by using -Dmaven.legacyLocalRepo=true" ).create( LEGACY_LOCAL_REPOSITORY ) );
144 options.addOption( OptionBuilder.withLongOpt( "builder" ).hasArg().withDescription( "The id of the build strategy to use." ).create( BUILDER ) );
145
146
147 options.addOption( OptionBuilder.withLongOpt( "no-plugin-registry" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npr" ) );
148 options.addOption( OptionBuilder.withLongOpt( "check-plugin-updates" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "cpu" ) );
149 options.addOption( OptionBuilder.withLongOpt( "update-plugins" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "up" ) );
150 options.addOption( OptionBuilder.withLongOpt( "no-plugin-updates" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npu" ) );
151 }
152
153 public CommandLine parse( String[] args )
154 throws ParseException
155 {
156
157 String[] cleanArgs = cleanArgs( args );
158
159 CommandLineParser parser = new GnuParser();
160
161 return parser.parse( options, cleanArgs );
162 }
163
164 private String[] cleanArgs( String[] args )
165 {
166 List<String> cleaned = new ArrayList<String>();
167
168 StringBuilder currentArg = null;
169
170 for ( String arg : args )
171 {
172 boolean addedToBuffer = false;
173
174 if ( arg.startsWith( "\"" ) )
175 {
176
177
178 if ( currentArg != null )
179 {
180 cleaned.add( currentArg.toString() );
181 }
182
183
184 currentArg = new StringBuilder( arg.substring( 1 ) );
185 addedToBuffer = true;
186 }
187
188
189 if ( arg.endsWith( "\"" ) )
190 {
191 String cleanArgPart = arg.substring( 0, arg.length() - 1 );
192
193
194 if ( currentArg != null )
195 {
196
197 if ( addedToBuffer )
198 {
199 currentArg.setLength( currentArg.length() - 1 );
200 }
201
202 else
203 {
204
205 currentArg.append( ' ' ).append( cleanArgPart );
206 }
207
208 cleaned.add( currentArg.toString() );
209 }
210 else
211 {
212 cleaned.add( cleanArgPart );
213 }
214
215 currentArg = null;
216
217 continue;
218 }
219
220
221
222
223
224 if ( !addedToBuffer )
225 {
226 if ( currentArg != null )
227 {
228 currentArg.append( ' ' ).append( arg );
229 }
230 else
231 {
232 cleaned.add( arg );
233 }
234 }
235 }
236
237 if ( currentArg != null )
238 {
239 cleaned.add( currentArg.toString() );
240 }
241
242 int cleanedSz = cleaned.size();
243
244 String[] cleanArgs;
245
246 if ( cleanedSz == 0 )
247 {
248 cleanArgs = args;
249 }
250 else
251 {
252 cleanArgs = cleaned.toArray( new String[cleanedSz] );
253 }
254
255 return cleanArgs;
256 }
257
258 public void displayHelp( PrintStream stdout )
259 {
260 stdout.println();
261
262 PrintWriter pw = new PrintWriter( stdout );
263
264 HelpFormatter formatter = new HelpFormatter();
265
266 formatter.printHelp( pw, HelpFormatter.DEFAULT_WIDTH, "mvn [options] [<goal(s)>] [<phase(s)>]", "\nOptions:",
267 options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, "\n", false );
268
269 pw.flush();
270 }
271 }