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