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