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.DefaultParser;
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  import org.apache.maven.jline.MessageUtils;
32  
33  /**
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 String NON_INTERACTIVE = "non-interactive";
41  
42      public static final String FORCE_INTERACTIVE = "force-interactive";
43  
44      public static final char SET_USER_PROPERTY = 'D';
45  
46      /**
47       * @deprecated Use {@link #SET_USER_PROPERTY}
48       */
49      @Deprecated
50      public static final char SET_SYSTEM_PROPERTY = SET_USER_PROPERTY;
51  
52      public static final char OFFLINE = 'o';
53  
54      public static final char QUIET = 'q';
55  
56      public static final char VERBOSE = 'X';
57  
58      public static final char ERRORS = 'e';
59  
60      public static final char HELP = 'h';
61  
62      public static final char VERSION = 'v';
63  
64      public static final char SHOW_VERSION = 'V';
65  
66      public static final char NON_RECURSIVE = 'N';
67  
68      public static final char UPDATE_SNAPSHOTS = 'U';
69  
70      public static final char ACTIVATE_PROFILES = 'P';
71  
72      public static final String SUPPRESS_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_PROJECT_SETTINGS = "ps";
81  
82      @Deprecated
83      public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
84  
85      public static final String ALTERNATE_INSTALLATION_SETTINGS = "is";
86  
87      public static final char ALTERNATE_USER_TOOLCHAINS = 't';
88  
89      @Deprecated
90      public static final String ALTERNATE_GLOBAL_TOOLCHAINS = "gt";
91  
92      public static final String ALTERNATE_INSTALLATION_TOOLCHAINS = "it";
93  
94      public static final String FAIL_FAST = "ff";
95  
96      public static final String FAIL_ON_SEVERITY = "fos";
97  
98      public static final String FAIL_AT_END = "fae";
99  
100     public static final String FAIL_NEVER = "fn";
101 
102     public static final String RESUME = "r";
103 
104     public static final String RESUME_FROM = "rf";
105 
106     public static final String PROJECT_LIST = "pl";
107 
108     public static final String ALSO_MAKE = "am";
109 
110     public static final String ALSO_MAKE_DEPENDENTS = "amd";
111 
112     public static final String LOG_FILE = "l";
113 
114     public static final String ENCRYPT_MASTER_PASSWORD = "emp";
115 
116     public static final String ENCRYPT_PASSWORD = "ep";
117 
118     public static final String THREADS = "T";
119 
120     public static final String BUILDER = "b";
121 
122     public static final String NO_TRANSFER_PROGRESS = "ntp";
123 
124     public static final String COLOR = "color";
125 
126     public static final String CACHE_ARTIFACT_NOT_FOUND = "canf";
127 
128     public static final String STRICT_ARTIFACT_DESCRIPTOR_POLICY = "sadp";
129 
130     public static final String IGNORE_TRANSITIVE_REPOSITORIES = "itr";
131 
132     /** This option is deprecated and may be repurposed as Java debug in a future version.
133      * Use {@code -X/--verbose} instead. */
134     @Deprecated
135     public static final String DEBUG = "debug";
136 
137     protected Options options;
138 
139     @SuppressWarnings("checkstyle:MethodLength")
140     public CLIManager() {
141         options = new Options();
142         options.addOption(Option.builder(Character.toString(HELP))
143                 .longOpt("help")
144                 .desc("Display help information")
145                 .build());
146         options.addOption(Option.builder(Character.toString(ALTERNATE_POM_FILE))
147                 .longOpt("file")
148                 .hasArg()
149                 .desc("Force the use of an alternate POM file (or directory with pom.xml)")
150                 .build());
151         options.addOption(Option.builder(Character.toString(SET_USER_PROPERTY))
152                 .numberOfArgs(2)
153                 .valueSeparator('=')
154                 .desc("Define a user property")
155                 .build());
156         options.addOption(Option.builder(Character.toString(OFFLINE))
157                 .longOpt("offline")
158                 .desc("Work offline")
159                 .build());
160         options.addOption(Option.builder(Character.toString(VERSION))
161                 .longOpt("version")
162                 .desc("Display version information")
163                 .build());
164         options.addOption(Option.builder(Character.toString(QUIET))
165                 .longOpt("quiet")
166                 .desc("Quiet output - only show errors")
167                 .build());
168         options.addOption(Option.builder(Character.toString(VERBOSE))
169                 .longOpt("verbose")
170                 .desc("Produce execution verbose output")
171                 .build());
172         options.addOption(Option.builder(Character.toString(ERRORS))
173                 .longOpt("errors")
174                 .desc("Produce execution error messages")
175                 .build());
176         options.addOption(Option.builder(Character.toString(NON_RECURSIVE))
177                 .longOpt("non-recursive")
178                 .desc(
179                         "Do not recurse into sub-projects. When used together with -pl, do not recurse into sub-projects of selected aggregators")
180                 .build());
181         options.addOption(Option.builder(Character.toString(UPDATE_SNAPSHOTS))
182                 .longOpt("update-snapshots")
183                 .desc("Forces a check for missing releases and updated snapshots on remote repositories")
184                 .build());
185         options.addOption(Option.builder(Character.toString(ACTIVATE_PROFILES))
186                 .longOpt("activate-profiles")
187                 .desc(
188                         "Comma-delimited list of profiles to activate. Prefixing a profile with ! excludes it, and ? marks it as optional")
189                 .hasArg()
190                 .build());
191         options.addOption(Option.builder(Character.toString(BATCH_MODE))
192                 .longOpt("batch-mode")
193                 .desc("Run in non-interactive mode. Alias for --non-interactive (kept for backwards compatability)")
194                 .build());
195         options.addOption(Option.builder()
196                 .longOpt(NON_INTERACTIVE)
197                 .desc("Run in non-interactive mode. Alias for --batch-mode")
198                 .build());
199         options.addOption(Option.builder()
200                 .longOpt(FORCE_INTERACTIVE)
201                 .desc(
202                         "Run in interactive mode. Overrides, if applicable, the CI environment variable and --non-interactive/--batch-mode options")
203                 .build());
204         options.addOption(Option.builder(SUPPRESS_SNAPSHOT_UPDATES)
205                 .longOpt("no-snapshot-updates")
206                 .desc("Suppress SNAPSHOT updates")
207                 .build());
208         options.addOption(Option.builder(Character.toString(CHECKSUM_FAILURE_POLICY))
209                 .longOpt("strict-checksums")
210                 .desc("Fail the build if checksums don't match")
211                 .build());
212         options.addOption(Option.builder(Character.toString(CHECKSUM_WARNING_POLICY))
213                 .longOpt("lax-checksums")
214                 .desc("Warn if checksums don't match")
215                 .build());
216         options.addOption(Option.builder(Character.toString(ALTERNATE_USER_SETTINGS))
217                 .longOpt("settings")
218                 .desc("Alternate path for the user settings file")
219                 .hasArg()
220                 .build());
221         options.addOption(Option.builder(ALTERNATE_PROJECT_SETTINGS)
222                 .longOpt("project-settings")
223                 .desc("Alternate path for the project settings file")
224                 .hasArg()
225                 .build());
226         options.addOption(Option.builder(ALTERNATE_GLOBAL_SETTINGS)
227                 .longOpt("global-settings")
228                 .desc("Alternate path for the global settings file")
229                 .hasArg()
230                 .deprecated()
231                 .build());
232         options.addOption(Option.builder(ALTERNATE_INSTALLATION_SETTINGS)
233                 .longOpt("install-settings")
234                 .desc("Alternate path for the installation settings file")
235                 .hasArg()
236                 .build());
237         options.addOption(Option.builder(Character.toString(ALTERNATE_USER_TOOLCHAINS))
238                 .longOpt("toolchains")
239                 .desc("Alternate path for the user toolchains file")
240                 .hasArg()
241                 .build());
242         options.addOption(Option.builder(ALTERNATE_GLOBAL_TOOLCHAINS)
243                 .longOpt("global-toolchains")
244                 .desc("Alternate path for the global toolchains file")
245                 .hasArg()
246                 .deprecated()
247                 .build());
248         options.addOption(Option.builder(ALTERNATE_INSTALLATION_TOOLCHAINS)
249                 .longOpt("install-toolchains")
250                 .desc("Alternate path for the installation toolchains file")
251                 .hasArg()
252                 .build());
253         options.addOption(Option.builder(FAIL_ON_SEVERITY)
254                 .longOpt("fail-on-severity")
255                 .desc("Configure which severity of logging should cause the build to fail")
256                 .hasArg()
257                 .build());
258         options.addOption(Option.builder(FAIL_FAST)
259                 .longOpt("fail-fast")
260                 .desc("Stop at first failure in reactorized builds")
261                 .build());
262         options.addOption(Option.builder(FAIL_AT_END)
263                 .longOpt("fail-at-end")
264                 .desc("Only fail the build afterwards; allow all non-impacted builds to continue")
265                 .build());
266         options.addOption(Option.builder(FAIL_NEVER)
267                 .longOpt("fail-never")
268                 .desc("NEVER fail the build, regardless of project result")
269                 .build());
270         options.addOption(Option.builder(RESUME)
271                 .longOpt("resume")
272                 .desc(
273                         "Resume reactor from the last failed project, using the resume.properties file in the build directory")
274                 .build());
275         options.addOption(Option.builder(RESUME_FROM)
276                 .longOpt("resume-from")
277                 .hasArg()
278                 .desc("Resume reactor from specified project")
279                 .build());
280         options.addOption(Option.builder(PROJECT_LIST)
281                 .longOpt("projects")
282                 .desc(
283                         "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. Prefixing a project with ! excludes it, and ? marks it as optional")
284                 .hasArg()
285                 .build());
286         options.addOption(Option.builder(ALSO_MAKE)
287                 .longOpt("also-make")
288                 .desc("If project list is specified, also build projects required by the list")
289                 .build());
290         options.addOption(Option.builder(ALSO_MAKE_DEPENDENTS)
291                 .longOpt("also-make-dependents")
292                 .desc("If project list is specified, also build projects that depend on projects on the list")
293                 .build());
294         options.addOption(Option.builder(LOG_FILE)
295                 .longOpt("log-file")
296                 .hasArg()
297                 .desc("Log file where all build output will go (disables output color)")
298                 .build());
299         options.addOption(Option.builder(Character.toString(SHOW_VERSION))
300                 .longOpt("show-version")
301                 .desc("Display version information WITHOUT stopping build")
302                 .build());
303         options.addOption(Option.builder(ENCRYPT_MASTER_PASSWORD)
304                 .longOpt("encrypt-master-password")
305                 .hasArg()
306                 .optionalArg(true)
307                 .desc("Encrypt master security password")
308                 .build());
309         options.addOption(Option.builder(ENCRYPT_PASSWORD)
310                 .longOpt("encrypt-password")
311                 .hasArg()
312                 .optionalArg(true)
313                 .desc("Encrypt server password")
314                 .build());
315         options.addOption(Option.builder(THREADS)
316                 .longOpt("threads")
317                 .hasArg()
318                 .desc("Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied")
319                 .build());
320         options.addOption(Option.builder(BUILDER)
321                 .longOpt("builder")
322                 .hasArg()
323                 .desc("The id of the build strategy to use")
324                 .build());
325         options.addOption(Option.builder(NO_TRANSFER_PROGRESS)
326                 .longOpt("no-transfer-progress")
327                 .desc("Do not display transfer progress when downloading or uploading")
328                 .build());
329         options.addOption(Option.builder()
330                 .longOpt(COLOR)
331                 .hasArg()
332                 .optionalArg(true)
333                 .desc("Defines the color mode of the output. Supported are 'auto', 'always', 'never'.")
334                 .build());
335         options.addOption(Option.builder(CACHE_ARTIFACT_NOT_FOUND)
336                 .longOpt("cache-artifact-not-found")
337                 .hasArg()
338                 .desc(
339                         "Defines caching behaviour for 'not found' artifacts. Supported values are 'true' (default), 'false'.")
340                 .build());
341         options.addOption(Option.builder(STRICT_ARTIFACT_DESCRIPTOR_POLICY)
342                 .longOpt("strict-artifact-descriptor-policy")
343                 .hasArg()
344                 .desc("Defines 'strict' artifact descriptor policy. Supported values are 'true', 'false' (default).")
345                 .build());
346         options.addOption(Option.builder(IGNORE_TRANSITIVE_REPOSITORIES)
347                 .longOpt("ignore-transitive-repositories")
348                 .desc("If set, Maven will ignore remote repositories introduced by transitive dependencies.")
349                 .build());
350 
351         // Adding this back to make Maven fail if used
352         options.addOption(Option.builder("llr")
353                 .longOpt("legacy-local-repository")
354                 .desc("UNSUPPORTED: Use of this option will make Maven invocation fail.")
355                 .build());
356 
357         // Deprecated
358         options.addOption(Option.builder()
359                 .longOpt(DEBUG)
360                 .desc("Produce execution verbose output (deprecated; only kept for backward compatibility)")
361                 .build());
362     }
363 
364     public CommandLine parse(String[] args) throws ParseException {
365         // We need to eat any quotes surrounding arguments...
366         String[] cleanArgs = CleanArgument.cleanArgs(args);
367 
368         CommandLineParser parser = new DefaultParser();
369 
370         return parser.parse(options, cleanArgs);
371     }
372 
373     public void displayHelp(PrintStream stdout) {
374         stdout.println();
375 
376         PrintWriter pw = new PrintWriter(stdout);
377 
378         HelpFormatter formatter = new HelpFormatter();
379 
380         int width = MessageUtils.getTerminalWidth();
381         if (width <= 0) {
382             width = HelpFormatter.DEFAULT_WIDTH;
383         }
384 
385         formatter.printHelp(
386                 pw,
387                 width,
388                 "mvn [args]",
389                 System.lineSeparator() + "Options:",
390                 options,
391                 HelpFormatter.DEFAULT_LEFT_PAD,
392                 HelpFormatter.DEFAULT_DESC_PAD,
393                 System.lineSeparator(),
394                 false);
395 
396         pw.flush();
397     }
398 }