2020
2121import org .apache .commons .cli .CommandLine ;
2222import org .apache .commons .cli .CommandLineParser ;
23- import org .apache .commons .cli .GnuParser ;
24- import org .apache .commons .cli .HelpFormatter ;
23+ import org .apache .commons .cli .DefaultParser ;
2524import org .apache .commons .cli .Option ;
26- import org .apache .commons .cli .OptionBuilder ;
2725import org .apache .commons .cli .Options ;
2826import org .apache .commons .cli .ParseException ;
27+ import org .apache .commons .cli .help .HelpFormatter ;
2928
3029import java .io .IOException ;
3130import java .io .InputStream ;
3635 * @author Olivier Lamy
3736 * @since 2.0
3837 */
39- @ SuppressWarnings ( "static-access" )
4038public class TomcatRunnerCli
4139{
4240
4341 public static final String STAND_ALONE_PROPERTIES_FILENAME = "tomcat.standalone.properties" ;
4442
45- static Option httpPort =
46- OptionBuilder .withArgName ( "httpPort" ).hasArg ().withDescription ( "http port to use" ).create ( "httpPort" );
43+ static final Option HTTP_PORT =
44+ Option .builder ().longOpt ( "httpPort" ).hasArg ().argName ( "httpPort" )
45+ .desc ( "http port to use" ).get ();
4746
48- static Option httpsPort =
49- OptionBuilder .withArgName ( "httpsPort" ).hasArg ().withDescription ( "https port to use" ).create ( "httpsPort" );
47+ static final Option HTTPS_PORT =
48+ Option .builder ().longOpt ( "httpsPort" ).hasArg ().argName ( "httpsPort" )
49+ .desc ( "https port to use" ).get ();
5050
51- static Option maxPostSize =
52- OptionBuilder . withArgName ( "maxPostSize" ).hasArg ().withDescription ( "max post size to use " ). create (
53- "maxPostSize" );
51+ static final Option MAX_POST_SIZE =
52+ Option . builder (). longOpt ( "maxPostSize" ).hasArg ().argName ( "maxPostSize " )
53+ . desc ( "max post size to use" ). get ( );
5454
55- static Option ajpPort =
56- OptionBuilder .withArgName ( "ajpPort" ).hasArg ().withDescription ( "ajp port to use" ).create ( "ajpPort" );
55+ static final Option AJP_PORT =
56+ Option .builder ().longOpt ( "ajpPort" ).hasArg ().argName ( "ajpPort" )
57+ .desc ( "ajp port to use" ).get ();
5758
58- static Option serverXmlPath =
59- OptionBuilder . withArgName ( "serverXmlPath" ).hasArg ().withDescription ( "server.xml to use, optional " ). create (
60- "serverXmlPath" );
59+ static final Option SERVER_XML_PATH =
60+ Option . builder (). longOpt ( "serverXmlPath" ).hasArg ().argName ( "serverXmlPath " )
61+ . desc ( "server.xml to use, optional" ). get ( );
6162
62- static Option resetExtract =
63- OptionBuilder .withArgName ( "resetExtract" ).withDescription ( "clean previous extract directory" ).create (
64- "resetExtract" );
63+ static final Option RESET_EXTRACT =
64+ Option .builder ().longOpt ( "resetExtract" ).desc ( "clean previous extract directory" ).get ();
6565
66- static Option help = OptionBuilder .withLongOpt ( "help" ).withDescription ( "help" ).create ( 'h' );
66+ static final Option HELP =
67+ Option .builder ().longOpt ( "help" ).desc ( "help" ).get ();
6768
68- static Option debug = OptionBuilder .withLongOpt ( "debug" ).withDescription ( "debug" ).create ( 'X' );
69+ static final Option DEBUG =
70+ Option .builder ().option ( "X" ).longOpt ( "debug" ).desc ( "debug" ).get ();
6971
70- static Option sysProps = OptionBuilder .withDescription ( "use value for given property" ).hasArgs ().withDescription (
71- "key=value" ).withValueSeparator ().create ( 'D' );
72+ static final Option SYS_PROPS =
73+ Option .builder ().option ( "D" ).hasArgs ().valueSeparator ().argName ( "key=value" )
74+ .desc ( "use value for given property" ).get ();
7275
73- static Option clientAuth =
74- OptionBuilder .withArgName ( "clientAuth" ).withDescription ( "enable client authentication for https" ).create (
75- "clientAuth" );
76+ static final Option CLIENT_AUTH =
77+ Option .builder ().longOpt ( "clientAuth" ).desc ( "enable client authentication for https" ).get ();
7678
77- static Option keyAlias =
78- OptionBuilder .withArgName ( "keyAlias" ).hasArgs ().withDescription ( "alias from keystore for ssl" ).create (
79- "keyAlias" );
79+ static final Option KEY_ALIAS =
80+ Option .builder ().longOpt ( "keyAlias" ).hasArgs ().argName ( "alias from keystore for ssl" ).get ();
8081
81- static Option obfuscate =
82- OptionBuilder . withArgName ( "password " ).hasArgs ().withDescription ( "obfuscate the password and exit " ). create (
83- "obfuscate" );
82+ static final Option OBFUSCATE =
83+ Option . builder (). longOpt ( "obfuscate " ).hasArgs ().argName ( "password" )
84+ . desc ( "obfuscate the password and exit" ). get ( );
8485
85- static Option httpProtocol = OptionBuilder .withArgName ( "httpProtocol" ).hasArg ().withDescription (
86- "http protocol to use: HTTP/1.1 or org.apache.coyote.http11.Http11NioProtocol" ).create ( "httpProtocol" );
86+ static final Option HTTP_PROTOCOL =
87+ Option .builder ().longOpt ( "httpProtocol" ).hasArg ().argName ( "httpProtocol" )
88+ .desc ( "http protocol to use: HTTP/1.1 or org.apache.coyote.http11.Http11NioProtocol" ).get ();
8789
88- static Option extractDirectory = OptionBuilder .withArgName ( "extractDirectory" ).hasArg ().withDescription (
89- "path to extract war content, default value: .extract" ).create ( "extractDirectory" );
90+ static final Option EXTRACT_DIRECTORY =
91+ Option .builder ().longOpt ( "extractDirectory" ).hasArg ().argName ( "extractDirectory" )
92+ .desc ( "path to extract war content, default value: .extract" ).get ();
9093
91- static Option uriEncoding = OptionBuilder .withArgName ( "uriEncoding" ).hasArg ().withDescription (
92- "connector uriEncoding default ISO-8859-1" ).create ( "uriEncoding" );
94+ static final Option URI_ENCODING =
95+ Option .builder ().longOpt ( "uriEncoding" ).hasArg ().argName ( "uriEncoding" )
96+ .desc ( "connector uriEncoding default ISO-8859-1" ).get ();
9397
94- static Options options = new Options ();
98+ static final Options OPTIONS = new Options ();
9599
96100 static
97101 {
98- options .addOption ( httpPort ) //
99- .addOption ( httpsPort ) //
100- .addOption ( ajpPort ) //
101- .addOption ( serverXmlPath ) //
102- .addOption ( resetExtract ) //
103- .addOption ( help ) //
104- .addOption ( debug ) //
105- .addOption ( sysProps ) //
106- .addOption ( httpProtocol ) //
107- .addOption ( clientAuth ) //
108- .addOption ( keyAlias ) //
109- .addOption ( obfuscate ) //
110- .addOption ( extractDirectory ) //
111- .addOption ( uriEncoding ) //
112- .addOption ( maxPostSize );
102+ OPTIONS .addOption ( HTTP_PORT )
103+ .addOption ( HTTPS_PORT )
104+ .addOption ( AJP_PORT )
105+ .addOption ( MAX_POST_SIZE )
106+ .addOption ( SERVER_XML_PATH )
107+ .addOption ( RESET_EXTRACT )
108+ .addOption ( HELP )
109+ .addOption ( DEBUG )
110+ .addOption ( SYS_PROPS )
111+ .addOption ( HTTP_PROTOCOL )
112+ .addOption ( CLIENT_AUTH )
113+ .addOption ( KEY_ALIAS )
114+ .addOption ( OBFUSCATE )
115+ .addOption ( EXTRACT_DIRECTORY )
116+ .addOption ( URI_ENCODING );
113117 }
114118
115119
116120 public static void main ( String [] args )
117121 throws Exception
118122 {
119- CommandLineParser parser = new GnuParser ();
123+ CommandLineParser parser = new DefaultParser ();
120124 CommandLine line = null ;
121125 try
122126 {
123- line = parser .parse ( TomcatRunnerCli .options , args );
127+ line = parser .parse ( TomcatRunnerCli .OPTIONS , args );
124128 }
125129 catch ( ParseException e )
126130 {
127131 System .err .println ( "Parsing failed. Reason: " + e .getMessage () );
128- HelpFormatter formatter = new HelpFormatter ();
129- formatter .printHelp ( getCmdLineSyntax (), TomcatRunnerCli .options );
132+ HelpFormatter formatter = HelpFormatter .builder ().get ();
133+ formatter .printHelp (getCmdLineSyntax (), "Apache Tomcat Maven plugin executable WAR" , TomcatRunnerCli .OPTIONS ,
134+ "NOTE: The command line options are disabled if using Tomcat server.xml configuration" , true );
130135 System .exit ( 1 );
131136 }
132137
133- if ( line .hasOption ( help . getOpt () ) )
138+ if ( line .hasOption ( HELP ) )
134139 {
135- HelpFormatter formatter = new HelpFormatter ();
136- formatter .printHelp ( getCmdLineSyntax (), TomcatRunnerCli .options );
140+ HelpFormatter formatter = HelpFormatter .builder ().get ();
141+ formatter .printHelp (getCmdLineSyntax (), "Apache Tomcat Maven plugin executable WAR" , TomcatRunnerCli .OPTIONS ,
142+ "NOTE: The command line options are disabled if using Tomcat server.xml configuration" , true );
137143 System .exit ( 0 );
138144 }
139145
140- if ( line .hasOption ( obfuscate . getOpt () ) )
146+ if ( line .hasOption ( OBFUSCATE ) )
141147 {
142- System .out .println ( PasswordUtil .obfuscate ( line .getOptionValue ( obfuscate . getOpt () ) ) );
148+ System .out .println ( PasswordUtil .obfuscate ( line .getOptionValue ( OBFUSCATE ) ) );
143149 System .exit ( 0 );
144150 }
145151 TomcatRunner tomcatRunner = new TomcatRunner ();
146152
147153 tomcatRunner .runtimeProperties = buildStandaloneProperties ();
148154
149- if ( line .hasOption ( serverXmlPath . getOpt () ) )
155+ if ( line .hasOption ( SERVER_XML_PATH ) )
150156 {
151- tomcatRunner .serverXmlPath = line .getOptionValue ( serverXmlPath . getOpt () );
157+ tomcatRunner .serverXmlPath = line .getOptionValue ( SERVER_XML_PATH );
152158 }
153159
154160 String port = tomcatRunner .runtimeProperties .getProperty ( TomcatRunner .HTTP_PORT_KEY );
@@ -158,41 +164,41 @@ public static void main( String[] args )
158164 }
159165
160166 // cli win for the port
161- if ( line .hasOption ( httpPort . getOpt () ) )
167+ if ( line .hasOption ( HTTP_PORT ) )
162168 {
163- tomcatRunner .httpPort = Integer .parseInt ( line .getOptionValue ( httpPort . getOpt () ) );
169+ tomcatRunner .httpPort = Integer .parseInt ( line .getOptionValue ( HTTP_PORT ) );
164170 }
165171
166- if ( line .hasOption ( maxPostSize . getOpt () ) )
172+ if ( line .hasOption ( MAX_POST_SIZE ) )
167173 {
168- tomcatRunner .maxPostSize = Integer .parseInt ( line .getOptionValue ( maxPostSize . getOpt () ) );
174+ tomcatRunner .maxPostSize = Integer .parseInt ( line .getOptionValue ( MAX_POST_SIZE ) );
169175 }
170176
171- if ( line .hasOption ( httpsPort . getOpt () ) )
177+ if ( line .hasOption ( HTTPS_PORT ) )
172178 {
173- tomcatRunner .httpsPort = Integer .parseInt ( line .getOptionValue ( httpsPort . getOpt () ) );
179+ tomcatRunner .httpsPort = Integer .parseInt ( line .getOptionValue ( HTTPS_PORT ) );
174180 }
175- if ( line .hasOption ( ajpPort . getOpt () ) )
181+ if ( line .hasOption ( AJP_PORT ) )
176182 {
177- tomcatRunner .ajpPort = Integer .parseInt ( line .getOptionValue ( ajpPort . getOpt () ) );
183+ tomcatRunner .ajpPort = Integer .parseInt ( line .getOptionValue ( AJP_PORT ) );
178184 }
179- if ( line .hasOption ( resetExtract . getOpt () ) )
185+ if ( line .hasOption ( RESET_EXTRACT ) )
180186 {
181187 tomcatRunner .resetExtract = true ;
182188 }
183- if ( line .hasOption ( debug . getOpt () ) )
189+ if ( line .hasOption ( DEBUG ) )
184190 {
185191 tomcatRunner .debug = true ;
186192 }
187193
188- if ( line .hasOption ( httpProtocol . getOpt () ) )
194+ if ( line .hasOption ( HTTP_PROTOCOL ) )
189195 {
190- tomcatRunner .httpProtocol = line .getOptionValue ( httpProtocol . getOpt () );
196+ tomcatRunner .httpProtocol = line .getOptionValue ( HTTP_PROTOCOL );
191197 }
192198
193- if ( line .hasOption ( sysProps . getOpt () ) )
199+ if ( line .hasOption ( SYS_PROPS ) )
194200 {
195- Properties systemProperties = line .getOptionProperties ( sysProps . getOpt () );
201+ Properties systemProperties = line .getOptionProperties ( SYS_PROPS );
196202 if ( systemProperties != null && !systemProperties .isEmpty () )
197203 {
198204 for ( Map .Entry <Object , Object > sysProp : systemProperties .entrySet () )
@@ -201,23 +207,23 @@ public static void main( String[] args )
201207 }
202208 }
203209 }
204- if ( line .hasOption ( clientAuth . getOpt () ) )
210+ if ( line .hasOption ( CLIENT_AUTH ) )
205211 {
206212 tomcatRunner .clientAuth = "true" ;
207213 }
208- if ( line .hasOption ( keyAlias . getOpt () ) )
214+ if ( line .hasOption ( KEY_ALIAS ) )
209215 {
210- tomcatRunner .keyAlias = line .getOptionValue ( keyAlias . getOpt () );
216+ tomcatRunner .keyAlias = line .getOptionValue ( KEY_ALIAS );
211217 }
212218
213- if ( line .hasOption ( extractDirectory . getOpt () ) )
219+ if ( line .hasOption ( EXTRACT_DIRECTORY ) )
214220 {
215- tomcatRunner .extractDirectory = line .getOptionValue ( extractDirectory . getOpt () );
221+ tomcatRunner .extractDirectory = line .getOptionValue ( EXTRACT_DIRECTORY );
216222 }
217223
218- if ( line .hasOption ( uriEncoding . getOpt () ) )
224+ if ( line .hasOption ( URI_ENCODING ) )
219225 {
220- tomcatRunner .uriEncoding = line .getOptionValue ( uriEncoding . getOpt () );
226+ tomcatRunner .uriEncoding = line .getOptionValue ( URI_ENCODING );
221227 }
222228
223229 // here we go
@@ -238,4 +244,4 @@ public static String getCmdLineSyntax()
238244 {
239245 return "java -jar [path to your exec war jar]" ;
240246 }
241- }
247+ }
0 commit comments