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.GnuParser;
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  
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 DEBUG = '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 SUPRESS_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_AT_END = "fae";
85  
86      public static final String FAIL_NEVER = "fn";
87  
88      public static final String RESUME_FROM = "rf";
89  
90      public static final String PROJECT_LIST = "pl";
91  
92      public static final String ALSO_MAKE = "am";
93  
94      public static final String ALSO_MAKE_DEPENDENTS = "amd";
95  
96      public static final String LOG_FILE = "l";
97  
98      public static final String ENCRYPT_MASTER_PASSWORD = "emp";
99  
100     public static final String ENCRYPT_PASSWORD = "ep";
101 
102     public static final String THREADS = "T";
103 
104     public static final String BUILDER = "b";
105 
106     public static final String NO_TRANSFER_PROGRESS = "ntp";
107 
108     public static final String COLOR = "color";
109 
110     public static final String IGNORE_TRANSITIVE_REPOSITORIES = "itr";
111 
112     protected Options options;
113 
114     @SuppressWarnings({"checkstyle:linelength", "checkstyle:MethodLength"})
115     public CLIManager() {
116         options = new Options();
117         options.addOption(Option.builder(Character.toString(HELP))
118                 .longOpt("help")
119                 .desc("Display help information")
120                 .build());
121         options.addOption(Option.builder(Character.toString(ALTERNATE_POM_FILE))
122                 .longOpt("file")
123                 .hasArg()
124                 .desc("Force the use of an alternate POM file (or directory with pom.xml)")
125                 .build());
126         options.addOption(Option.builder(Character.toString(SET_USER_PROPERTY))
127                 .longOpt("define")
128                 .hasArg()
129                 .desc("Define a user property")
130                 .build());
131         options.addOption(Option.builder(Character.toString(OFFLINE))
132                 .longOpt("offline")
133                 .desc("Work offline")
134                 .build());
135         options.addOption(Option.builder(Character.toString(VERSION))
136                 .longOpt("version")
137                 .desc("Display version information")
138                 .build());
139         options.addOption(Option.builder(Character.toString(QUIET))
140                 .longOpt("quiet")
141                 .desc("Quiet output - only show errors")
142                 .build());
143         options.addOption(Option.builder(Character.toString(DEBUG))
144                 .longOpt("debug")
145                 .desc("Produce execution debug output")
146                 .build());
147         options.addOption(Option.builder(Character.toString(ERRORS))
148                 .longOpt("errors")
149                 .desc("Produce execution error messages")
150                 .build());
151         options.addOption(Option.builder(Character.toString(NON_RECURSIVE))
152                 .longOpt("non-recursive")
153                 .desc("Do not recurse into sub-projects")
154                 .build());
155         options.addOption(Option.builder(Character.toString(UPDATE_SNAPSHOTS))
156                 .longOpt("update-snapshots")
157                 .desc("Forces a check for missing releases and updated snapshots on remote repositories")
158                 .build());
159         options.addOption(Option.builder(Character.toString(ACTIVATE_PROFILES))
160                 .longOpt("activate-profiles")
161                 .desc("Comma-delimited list of profiles to activate")
162                 .hasArg()
163                 .build());
164         options.addOption(Option.builder(Character.toString(BATCH_MODE))
165                 .longOpt("batch-mode")
166                 .desc("Run in non-interactive (batch) mode (disables output color)")
167                 .build());
168         options.addOption(Option.builder(SUPRESS_SNAPSHOT_UPDATES)
169                 .longOpt("no-snapshot-updates")
170                 .desc("Suppress SNAPSHOT updates")
171                 .build());
172         options.addOption(Option.builder(Character.toString(CHECKSUM_FAILURE_POLICY))
173                 .longOpt("strict-checksums")
174                 .desc("Fail the build if checksums don't match")
175                 .build());
176         options.addOption(Option.builder(Character.toString(CHECKSUM_WARNING_POLICY))
177                 .longOpt("lax-checksums")
178                 .desc("Warn if checksums don't match")
179                 .build());
180         options.addOption(Option.builder(Character.toString(ALTERNATE_USER_SETTINGS))
181                 .longOpt("settings")
182                 .desc("Alternate path for the user settings file")
183                 .hasArg()
184                 .build());
185         options.addOption(Option.builder(ALTERNATE_GLOBAL_SETTINGS)
186                 .longOpt("global-settings")
187                 .desc("Alternate path for the global settings file")
188                 .hasArg()
189                 .build());
190         options.addOption(Option.builder(Character.toString(ALTERNATE_USER_TOOLCHAINS))
191                 .longOpt("toolchains")
192                 .desc("Alternate path for the user toolchains file")
193                 .hasArg()
194                 .build());
195         options.addOption(Option.builder(ALTERNATE_GLOBAL_TOOLCHAINS)
196                 .longOpt("global-toolchains")
197                 .desc("Alternate path for the global toolchains file")
198                 .hasArg()
199                 .build());
200         options.addOption(Option.builder(FAIL_FAST)
201                 .longOpt("fail-fast")
202                 .desc("Stop at first failure in reactorized builds")
203                 .build());
204         options.addOption(Option.builder(FAIL_AT_END)
205                 .longOpt("fail-at-end")
206                 .desc("Only fail the build afterwards; allow all non-impacted builds to continue")
207                 .build());
208         options.addOption(Option.builder(FAIL_NEVER)
209                 .longOpt("fail-never")
210                 .desc("NEVER fail the build, regardless of project result")
211                 .build());
212         options.addOption(Option.builder(RESUME_FROM)
213                 .longOpt("resume-from")
214                 .hasArg()
215                 .desc("Resume reactor from specified project")
216                 .build());
217         options.addOption(Option.builder(PROJECT_LIST)
218                 .longOpt("projects")
219                 .desc(
220                         "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")
221                 .hasArg()
222                 .build());
223         options.addOption(Option.builder(ALSO_MAKE)
224                 .longOpt("also-make")
225                 .desc("If project list is specified, also build projects required by the list")
226                 .build());
227         options.addOption(Option.builder(ALSO_MAKE_DEPENDENTS)
228                 .longOpt("also-make-dependents")
229                 .desc("If project list is specified, also build projects that depend on projects on the list")
230                 .build());
231         options.addOption(Option.builder(LOG_FILE)
232                 .longOpt("log-file")
233                 .hasArg()
234                 .desc("Log file where all build output will go (disables output color)")
235                 .build());
236         options.addOption(Option.builder(Character.toString(SHOW_VERSION))
237                 .longOpt("show-version")
238                 .desc("Display version information WITHOUT stopping build")
239                 .build());
240         options.addOption(Option.builder(ENCRYPT_MASTER_PASSWORD)
241                 .longOpt("encrypt-master-password")
242                 .hasArg()
243                 .optionalArg(true)
244                 .desc("Encrypt master security password")
245                 .build());
246         options.addOption(Option.builder(ENCRYPT_PASSWORD)
247                 .longOpt("encrypt-password")
248                 .hasArg()
249                 .optionalArg(true)
250                 .desc("Encrypt server password")
251                 .build());
252         options.addOption(Option.builder(THREADS)
253                 .longOpt("threads")
254                 .hasArg()
255                 .desc("Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied")
256                 .build());
257         options.addOption(Option.builder(BUILDER)
258                 .longOpt("builder")
259                 .hasArg()
260                 .desc("The id of the build strategy to use")
261                 .build());
262         options.addOption(Option.builder(NO_TRANSFER_PROGRESS)
263                 .longOpt("no-transfer-progress")
264                 .desc("Do not display transfer progress when downloading or uploading")
265                 .build());
266         options.addOption(Option.builder(IGNORE_TRANSITIVE_REPOSITORIES)
267                 .longOpt("ignore-transitive-repositories")
268                 .desc("If set, Maven will ignore remote repositories introduced by transitive dependencies.")
269                 .build());
270 
271         // Adding this back in for compatibility with the verifier that hard codes this option.
272         options.addOption(Option.builder("npr")
273                 .longOpt("no-plugin-registry")
274                 .desc("Ineffective, only kept for backward compatibility")
275                 .build());
276         options.addOption(Option.builder("cpu")
277                 .longOpt("check-plugin-updates")
278                 .desc("Ineffective, only kept for backward compatibility")
279                 .build());
280         options.addOption(Option.builder("up")
281                 .longOpt("update-plugins")
282                 .desc("Ineffective, only kept for backward compatibility")
283                 .build());
284         options.addOption(Option.builder("npu")
285                 .longOpt("no-plugin-updates")
286                 .desc("Ineffective, only kept for backward compatibility")
287                 .build());
288 
289         // Adding this back to make Maven fail if used
290         options.addOption(Option.builder("llr")
291                 .longOpt("legacy-local-repository")
292                 .desc("UNSUPPORTED: Use of this option will make Maven invocation fail.")
293                 .build());
294 
295         options.addOption(Option.builder()
296                 .longOpt(COLOR)
297                 .hasArg()
298                 .optionalArg(true)
299                 .desc("Defines the color mode of the output. Supported are 'auto', 'always', 'never'.")
300                 .build());
301     }
302 
303     public CommandLine parse(String[] args) throws ParseException {
304         // We need to eat any quotes surrounding arguments...
305         String[] cleanArgs = CleanArgument.cleanArgs(args);
306 
307         CommandLineParser parser = new GnuParser();
308 
309         return parser.parse(options, cleanArgs);
310     }
311 
312     public void displayHelp(PrintStream stdout) {
313         stdout.println();
314 
315         PrintWriter pw = new PrintWriter(stdout);
316 
317         HelpFormatter formatter = new HelpFormatter();
318 
319         formatter.printHelp(
320                 pw,
321                 HelpFormatter.DEFAULT_WIDTH,
322                 "mvn [options] [<goal(s)>] [<phase(s)>]",
323                 "\nOptions:",
324                 options,
325                 HelpFormatter.DEFAULT_LEFT_PAD,
326                 HelpFormatter.DEFAULT_DESC_PAD,
327                 "\n",
328                 false);
329 
330         pw.flush();
331     }
332 }