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