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