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