View Javadoc

1   package org.apache.maven.cli;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import org.apache.commons.cli.CommandLine;
22  import org.apache.commons.cli.CommandLineParser;
23  import org.apache.commons.cli.HelpFormatter;
24  import org.apache.commons.cli.OptionBuilder;
25  import org.apache.commons.cli.Options;
26  import org.apache.commons.cli.ParseException;
27  import org.apache.commons.cli.PosixParser;
28  
29  /**
30   * Utility for managing and parsing MavenSession's command-line.
31   *
32   * <p>
33   * <b>implementation note:</b> This is the class to modify when
34   * adding, removing or modifying MavenSession's command-line options
35   * and flags.
36   * </p>
37   *
38   * <p>
39   * This class uses the
40   * <a href="http://commons.apache.org/cli/">commons-cli</a>
41   * library for command-line parsing.
42   * </p>
43   *
44   * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
45   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
46   */
47  public class CLIManager
48  {
49      /**
50       * MavenSession't command-line option configuration.
51       */
52      private static Options options = null;
53  
54      /** Configure the option set. */
55  
56      static
57      {
58          options = new Options();
59  
60          /*
61           * Although this looks broken and confusing, it is by design (commons-cli, not ours).
62           * It only presents an issue if this can be called in multiple threads in the
63           * same classloader simultaneously. Which it can't, as this is a static initializer.
64           * The new commons-cli fixes the Builder pattern to work in a better way (OptionBuilder
65           * no longer static).
66           */
67          OptionBuilder.withLongOpt( "nobanner" );
68          OptionBuilder.withDescription( "Suppress logo banner" );
69          options.addOption( OptionBuilder.create( 'b' ) );
70  
71          OptionBuilder.withLongOpt( "define" );
72          OptionBuilder.hasArg();
73          OptionBuilder.withDescription( "Define a system property" );
74          options.addOption( OptionBuilder.create( 'D' ) );
75  
76          OptionBuilder.withLongOpt( "dir" );
77          OptionBuilder.hasArg();
78          OptionBuilder.withDescription( "Set effective working directory (ignored with -p or -f)" );
79          options.addOption( OptionBuilder.create( 'd' ) );
80  
81          OptionBuilder.withLongOpt( "exception" );
82          OptionBuilder.withDescription( "Produce exception stack traces" );
83          options.addOption( OptionBuilder.create( 'e' ) );
84  
85          OptionBuilder.withLongOpt( "emacs" );
86          OptionBuilder.withDescription( "Produce logging information without adornments" );
87          options.addOption( OptionBuilder.create( 'E' ) );
88  
89          OptionBuilder.withLongOpt( "find" );
90          OptionBuilder.hasArg();
91          OptionBuilder.withDescription( "Set project file and effective working directory by finding the project file" );
92          options.addOption( OptionBuilder.create( 'f' ) );
93  
94          OptionBuilder.withLongOpt( "goals" );
95          OptionBuilder.withDescription( "Display available goals" );
96          options.addOption( OptionBuilder.create( 'g' ) );
97  
98          OptionBuilder.withLongOpt( "usage" );
99          OptionBuilder.withDescription( "Display help on using the current project" );
100         options.addOption( OptionBuilder.create( 'u' ) );
101 
102         OptionBuilder.withLongOpt( "help" );
103         OptionBuilder.withDescription( "Display help information" );
104         options.addOption( OptionBuilder.create( 'h' ) );
105 
106         OptionBuilder.withLongOpt( "info" );
107         OptionBuilder.withDescription( "Display system information" );
108         options.addOption( OptionBuilder.create( 'i' ) );
109 
110         OptionBuilder.withLongOpt( "offline" );
111         OptionBuilder.withDescription( "Build is happening offline" );
112         options.addOption( OptionBuilder.create( 'o' ) );
113 
114         OptionBuilder.withLongOpt( "pom" );
115         OptionBuilder.hasArg();
116         OptionBuilder.withDescription( "Set project file" );
117         options.addOption( OptionBuilder.create( 'p' ) );
118 
119         OptionBuilder.withLongOpt( "version" );
120         OptionBuilder.withDescription( "Display version information" );
121         options.addOption( OptionBuilder.create( 'v' ) );
122 
123         OptionBuilder.withLongOpt( "quiet" );
124         OptionBuilder.withDescription( "Reduce execution output" );
125         options.addOption( OptionBuilder.create( 'q' ) );
126 
127         OptionBuilder.withLongOpt( "debug" );
128         OptionBuilder.withDescription( "Produce execution debug output" );
129         options.addOption( OptionBuilder.create( 'X' ) );
130 
131         OptionBuilder.withLongOpt( "plugin-help" );
132         OptionBuilder.withDescription( "Display help on using a given plugin" );
133         OptionBuilder.hasOptionalArg();
134         options.addOption( OptionBuilder.create( 'P' ) );
135     }
136 
137     /**
138      * Parse a string-array of command-line arguments.
139      *
140      * <p>
141      * This will parse the arguments against the configured
142      * maven command-line options, and return a <code>CommandLine</code>
143      * object which may be queried for the presence of flags
144      * and options and their arguments, if any.
145      * </p>
146      *
147      * @param args The command-line arguments to parse.
148      * @return The parsed <code>CommandLine</code> result.
149      * @throws ParseException If an error occurs while parsing
150      * the command-line options.
151      * @see <a href="http://commons.apache.org/cli/">CLI</a>
152      */
153     public static CommandLine parse( String[] args )
154         throws ParseException
155     {
156         CommandLineParser parser = new PosixParser();
157         return parser.parse( options, args );
158     }
159 
160     /**
161      * Display usage information based upon current
162      * command-line option configuration.
163      */
164     public static void displayHelp()
165     {
166         HelpFormatter formatter = new HelpFormatter();
167 
168         formatter.printHelp( "maven [options] [goal [goal2 [goal3] ...]]", "\nOptions:", options, "\n" );
169 
170     }
171 
172     /**
173      * Display system information. This is generally useful, maybe
174      * this could be made part of CLI? But it definitely shouldn't
175      * be stuck in MavenSession.
176      */
177     public static void displayInfo()
178     {
179         System.out.println( "----- Environment" );
180         String[] properties = new String[] {
181             "java.version",
182             "file.encoding",
183             "java.ext.dirs",
184             "java.class.path",
185             "os.name",
186             "java.vendor",
187             "sun.boot.class.path",
188             "java.runtime.name" };
189         for ( int i = 0; i < properties.length; i++ )
190         {
191             System.out.println( "  " + properties[i] + "=" + System.getProperty( properties[i] ) );
192         }
193         System.out.println( "-----" );
194     }
195 }