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