View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cli;
20  
21  import java.io.PrintStream;
22  import java.io.PrintWriter;
23  
24  import org.apache.commons.cli.CommandLine;
25  import org.apache.commons.cli.CommandLineParser;
26  import org.apache.commons.cli.GnuParser;
27  import org.apache.commons.cli.HelpFormatter;
28  import org.apache.commons.cli.Option;
29  import org.apache.commons.cli.Options;
30  import org.apache.commons.cli.ParseException;
31  
32  /**
33   * @author Jason van Zyl
34   */
35  public class CLIManager {
36      public static final char ALTERNATE_POM_FILE = 'f';
37  
38      public static final char BATCH_MODE = 'B';
39  
40      public static final char SET_USER_PROPERTY = 'D';
41  
42      /**
43       * @deprecated Use {@link #SET_USER_PROPERTY}
44       */
45      @Deprecated
46      public static final char SET_SYSTEM_PROPERTY = SET_USER_PROPERTY;
47  
48      public static final char OFFLINE = 'o';
49  
50      public static final char QUIET = 'q';
51  
52      public static final char DEBUG = 'X';
53  
54      public static final char ERRORS = 'e';
55  
56      public static final char HELP = 'h';
57  
58      public static final char VERSION = 'v';
59  
60      public static final char SHOW_VERSION = 'V';
61  
62      public static final char NON_RECURSIVE = 'N';
63  
64      public static final char UPDATE_SNAPSHOTS = 'U';
65  
66      public static final String UPDATE_METADATA = "UM";
67  
68      public static final String UPDATE_ARTIFACTS = "UA";
69  
70      public static final char ACTIVATE_PROFILES = 'P';
71  
72      public static final String SUPRESS_SNAPSHOT_UPDATES = "nsu";
73  
74      public static final char CHECKSUM_FAILURE_POLICY = 'C';
75  
76      public static final char CHECKSUM_WARNING_POLICY = 'c';
77  
78      public static final char ALTERNATE_USER_SETTINGS = 's';
79  
80      public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
81  
82      public static final char ALTERNATE_USER_TOOLCHAINS = 't';
83  
84      public static final String ALTERNATE_GLOBAL_TOOLCHAINS = "gt";
85  
86      public static final String FAIL_FAST = "ff";
87  
88      public static final String FAIL_AT_END = "fae";
89  
90      public static final String FAIL_NEVER = "fn";
91  
92      public static final String RESUME_FROM = "rf";
93  
94      public static final String PROJECT_LIST = "pl";
95  
96      public static final String ALSO_MAKE = "am";
97  
98      public static final String ALSO_MAKE_DEPENDENTS = "amd";
99  
100     public static final String LOG_FILE = "l";
101 
102     public static final String ENCRYPT_MASTER_PASSWORD = "emp";
103 
104     public static final String ENCRYPT_PASSWORD = "ep";
105 
106     public static final String THREADS = "T";
107 
108     public static final String BUILDER = "b";
109 
110     public static final String NO_TRANSFER_PROGRESS = "ntp";
111 
112     public static final String COLOR = "color";
113 
114     public static final String IGNORE_TRANSITIVE_REPOSITORIES = "itr";
115 
116     public static final String RAW_STREAMS = "raw-streams";
117 
118     public static final String ARTIFACTS_UPDATE_POLICY = "artifacts-update-policy";
119 
120     public static final String METADATA_UPDATE_POLICY = "metadata-update-policy";
121 
122     protected Options options;
123 
124     @SuppressWarnings({"checkstyle:linelength", "checkstyle:MethodLength"})
125     public CLIManager() {
126         options = new Options();
127         options.addOption(Option.builder(Character.toString(HELP))
128                 .longOpt("help")
129                 .desc("Display help information")
130                 .build());
131         options.addOption(Option.builder(Character.toString(ALTERNATE_POM_FILE))
132                 .longOpt("file")
133                 .hasArg()
134                 .desc("Force the use of an alternate POM file (or directory with pom.xml)")
135                 .build());
136         options.addOption(Option.builder(Character.toString(SET_USER_PROPERTY))
137                 .longOpt("define")
138                 .hasArg()
139                 .desc("Define a user property")
140                 .build());
141         options.addOption(Option.builder(Character.toString(OFFLINE))
142                 .longOpt("offline")
143                 .desc("Work offline")
144                 .build());
145         options.addOption(Option.builder(Character.toString(VERSION))
146                 .longOpt("version")
147                 .desc("Display version information")
148                 .build());
149         options.addOption(Option.builder(Character.toString(QUIET))
150                 .longOpt("quiet")
151                 .desc("Quiet output - only show errors")
152                 .build());
153         options.addOption(Option.builder(Character.toString(DEBUG))
154                 .longOpt("debug")
155                 .desc("Produce execution debug output")
156                 .build());
157         options.addOption(Option.builder(Character.toString(ERRORS))
158                 .longOpt("errors")
159                 .desc("Produce execution error messages")
160                 .build());
161         options.addOption(Option.builder(Character.toString(NON_RECURSIVE))
162                 .longOpt("non-recursive")
163                 .desc("Do not recurse into sub-projects")
164                 .build());
165         options.addOption(Option.builder(Character.toString(UPDATE_SNAPSHOTS))
166                 .longOpt("update-snapshots")
167                 .desc("Forces a check for missing releases and updated snapshots on remote repositories")
168                 .build());
169         options.addOption(Option.builder(UPDATE_METADATA)
170                 .longOpt("update-metadata")
171                 .desc(
172                         "Forces updates of remote repository metadata (updates plugin prefixes, version ranges and snapshots)")
173                 .build());
174         options.addOption(Option.builder(UPDATE_ARTIFACTS)
175                 .longOpt("update-artifacts")
176                 .desc("Forces checks for missing artifacts (retries cached retrieval errors)")
177                 .build());
178         options.addOption(Option.builder(Character.toString(ACTIVATE_PROFILES))
179                 .longOpt("activate-profiles")
180                 .desc("Comma-delimited list of profiles to activate")
181                 .hasArg()
182                 .build());
183         options.addOption(Option.builder(Character.toString(BATCH_MODE))
184                 .longOpt("batch-mode")
185                 .desc("Run in non-interactive (batch) mode (disables output color)")
186                 .build());
187         options.addOption(Option.builder(SUPRESS_SNAPSHOT_UPDATES)
188                 .longOpt("no-snapshot-updates")
189                 .desc("Suppress SNAPSHOT updates")
190                 .build());
191         options.addOption(Option.builder(Character.toString(CHECKSUM_FAILURE_POLICY))
192                 .longOpt("strict-checksums")
193                 .desc("Fail the build if checksums don't match")
194                 .build());
195         options.addOption(Option.builder(Character.toString(CHECKSUM_WARNING_POLICY))
196                 .longOpt("lax-checksums")
197                 .desc("Warn if checksums don't match")
198                 .build());
199         options.addOption(Option.builder(Character.toString(ALTERNATE_USER_SETTINGS))
200                 .longOpt("settings")
201                 .desc("Alternate path for the user settings file")
202                 .hasArg()
203                 .build());
204         options.addOption(Option.builder(ALTERNATE_GLOBAL_SETTINGS)
205                 .longOpt("global-settings")
206                 .desc("Alternate path for the global settings file")
207                 .hasArg()
208                 .build());
209         options.addOption(Option.builder(Character.toString(ALTERNATE_USER_TOOLCHAINS))
210                 .longOpt("toolchains")
211                 .desc("Alternate path for the user toolchains file")
212                 .hasArg()
213                 .build());
214         options.addOption(Option.builder(ALTERNATE_GLOBAL_TOOLCHAINS)
215                 .longOpt("global-toolchains")
216                 .desc("Alternate path for the global toolchains file")
217                 .hasArg()
218                 .build());
219         options.addOption(Option.builder(FAIL_FAST)
220                 .longOpt("fail-fast")
221                 .desc("Stop at first failure in reactorized builds")
222                 .build());
223         options.addOption(Option.builder(FAIL_AT_END)
224                 .longOpt("fail-at-end")
225                 .desc("Only fail the build afterwards; allow all non-impacted builds to continue")
226                 .build());
227         options.addOption(Option.builder(FAIL_NEVER)
228                 .longOpt("fail-never")
229                 .desc("NEVER fail the build, regardless of project result")
230                 .build());
231         options.addOption(Option.builder(RESUME_FROM)
232                 .longOpt("resume-from")
233                 .hasArg()
234                 .desc("Resume reactor from specified project")
235                 .build());
236         options.addOption(Option.builder(PROJECT_LIST)
237                 .longOpt("projects")
238                 .desc(
239                         "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")
240                 .hasArg()
241                 .build());
242         options.addOption(Option.builder(ALSO_MAKE)
243                 .longOpt("also-make")
244                 .desc("If project list is specified, also build projects required by the list")
245                 .build());
246         options.addOption(Option.builder(ALSO_MAKE_DEPENDENTS)
247                 .longOpt("also-make-dependents")
248                 .desc("If project list is specified, also build projects that depend on projects on the list")
249                 .build());
250         options.addOption(Option.builder(LOG_FILE)
251                 .longOpt("log-file")
252                 .hasArg()
253                 .desc("Log file where all build output will go (disables output color)")
254                 .build());
255         options.addOption(Option.builder(Character.toString(SHOW_VERSION))
256                 .longOpt("show-version")
257                 .desc("Display version information WITHOUT stopping build")
258                 .build());
259         options.addOption(Option.builder(ENCRYPT_MASTER_PASSWORD)
260                 .longOpt("encrypt-master-password")
261                 .hasArg()
262                 .optionalArg(true)
263                 .desc("Encrypt master security password")
264                 .build());
265         options.addOption(Option.builder(ENCRYPT_PASSWORD)
266                 .longOpt("encrypt-password")
267                 .hasArg()
268                 .optionalArg(true)
269                 .desc("Encrypt server password")
270                 .build());
271         options.addOption(Option.builder(THREADS)
272                 .longOpt("threads")
273                 .hasArg()
274                 .desc("Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied")
275                 .build());
276         options.addOption(Option.builder(BUILDER)
277                 .longOpt("builder")
278                 .hasArg()
279                 .desc("The id of the build strategy to use")
280                 .build());
281         options.addOption(Option.builder(NO_TRANSFER_PROGRESS)
282                 .longOpt("no-transfer-progress")
283                 .desc("Do not display transfer progress when downloading or uploading")
284                 .build());
285         options.addOption(Option.builder(IGNORE_TRANSITIVE_REPOSITORIES)
286                 .longOpt("ignore-transitive-repositories")
287                 .desc("If set, Maven will ignore remote repositories introduced by transitive dependencies.")
288                 .build());
289         options.addOption(Option.builder()
290                 .longOpt(ARTIFACTS_UPDATE_POLICY)
291                 .hasArg(true)
292                 .desc(
293                         "The update policy to apply onto artifacts ('always', 'daily', 'never'); The -UA option overrides this option with 'always'")
294                 .build());
295         options.addOption(Option.builder()
296                 .longOpt(METADATA_UPDATE_POLICY)
297                 .hasArg(true)
298                 .desc(
299                         "The update policy to apply onto metadata ('always', 'daily', 'never'); The -UM option overrides this option with 'always'")
300                 .build());
301 
302         // Adding this back in for compatibility with the verifier that hard codes this option.
303         options.addOption(Option.builder("npr")
304                 .longOpt("no-plugin-registry")
305                 .desc("Ineffective, only kept for backward compatibility")
306                 .build());
307         options.addOption(Option.builder("cpu")
308                 .longOpt("check-plugin-updates")
309                 .desc("Ineffective, only kept for backward compatibility")
310                 .build());
311         options.addOption(Option.builder("up")
312                 .longOpt("update-plugins")
313                 .desc("Ineffective, only kept for backward compatibility")
314                 .build());
315         options.addOption(Option.builder("npu")
316                 .longOpt("no-plugin-updates")
317                 .desc("Ineffective, only kept for backward compatibility")
318                 .build());
319 
320         // Adding this back to make Maven fail if used
321         options.addOption(Option.builder("llr")
322                 .longOpt("legacy-local-repository")
323                 .desc("UNSUPPORTED: Use of this option will make Maven invocation fail.")
324                 .build());
325 
326         // Adding this to make Maven3 silently ignore these otherwise Maven4 options
327         options.addOption(Option.builder()
328                 .longOpt(RAW_STREAMS)
329                 .desc("Ignored (Maven4 option)")
330                 .build());
331 
332         options.addOption(Option.builder()
333                 .longOpt(COLOR)
334                 .hasArg()
335                 .optionalArg(true)
336                 .desc("Defines the color mode of the output. Supported are 'auto', 'always', 'never'.")
337                 .build());
338     }
339 
340     public CommandLine parse(String[] args) throws ParseException {
341         // We need to eat any quotes surrounding arguments...
342         String[] cleanArgs = CleanArgument.cleanArgs(args);
343 
344         CommandLineParser parser = new GnuParser();
345 
346         return parser.parse(options, cleanArgs);
347     }
348 
349     public void displayHelp(PrintStream stdout) {
350         stdout.println();
351 
352         PrintWriter pw = new PrintWriter(stdout);
353 
354         HelpFormatter formatter = new HelpFormatter();
355 
356         formatter.printHelp(
357                 pw,
358                 HelpFormatter.DEFAULT_WIDTH,
359                 "mvn [options] [<goal(s)>] [<phase(s)>]",
360                 "\nOptions:",
361                 options,
362                 HelpFormatter.DEFAULT_LEFT_PAD,
363                 HelpFormatter.DEFAULT_DESC_PAD,
364                 "\n",
365                 false);
366 
367         pw.flush();
368     }
369 }