001package org.apache.maven.cli;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.PrintStream;
023import java.io.PrintWriter;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.commons.cli.CommandLine;
028import org.apache.commons.cli.CommandLineParser;
029import org.apache.commons.cli.GnuParser;
030import org.apache.commons.cli.HelpFormatter;
031import org.apache.commons.cli.OptionBuilder;
032import org.apache.commons.cli.Options;
033import org.apache.commons.cli.ParseException;
034
035/**
036 * @author Jason van Zyl
037 */
038public class CLIManager
039{
040    public static final char ALTERNATE_POM_FILE = 'f';
041
042    public static final char BATCH_MODE = 'B';
043
044    public static final char SET_SYSTEM_PROPERTY = 'D';
045
046    public static final char OFFLINE = 'o';
047
048    public static final char QUIET = 'q';
049
050    public static final char DEBUG = 'X';
051
052    public static final char ERRORS = 'e';
053
054    public static final char HELP = 'h';
055
056    public static final char VERSION = 'v';
057
058    public static final char SHOW_VERSION = 'V';
059
060    public static final char NON_RECURSIVE = 'N';
061
062    public static final char UPDATE_SNAPSHOTS = 'U';
063
064    public static final char ACTIVATE_PROFILES = 'P';
065
066    public static final String SUPRESS_SNAPSHOT_UPDATES = "nsu";
067
068    public static final char CHECKSUM_FAILURE_POLICY = 'C';
069
070    public static final char CHECKSUM_WARNING_POLICY = 'c';
071
072    public static final char ALTERNATE_USER_SETTINGS = 's';
073
074    public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
075
076    public static final char ALTERNATE_USER_TOOLCHAINS = 't';
077
078    public static final String FAIL_FAST = "ff";
079
080    public static final String FAIL_AT_END = "fae";
081
082    public static final String FAIL_NEVER = "fn";
083
084    public static final String RESUME_FROM = "rf";
085
086    public static final String PROJECT_LIST = "pl";
087
088    public static final String ALSO_MAKE = "am";
089
090    public static final String ALSO_MAKE_DEPENDENTS = "amd";
091
092    public static final String LOG_FILE = "l";
093
094    public static final String ENCRYPT_MASTER_PASSWORD = "emp";
095
096    public static final String ENCRYPT_PASSWORD = "ep";
097
098    public static final String THREADS = "T";
099
100    public static final String LEGACY_LOCAL_REPOSITORY = "llr";
101
102    protected Options options;
103
104    @SuppressWarnings( "static-access" )
105    public CLIManager()
106    {
107        options = new Options();
108        options.addOption( OptionBuilder.withLongOpt( "help" ).withDescription( "Display help information" ).create( HELP ) );
109        options.addOption( OptionBuilder.withLongOpt( "file" ).hasArg().withDescription( "Force the use of an alternate POM file (or directory with pom.xml)." ).create( ALTERNATE_POM_FILE ) );
110        options.addOption( OptionBuilder.withLongOpt( "define" ).hasArg().withDescription( "Define a system property" ).create( SET_SYSTEM_PROPERTY ) );
111        options.addOption( OptionBuilder.withLongOpt( "offline" ).withDescription( "Work offline" ).create( OFFLINE ) );
112        options.addOption( OptionBuilder.withLongOpt( "version" ).withDescription( "Display version information" ).create( VERSION ) );
113        options.addOption( OptionBuilder.withLongOpt( "quiet" ).withDescription( "Quiet output - only show errors" ).create( QUIET ) );
114        options.addOption( OptionBuilder.withLongOpt( "debug" ).withDescription( "Produce execution debug output" ).create( DEBUG ) );
115        options.addOption( OptionBuilder.withLongOpt( "errors" ).withDescription( "Produce execution error messages" ).create( ERRORS ) );
116        options.addOption( OptionBuilder.withLongOpt( "non-recursive" ).withDescription( "Do not recurse into sub-projects" ).create( NON_RECURSIVE ) );
117        options.addOption( OptionBuilder.withLongOpt( "update-snapshots" ).withDescription( "Forces a check for updated releases and snapshots on remote repositories" ).create( UPDATE_SNAPSHOTS ) );
118        options.addOption( OptionBuilder.withLongOpt( "activate-profiles" ).withDescription( "Comma-delimited list of profiles to activate" ).hasArg().create( ACTIVATE_PROFILES ) );
119        options.addOption( OptionBuilder.withLongOpt( "batch-mode" ).withDescription( "Run in non-interactive (batch) mode" ).create( BATCH_MODE ) );
120        options.addOption( OptionBuilder.withLongOpt( "no-snapshot-updates" ).withDescription( "Suppress SNAPSHOT updates" ).create( SUPRESS_SNAPSHOT_UPDATES ) );
121        options.addOption( OptionBuilder.withLongOpt( "strict-checksums" ).withDescription( "Fail the build if checksums don't match" ).create( CHECKSUM_FAILURE_POLICY ) );
122        options.addOption( OptionBuilder.withLongOpt( "lax-checksums" ).withDescription( "Warn if checksums don't match" ).create( CHECKSUM_WARNING_POLICY ) );
123        options.addOption( OptionBuilder.withLongOpt( "settings" ).withDescription( "Alternate path for the user settings file" ).hasArg().create( ALTERNATE_USER_SETTINGS ) );
124        options.addOption( OptionBuilder.withLongOpt( "global-settings" ).withDescription( "Alternate path for the global settings file" ).hasArg().create( ALTERNATE_GLOBAL_SETTINGS ) );
125        options.addOption( OptionBuilder.withLongOpt( "toolchains" ).withDescription( "Alternate path for the user toolchains file" ).hasArg().create( ALTERNATE_USER_TOOLCHAINS ) );
126        options.addOption( OptionBuilder.withLongOpt( "fail-fast" ).withDescription( "Stop at first failure in reactorized builds" ).create( FAIL_FAST ) );
127        options.addOption( OptionBuilder.withLongOpt( "fail-at-end" ).withDescription( "Only fail the build afterwards; allow all non-impacted builds to continue" ).create( FAIL_AT_END ) );
128        options.addOption( OptionBuilder.withLongOpt( "fail-never" ).withDescription( "NEVER fail the build, regardless of project result" ).create( FAIL_NEVER ) );
129        options.addOption( OptionBuilder.withLongOpt( "resume-from" ).hasArg().withDescription( "Resume reactor from specified project" ).create( RESUME_FROM ) );
130        options.addOption( OptionBuilder.withLongOpt( "projects" ).withDescription( "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." ).hasArg().create( PROJECT_LIST ) );
131        options.addOption( OptionBuilder.withLongOpt( "also-make" ).withDescription( "If project list is specified, also build projects required by the list" ).create( ALSO_MAKE ) );
132        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 ) );
133        options.addOption( OptionBuilder.withLongOpt( "log-file" ).hasArg().withDescription( "Log file to where all build output will go." ).create( LOG_FILE ) );
134        options.addOption( OptionBuilder.withLongOpt( "show-version" ).withDescription( "Display version information WITHOUT stopping build" ).create( SHOW_VERSION ) );
135        options.addOption( OptionBuilder.withLongOpt( "encrypt-master-password" ).hasArg().withDescription( "Encrypt master security password" ).create( ENCRYPT_MASTER_PASSWORD ) );
136        options.addOption( OptionBuilder.withLongOpt( "encrypt-password" ).hasArg().withDescription( "Encrypt server password" ).create( ENCRYPT_PASSWORD ) );
137        options.addOption( OptionBuilder.withLongOpt( "threads" ).hasArg().withDescription( "Thread count, for instance 2.0C where C is core multiplied" ).create( THREADS ) );
138        options.addOption( OptionBuilder.withLongOpt( "legacy-local-repository" ).withDescription( "Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be activated by using -Dmaven.legacyLocalRepo=true" ).create( LEGACY_LOCAL_REPOSITORY ) );
139
140        // Adding this back in for compatibility with the verifier that hard codes this option.
141        options.addOption( OptionBuilder.withLongOpt( "no-plugin-registry" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npr" ) );
142        options.addOption( OptionBuilder.withLongOpt( "check-plugin-updates" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "cpu" ) );
143        options.addOption( OptionBuilder.withLongOpt( "update-plugins" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "up" ) );
144        options.addOption( OptionBuilder.withLongOpt( "no-plugin-updates" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npu" ) );
145    }
146
147    public CommandLine parse( String[] args )
148        throws ParseException
149    {
150        // We need to eat any quotes surrounding arguments...
151        String[] cleanArgs = cleanArgs( args );
152
153        CommandLineParser parser = new GnuParser();
154
155        return parser.parse( options, cleanArgs );
156    }
157
158    private String[] cleanArgs( String[] args )
159    {
160        List<String> cleaned = new ArrayList<String>();
161
162        StringBuilder currentArg = null;
163
164        for ( String arg : args )
165        {
166            boolean addedToBuffer = false;
167
168            if ( arg.startsWith( "\"" ) )
169            {
170                // if we're in the process of building up another arg, push it and start over.
171                // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
172                if ( currentArg != null )
173                {
174                    cleaned.add( currentArg.toString() );
175                }
176
177                // start building an argument here.
178                currentArg = new StringBuilder( arg.substring( 1 ) );
179                addedToBuffer = true;
180            }
181
182            // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
183            if ( arg.endsWith( "\"" ) )
184            {
185                String cleanArgPart = arg.substring( 0, arg.length() - 1 );
186
187                // if we're building an argument, keep doing so.
188                if ( currentArg != null )
189                {
190                    // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
191                    if ( addedToBuffer )
192                    {
193                        currentArg.setLength( currentArg.length() - 1 );
194                    }
195                    // otherwise, we trim the trailing " and append to the buffer.
196                    else
197                    {
198                        // TODO: introducing a space here...not sure what else to do but collapse whitespace
199                        currentArg.append( ' ' ).append( cleanArgPart );
200                    }
201
202                    cleaned.add( currentArg.toString() );
203                }
204                else
205                {
206                    cleaned.add( cleanArgPart );
207                }
208
209                currentArg = null;
210
211                continue;
212            }
213
214            // if we haven't added this arg to the buffer, and we ARE building an argument
215            // buffer, then append it with a preceding space...again, not sure what else to
216            // do other than collapse whitespace.
217            // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
218            if ( !addedToBuffer )
219            {
220                if ( currentArg != null )
221                {
222                    currentArg.append( ' ' ).append( arg );
223                }
224                else
225                {
226                    cleaned.add( arg );
227                }
228            }
229        }
230
231        if ( currentArg != null )
232        {
233            cleaned.add( currentArg.toString() );
234        }
235
236        int cleanedSz = cleaned.size();
237
238        String[] cleanArgs = null;
239
240        if ( cleanedSz == 0 )
241        {
242            cleanArgs = args;
243        }
244        else
245        {
246            cleanArgs = cleaned.toArray( new String[cleanedSz] );
247        }
248
249        return cleanArgs;
250    }
251
252    public void displayHelp( PrintStream stdout )
253    {
254        stdout.println();
255
256        PrintWriter pw = new PrintWriter( stdout );
257
258        HelpFormatter formatter = new HelpFormatter();
259
260        formatter.printHelp( pw, HelpFormatter.DEFAULT_WIDTH, "mvn [options] [<goal(s)>] [<phase(s)>]", "\nOptions:",
261                             options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, "\n", false );
262
263        pw.flush();
264    }
265}