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  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.cli.CommandLine;
28  import org.apache.commons.cli.CommandLineParser;
29  import org.apache.commons.cli.GnuParser;
30  import org.apache.commons.cli.HelpFormatter;
31  import org.apache.commons.cli.OptionBuilder;
32  import org.apache.commons.cli.Options;
33  import org.apache.commons.cli.ParseException;
34  
35  /**
36   * @author Jason van Zyl
37   * @version $Revision: 958295 $
38   */
39  public class CLIManager
40  {
41      public static final char ALTERNATE_POM_FILE = 'f';
42  
43      public static final char BATCH_MODE = 'B';
44  
45      public static final char SET_SYSTEM_PROPERTY = 'D';
46  
47      public static final char OFFLINE = 'o';
48  
49      public static final char QUIET = 'q';
50  
51      public static final char DEBUG = 'X';
52  
53      public static final char ERRORS = 'e';
54  
55      public static final char HELP = 'h';
56  
57      public static final char VERSION = 'v';
58  
59      public static final char SHOW_VERSION = 'V';
60  
61      public static final char NON_RECURSIVE = 'N';
62  
63      public static final char UPDATE_SNAPSHOTS = 'U';
64  
65      public static final char ACTIVATE_PROFILES = 'P';
66  
67      public static final String SUPRESS_SNAPSHOT_UPDATES = "nsu";
68  
69      public static final char CHECKSUM_FAILURE_POLICY = 'C';
70  
71      public static final char CHECKSUM_WARNING_POLICY = 'c';
72  
73      public static final char ALTERNATE_USER_SETTINGS = 's';
74  
75      public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
76  
77      public static final char ALTERNATE_USER_TOOLCHAINS = 't';
78  
79      public static final String FAIL_FAST = "ff";
80  
81      public static final String FAIL_AT_END = "fae";
82  
83      public static final String FAIL_NEVER = "fn";
84  
85      public static final String RESUME_FROM = "rf";
86  
87      public static final String PROJECT_LIST = "pl";
88  
89      public static final String ALSO_MAKE = "am";
90  
91      public static final String ALSO_MAKE_DEPENDENTS = "amd";
92  
93      public static final String LOG_FILE = "l";
94  
95      public static final String ENCRYPT_MASTER_PASSWORD = "emp";
96  
97      public static final String ENCRYPT_PASSWORD = "ep";
98  
99      public static final String THREADS = "T";
100 
101     private Options options;
102 
103     @SuppressWarnings( "static-access" )
104     public CLIManager()
105     {
106         options = new Options();
107         options.addOption( OptionBuilder.withLongOpt( "file" ).hasArg().withDescription( "Force the use of an alternate POM file." ).create( ALTERNATE_POM_FILE ) );
108         options.addOption( OptionBuilder.withLongOpt( "define" ).hasArg().withDescription( "Define a system property" ).create( SET_SYSTEM_PROPERTY ) );
109         options.addOption( OptionBuilder.withLongOpt( "offline" ).withDescription( "Work offline" ).create( OFFLINE ) );
110         options.addOption( OptionBuilder.withLongOpt( "help" ).withDescription( "Display help information" ).create( HELP ) );
111         options.addOption( OptionBuilder.withLongOpt( "version" ).withDescription( "Display version information" ).create( VERSION ) );
112         options.addOption( OptionBuilder.withLongOpt( "quiet" ).withDescription( "Quiet output - only show errors" ).create( QUIET ) );
113         options.addOption( OptionBuilder.withLongOpt( "debug" ).withDescription( "Produce execution debug output" ).create( DEBUG ) );
114         options.addOption( OptionBuilder.withLongOpt( "errors" ).withDescription( "Produce execution error messages" ).create( ERRORS ) );
115         options.addOption( OptionBuilder.withLongOpt( "non-recursive" ).withDescription( "Do not recurse into sub-projects" ).create( NON_RECURSIVE ) );
116         options.addOption( OptionBuilder.withLongOpt( "update-snapshots" ).withDescription( "Forces a check for updated releases and snapshots on remote repositories" ).create( UPDATE_SNAPSHOTS ) );
117         options.addOption( OptionBuilder.withLongOpt( "activate-profiles" ).withDescription( "Comma-delimited list of profiles to activate" ).hasArg().create( ACTIVATE_PROFILES ) );
118         options.addOption( OptionBuilder.withLongOpt( "batch-mode" ).withDescription( "Run in non-interactive (batch) mode" ).create( BATCH_MODE ) );
119         options.addOption( OptionBuilder.withLongOpt( "no-snapshot-updates" ).withDescription( "Supress SNAPSHOT updates" ).create( SUPRESS_SNAPSHOT_UPDATES ) );
120         options.addOption( OptionBuilder.withLongOpt( "strict-checksums" ).withDescription( "Fail the build if checksums don't match" ).create( CHECKSUM_FAILURE_POLICY ) );
121         options.addOption( OptionBuilder.withLongOpt( "lax-checksums" ).withDescription( "Warn if checksums don't match" ).create( CHECKSUM_WARNING_POLICY ) );
122         options.addOption( OptionBuilder.withLongOpt( "settings" ).withDescription( "Alternate path for the user settings file" ).hasArg().create( ALTERNATE_USER_SETTINGS ) );
123         options.addOption( OptionBuilder.withLongOpt( "global-settings" ).withDescription( "Alternate path for the global settings file" ).hasArg().create( ALTERNATE_GLOBAL_SETTINGS ) );
124         options.addOption( OptionBuilder.withLongOpt( "toolchains" ).withDescription( "Alternate path for the user toolchains file" ).hasArg().create( ALTERNATE_USER_TOOLCHAINS ) );
125         options.addOption( OptionBuilder.withLongOpt( "fail-fast" ).withDescription( "Stop at first failure in reactorized builds" ).create( FAIL_FAST ) );
126         options.addOption( OptionBuilder.withLongOpt( "fail-at-end" ).withDescription( "Only fail the build afterwards; allow all non-impacted builds to continue" ).create( FAIL_AT_END ) );
127         options.addOption( OptionBuilder.withLongOpt( "fail-never" ).withDescription( "NEVER fail the build, regardless of project result" ).create( FAIL_NEVER ) );
128         options.addOption( OptionBuilder.withLongOpt( "resume-from" ).hasArg().withDescription( "Resume reactor from specified project" ).create( RESUME_FROM ) );
129         options.addOption( OptionBuilder.withLongOpt( "projects" ).withDescription( "Build specified reactor projects instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path." ).hasArg().create( PROJECT_LIST ) );
130         options.addOption( OptionBuilder.withLongOpt( "also-make" ).withDescription( "If project list is specified, also build projects required by the list" ).create( ALSO_MAKE ) );
131         options.addOption( OptionBuilder.withLongOpt( "also-make-dependents" ).withDescription( "If project list is specified, also build projects that depend on projects on the list" ).create( ALSO_MAKE_DEPENDENTS ) );
132         options.addOption( OptionBuilder.withLongOpt( "log-file" ).hasArg().withDescription( "Log file to where all build output will go." ).create( LOG_FILE ) );
133         options.addOption( OptionBuilder.withLongOpt( "show-version" ).withDescription( "Display version information WITHOUT stopping build" ).create( SHOW_VERSION ) );
134         options.addOption( OptionBuilder.withLongOpt( "encrypt-master-password" ).hasArg().withDescription( "Encrypt master security password" ).create( ENCRYPT_MASTER_PASSWORD ) );
135         options.addOption( OptionBuilder.withLongOpt( "encrypt-password" ).hasArg().withDescription( "Encrypt server password" ).create( ENCRYPT_PASSWORD ) );
136         options.addOption( OptionBuilder.withLongOpt( "threads" ).hasArg().withDescription( "Thread count, for instance 2.0C where C is core multiplied" ).create( THREADS ) );
137 
138         // Adding this back in for compatibility with the verifier that hard codes this option.
139 
140         options.addOption( OptionBuilder.withLongOpt( "no-plugin-registry" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npr" ) );
141         options.addOption( OptionBuilder.withLongOpt( "check-plugin-updates" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "cpu" ) );
142         options.addOption( OptionBuilder.withLongOpt( "update-plugins" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "up" ) );
143         options.addOption( OptionBuilder.withLongOpt( "no-plugin-updates" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npu" ) );
144     }
145 
146     public CommandLine parse( String[] args )
147         throws ParseException
148     {
149         // We need to eat any quotes surrounding arguments...
150         String[] cleanArgs = cleanArgs( args );
151 
152         CommandLineParser parser = new GnuParser();
153 
154         return parser.parse( options, cleanArgs );
155     }
156 
157     private String[] cleanArgs( String[] args )
158     {
159         List<String> cleaned = new ArrayList<String>();
160 
161         StringBuilder currentArg = null;
162 
163         for ( int i = 0; i < args.length; i++ )
164         {
165             String arg = args[i];
166 
167             boolean addedToBuffer = false;
168 
169             if ( arg.startsWith( "\"" ) )
170             {
171                 // if we're in the process of building up another arg, push it and start over.
172                 // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
173                 if ( currentArg != null )
174                 {
175                     cleaned.add( currentArg.toString() );
176                 }
177 
178                 // start building an argument here.
179                 currentArg = new StringBuilder( arg.substring( 1 ) );
180                 addedToBuffer = true;
181             }
182 
183             // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
184             if ( arg.endsWith( "\"" ) )
185             {
186                 String cleanArgPart = arg.substring( 0, arg.length() - 1 );
187 
188                 // if we're building an argument, keep doing so.
189                 if ( currentArg != null )
190                 {
191                     // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
192                     if ( addedToBuffer )
193                     {
194                         currentArg.setLength( currentArg.length() - 1 );
195                     }
196                     // otherwise, we trim the trailing " and append to the buffer.
197                     else
198                     {
199                         // TODO: introducing a space here...not sure what else to do but collapse whitespace
200                         currentArg.append( ' ' ).append( cleanArgPart );
201                     }
202 
203                     cleaned.add( currentArg.toString() );
204                 }
205                 else
206                 {
207                     cleaned.add( cleanArgPart );
208                 }
209 
210                 currentArg = null;
211 
212                 continue;
213             }
214 
215             // if we haven't added this arg to the buffer, and we ARE building an argument
216             // buffer, then append it with a preceding space...again, not sure what else to
217             // do other than collapse whitespace.
218             // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
219             if ( !addedToBuffer )
220             {
221                 if ( currentArg != null )
222                 {
223                     currentArg.append( ' ' ).append( arg );
224                 }
225                 else
226                 {
227                     cleaned.add( arg );
228                 }
229             }
230         }
231 
232         if ( currentArg != null )
233         {
234             cleaned.add( currentArg.toString() );
235         }
236 
237         int cleanedSz = cleaned.size();
238 
239         String[] cleanArgs = null;
240 
241         if ( cleanedSz == 0 )
242         {
243             cleanArgs = args;
244         }
245         else
246         {
247             cleanArgs = cleaned.toArray( new String[cleanedSz] );
248         }
249 
250         return cleanArgs;
251     }
252 
253     public void displayHelp( PrintStream stdout )
254     {
255         stdout.println();
256 
257         PrintWriter pw = new PrintWriter( stdout );
258 
259         HelpFormatter formatter = new HelpFormatter();
260 
261         formatter.printHelp( pw, HelpFormatter.DEFAULT_WIDTH, "mvn [options] [<goal(s)>] [<phase(s)>]", "\nOptions:",
262                              options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, "\n", false );
263 
264         pw.flush();
265     }
266 
267 }