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