View Javadoc
1   package org.apache.maven.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.PrintStream;
23  import java.io.PrintWriter;
24  
25  import org.apache.commons.cli.CommandLine;
26  import org.apache.commons.cli.CommandLineParser;
27  import org.apache.commons.cli.DefaultParser;
28  import org.apache.commons.cli.HelpFormatter;
29  import org.apache.commons.cli.Option;
30  import org.apache.commons.cli.Options;
31  import org.apache.commons.cli.ParseException;
32  import org.apache.maven.shared.utils.logging.MessageUtils;
33  
34  /**
35   * @author Jason van Zyl
36   */
37  public class CLIManager
38  {
39      public static final char ALTERNATE_POM_FILE = 'f';
40  
41      public static final char BATCH_MODE = 'B';
42  
43      public static final char SET_SYSTEM_PROPERTY = 'D';
44  
45      public static final char OFFLINE = 'o';
46  
47      public static final char QUIET = 'q';
48  
49      public static final char VERBOSE = 'X';
50  
51      public static final char ERRORS = 'e';
52  
53      public static final char HELP = 'h';
54  
55      public static final char VERSION = 'v';
56  
57      public static final char SHOW_VERSION = 'V';
58  
59      public static final char NON_RECURSIVE = 'N';
60  
61      public static final char UPDATE_SNAPSHOTS = 'U';
62  
63      public static final char ACTIVATE_PROFILES = 'P';
64  
65      public static final String SUPPRESS_SNAPSHOT_UPDATES = "nsu";
66  
67      public static final char CHECKSUM_FAILURE_POLICY = 'C';
68  
69      public static final char CHECKSUM_WARNING_POLICY = 'c';
70  
71      public static final char ALTERNATE_USER_SETTINGS = 's';
72  
73      public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
74  
75      public static final char ALTERNATE_USER_TOOLCHAINS = 't';
76  
77      public static final String ALTERNATE_GLOBAL_TOOLCHAINS = "gt";
78  
79      public static final String FAIL_FAST = "ff";
80  
81      public static final String FAIL_ON_SEVERITY = "fos";
82  
83      public static final String FAIL_AT_END = "fae";
84  
85      public static final String FAIL_NEVER = "fn";
86  
87      public static final String RESUME = "r";
88  
89      public static final String RESUME_FROM = "rf";
90  
91      public static final String PROJECT_LIST = "pl";
92  
93      public static final String ALSO_MAKE = "am";
94  
95      public static final String ALSO_MAKE_DEPENDENTS = "amd";
96  
97      public static final String LOG_FILE = "l";
98  
99      public static final String ENCRYPT_MASTER_PASSWORD = "emp";
100 
101     public static final String ENCRYPT_PASSWORD = "ep";
102 
103     public static final String THREADS = "T";
104 
105     public static final String LEGACY_LOCAL_REPOSITORY = "llr";
106 
107     public static final String BUILDER = "b";
108 
109     public static final String NO_TRANSFER_PROGRESS = "ntp";
110 
111     public static final String COLOR = "color";
112 
113     /** This option is deprecated and may be repurposed as Java debug in a future version.
114      * Use {@code -X/--verbose} instead. */
115     @Deprecated
116     public static final String DEBUG = "debug";
117 
118     protected Options options;
119 
120     @SuppressWarnings( "checkstyle:linelength" )
121     public CLIManager()
122     {
123         options = new Options();
124         options.addOption( Option.builder( Character.toString( HELP ) ).longOpt( "help" ).desc( "Display help information" ).build() );
125         options.addOption( Option.builder( Character.toString( ALTERNATE_POM_FILE ) ).longOpt( "file" ).hasArg().desc( "Force the use of an alternate POM file (or directory with pom.xml)" ).build() );
126         options.addOption( Option.builder( Character.toString( SET_SYSTEM_PROPERTY ) ).numberOfArgs( 2 ).valueSeparator( '=' ).desc( "Define a system property" ).build() );
127         options.addOption( Option.builder( Character.toString( OFFLINE ) ).longOpt( "offline" ).desc( "Work offline" ).build() );
128         options.addOption( Option.builder( Character.toString( VERSION ) ).longOpt( "version" ).desc( "Display version information" ).build() );
129         options.addOption( Option.builder( Character.toString( QUIET ) ).longOpt( "quiet" ).desc( "Quiet output - only show errors" ).build() );
130         options.addOption( Option.builder( Character.toString( VERBOSE ) ).longOpt( "verbose" ).desc( "Produce execution verbose output" ).build() );
131         options.addOption( Option.builder( Character.toString( ERRORS ) ).longOpt( "errors" ).desc( "Produce execution error messages" ).build() );
132         options.addOption( Option.builder( Character.toString( NON_RECURSIVE ) ).longOpt( "non-recursive" ).desc( "Do not recurse into sub-projects. When used together with -pl, do not recurse into sub-projects of selected aggregators" ).build() );
133         options.addOption( Option.builder( Character.toString( UPDATE_SNAPSHOTS ) ).longOpt( "update-snapshots" ).desc( "Forces a check for missing releases and updated snapshots on remote repositories" ).build() );
134         options.addOption( Option.builder( Character.toString( ACTIVATE_PROFILES ) ).longOpt( "activate-profiles" ).desc( "Comma-delimited list of profiles to activate. Prefixing a profile with ! excludes it, and ? marks it as optional" ).hasArg().build() );
135         options.addOption( Option.builder( Character.toString( BATCH_MODE ) ).longOpt( "batch-mode" ).desc( "Run in non-interactive (batch) mode (disables output color)" ).build() );
136         options.addOption( Option.builder( SUPPRESS_SNAPSHOT_UPDATES ).longOpt( "no-snapshot-updates" ).desc( "Suppress SNAPSHOT updates" ).build() );
137         options.addOption( Option.builder( Character.toString( CHECKSUM_FAILURE_POLICY ) ).longOpt( "strict-checksums" ).desc( "Fail the build if checksums don't match" ).build() );
138         options.addOption( Option.builder( Character.toString( CHECKSUM_WARNING_POLICY ) ).longOpt( "lax-checksums" ).desc( "Warn if checksums don't match" ).build() );
139         options.addOption( Option.builder( Character.toString( ALTERNATE_USER_SETTINGS ) ).longOpt( "settings" ).desc( "Alternate path for the user settings file" ).hasArg().build() );
140         options.addOption( Option.builder( ALTERNATE_GLOBAL_SETTINGS ).longOpt( "global-settings" ).desc( "Alternate path for the global settings file" ).hasArg().build() );
141         options.addOption( Option.builder( Character.toString( ALTERNATE_USER_TOOLCHAINS ) ).longOpt( "toolchains" ).desc( "Alternate path for the user toolchains file" ).hasArg().build() );
142         options.addOption( Option.builder( ALTERNATE_GLOBAL_TOOLCHAINS ).longOpt( "global-toolchains" ).desc( "Alternate path for the global toolchains file" ).hasArg().build() );
143         options.addOption( Option.builder( FAIL_ON_SEVERITY ).longOpt( "fail-on-severity" ).desc( "Configure which severity of logging should cause the build to fail" ).hasArg().build() );
144         options.addOption( Option.builder( FAIL_FAST ).longOpt( "fail-fast" ).desc( "Stop at first failure in reactorized builds" ).build() );
145         options.addOption( Option.builder( FAIL_AT_END ).longOpt( "fail-at-end" ).desc( "Only fail the build afterwards; allow all non-impacted builds to continue" ).build() );
146         options.addOption( Option.builder( FAIL_NEVER ).longOpt( "fail-never" ).desc( "NEVER fail the build, regardless of project result" ).build() );
147         options.addOption( Option.builder( RESUME ).longOpt( "resume" ).desc( "Resume reactor from the last failed project, using the resume.properties file in the build directory" ).build() );
148         options.addOption( Option.builder( RESUME_FROM ).longOpt( "resume-from" ).hasArg().desc( "Resume reactor from specified project" ).build() );
149         options.addOption( Option.builder( PROJECT_LIST ).longOpt( "projects" ).desc( "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" ).hasArg().build() );
150         options.addOption( Option.builder( ALSO_MAKE ).longOpt( "also-make" ).desc( "If project list is specified, also build projects required by the list" ).build() );
151         options.addOption( Option.builder( ALSO_MAKE_DEPENDENTS ).longOpt( "also-make-dependents" ).desc( "If project list is specified, also build projects that depend on projects on the list" ).build() );
152         options.addOption( Option.builder( LOG_FILE ).longOpt( "log-file" ).hasArg().desc( "Log file where all build output will go (disables output color)" ).build() );
153         options.addOption( Option.builder( Character.toString( SHOW_VERSION ) ).longOpt( "show-version" ).desc( "Display version information WITHOUT stopping build" ).build() );
154         options.addOption( Option.builder( ENCRYPT_MASTER_PASSWORD ).longOpt( "encrypt-master-password" ).hasArg().optionalArg( true ).desc( "Encrypt master security password" ).build() );
155         options.addOption( Option.builder( ENCRYPT_PASSWORD ).longOpt( "encrypt-password" ).hasArg().optionalArg( true ).desc( "Encrypt server password" ).build() );
156         options.addOption( Option.builder( THREADS ).longOpt( "threads" ).hasArg().desc( "Thread count, for instance 4 (int) or 2C/2.5C (int/float) where C is core multiplied" ).build() );
157         options.addOption( Option.builder( LEGACY_LOCAL_REPOSITORY ).longOpt( "legacy-local-repository" ).desc( "Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be activated by using -Dmaven.legacyLocalRepo=true" ).build() );
158         options.addOption( Option.builder( BUILDER ).longOpt( "builder" ).hasArg().desc( "The id of the build strategy to use" ).build() );
159         options.addOption( Option.builder( NO_TRANSFER_PROGRESS ).longOpt( "no-transfer-progress" ).desc( "Do not display transfer progress when downloading or uploading" ).build() );
160         options.addOption( Option.builder().longOpt( COLOR ).hasArg().optionalArg( true ).desc( "Defines the color mode of the output. Supported are 'auto', 'always', 'never'." ).build() );
161 
162         // Deprecated
163         options.addOption( Option.builder().longOpt( DEBUG ).desc( "Produce execution verbose output (deprecated; only kept for backward compatibility)" ).build() );
164     }
165 
166     public CommandLine parse( String[] args )
167         throws ParseException
168     {
169         // We need to eat any quotes surrounding arguments...
170         String[] cleanArgs = CleanArgument.cleanArgs( args );
171 
172         CommandLineParser parser = new DefaultParser();
173 
174         return parser.parse( options, cleanArgs );
175     }
176 
177     public void displayHelp( PrintStream stdout )
178     {
179         stdout.println();
180 
181         PrintWriter pw = new PrintWriter( stdout );
182 
183         HelpFormatter formatter = new HelpFormatter();
184 
185         int width = MessageUtils.getTerminalWidth();
186         if ( width <= 0 )
187         {
188             width = HelpFormatter.DEFAULT_WIDTH;
189         }
190 
191         formatter.printHelp( pw, width, "mvn [args]",
192                              System.lineSeparator() + "Options:", options,
193                              HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD,
194                              System.lineSeparator(), false );
195 
196         pw.flush();
197     }
198 }