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