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