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