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