add a command line parser

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@344954 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-11-16 08:29:06 +00:00
parent cabf3d4759
commit f1f18322e9
4 changed files with 269 additions and 45 deletions

View File

@ -11,18 +11,25 @@ ARGS=$@
)
ret=$?; if [ $ret != 0 ]; then exit $ret; fi
BOOTSTRAP_JAR=../bootstrap-mini/target/bootstrap-mini-2.0.1-SNAPSHOT.jar
BOOTSTRAP_JAR=bootstrap-mini/target/bootstrap-mini-2.0.1-SNAPSHOT.jar
(
cd bootstrap/bootstrap-installer
java -jar $BOOTSTRAP_JAR package $ARGS
java -jar ../$BOOTSTRAP_JAR package $ARGS
ret=$?; if [ $ret != 0 ]; then exit $ret; fi
)
ret=$?; if [ $ret != 0 ]; then exit $ret; fi
# TODO: improve M2_HOME setting
INSTALL_DIR=$M2_HOME
java -Dmaven.home=$INSTALL_DIR -jar bootstrap/bootstrap-installer/target/bootstrap-installer-2.0.1-SNAPSHOT.jar $ARGS
# TODO: get rid of M2_HOME?
java -jar bootstrap/bootstrap-installer/target/bootstrap-installer-2.0.1-SNAPSHOT.jar --prefix=`dirname $M2_HOME` $ARGS
ret=$?; if [ $ret != 0 ]; then exit $ret; fi
(
# TODO: should w ebe going back to the mini now that we have the real thing?
cd maven-core-it-verifier
java -jar ../bootstrap/$BOOTSTRAP_JAR package $ARGS
ret=$?; if [ $ret != 0 ]; then exit $ret; fi
)
ret=$?; if [ $ret != 0 ]; then exit $ret; fi
(

View File

@ -20,8 +20,9 @@ import org.apache.maven.bootstrap.Bootstrap;
import org.apache.maven.bootstrap.model.Dependency;
import org.apache.maven.bootstrap.model.ModelReader;
import org.apache.maven.bootstrap.util.FileUtils;
import org.codehaus.plexus.util.Os;
import org.apache.maven.bootstrap.util.SimpleArgumentParser;
import org.codehaus.plexus.util.Expand;
import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
@ -30,11 +31,9 @@ import org.codehaus.plexus.util.cli.WriterStreamConsumer;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.zip.ZipInputStream;
/**
* Main class for bootstrap module.
@ -46,32 +45,48 @@ public class BootstrapInstaller
{
private final Bootstrap bootstrapper;
public BootstrapInstaller( String[] args )
private final String prefix;
public BootstrapInstaller( SimpleArgumentParser parser )
throws Exception
{
this.bootstrapper = new Bootstrap( args );
this.bootstrapper = new Bootstrap( parser );
this.prefix = parser.getArgumentValue( "--prefix" );
}
public static void main( String[] args )
throws Exception
{
BootstrapInstaller bootstrap = new BootstrapInstaller( args );
SimpleArgumentParser parser = Bootstrap.createDefaultParser();
parser.addArgument( "--prefix", "The location to install Maven", true, getDefaultPrefix() );
parser.parseCommandLineArguments( args );
BootstrapInstaller bootstrap = new BootstrapInstaller( parser );
bootstrap.run();
}
private static String getDefaultPrefix()
{
String value;
if ( Os.isFamily( "windows" ) )
{
value = "c:\\program files";
}
else
{
value = "/usr/local";
}
return value;
}
private void run()
throws Exception
{
Date fullStart = new Date();
// TODO: use parameters instead, and use --prefix
String mavenHome = System.getProperty( "maven.home" );
if ( mavenHome == null )
{
throw new Exception( "maven.home system property is required" );
}
String basedir = System.getProperty( "user.dir" );
// TODO: only build this guy, then move the next part to a new phase using it for resolution
@ -92,17 +107,26 @@ public class BootstrapInstaller
File mavenCoreDir = mavenCoreModel.getProjectFile().getParentFile();
runMaven( installation, mavenCoreDir, new String[]{"clean", "assembly:assembly"} );
File file = new File( mavenCoreDir, "target/maven-" + mavenCoreModel.getVersion() + "-bin.zip" );
String finalName = "maven-" + mavenCoreModel.getVersion();
File file = new File( mavenCoreDir, "target/" + finalName + "-bin.zip" );
File mavenHome = new File( prefix, finalName );
System.out.println( "Installing Maven in " + mavenHome );
FileUtils.deleteDirectory( mavenHome );
Expand expand = new Expand();
expand.setSrc( file );
File prefix = new File( mavenHome ).getParentFile();
expand.setDest( prefix );
expand.setDest( new File( prefix ) );
expand.execute();
fixScriptPermissions( new File( prefix, "maven-" + mavenCoreModel.getVersion() + "/bin" ) );
if ( !mavenHome.exists() )
{
throw new Exception( "Maven was not installed in " + mavenHome );
}
fixScriptPermissions( new File( mavenHome, "bin" ) );
Bootstrap.stats( fullStart, new Date() );
}

View File

@ -32,6 +32,7 @@ import org.apache.maven.bootstrap.settings.Settings;
import org.apache.maven.bootstrap.util.FileUtils;
import org.apache.maven.bootstrap.util.IsolatedClassLoader;
import org.apache.maven.bootstrap.util.JarMojo;
import org.apache.maven.bootstrap.util.SimpleArgumentParser;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
@ -75,26 +76,39 @@ public class Bootstrap
private final ArtifactResolver resolver;
public Bootstrap( String[] args )
private static final String USER_HOME = System.getProperty( "user.home" );
public Bootstrap( SimpleArgumentParser parser )
throws Exception
{
String userHome = System.getProperty( "user.home" );
File settingsXml = getSettingsPath( userHome, args );
File settingsXml = new File( parser.getArgumentValue( "--settings" ) );
System.out.println( "Using settings from " + settingsXml );
Settings settings = Settings.read( userHome, settingsXml );
Settings settings = Settings.read( USER_HOME, settingsXml );
// TODO: have an alternative implementation of ArtifactResolver for source compiles
// - if building from source, checkout and build then resolve to built jar (still download POM?)
resolver = setupRepositories( settings );
}
public static SimpleArgumentParser createDefaultParser()
{
File defaultSettingsFile = new File( USER_HOME, ".m2/settings.xml" );
SimpleArgumentParser parser = new SimpleArgumentParser();
parser.addArgument( "--settings", "The location of the settings.xml file", "-s", true,
defaultSettingsFile.getAbsolutePath() );
return parser;
}
public static void main( String[] args )
throws Exception
{
Bootstrap bootstrap = new Bootstrap( args );
SimpleArgumentParser parser = createDefaultParser();
parser.parseCommandLineArguments( args );
Bootstrap bootstrap = new Bootstrap( parser );
String goal = null;
for ( int i = 0; i < args.length && goal == null; i++ )
@ -114,23 +128,6 @@ public class Bootstrap
bootstrap.run( goal );
}
private static File getSettingsPath( String userHome, String[] args )
throws Exception
{
for ( int i = 0; i < args.length; i++ )
{
if ( args[i].equals( "-s" ) || args[i].equals( "--settings" ) )
{
if ( i == args.length - 1 )
{
throw new Exception( "missing argument to -s" );
}
return new File( args[i + 1] );
}
}
return new File( userHome, ".m2/settings.xml" );
}
private void run( String goal )
throws Exception
{

View File

@ -0,0 +1,196 @@
package org.apache.maven.bootstrap.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Brett Porter
*/
public class SimpleArgumentParser
{
private Map arguments = new TreeMap();
private List parameters = new ArrayList();
public void parseCommandLineArguments( String[] args )
{
boolean stillHasArgs = true;
for ( int i = 0; i < args.length; i++ )
{
if ( args[i].equals( "--" ) )
{
stillHasArgs = false;
}
else if ( args[i].startsWith( "-" ) && stillHasArgs )
{
if ( args[i].startsWith( "--" ) )
{
String name;
int index = args[i].indexOf( '=' );
if ( index >= 0 )
{
name = args[i].substring( 0, index ).trim();
}
else
{
name = args[i];
}
Argument arg = (Argument) arguments.get( name );
if ( arg.isHasValue() )
{
String value = null;
if ( index >= 0 )
{
value = args[i].substring( index + 1 ).trim();
}
else if ( i != args.length - 1 && !args[i + 1].startsWith( "-" ) )
{
value = args[i + 1];
i++;
}
arg.setValue( value );
}
}
else
{
String name = args[i].substring( 0, 2 );
Argument arg = (Argument) arguments.get( name );
if ( arg.isHasValue() )
{
String value = null;
if ( args[i].length() > 2 )
{
value = args[i].substring( 2 );
}
else if ( i != args.length - 1 && !args[i + 1].startsWith( "-" ) )
{
value = args[i + 1];
i++;
}
arg.setValue( value );
}
}
}
else
{
parameters.add( args[i] );
}
}
}
public void addArgument( String argument, String description, String alias )
{
addArgument( argument, description, alias, false, null );
}
public void addArgument( String argument, String description, String alias, boolean hasValue, String defaultValue )
{
Argument arg = new Argument( argument, description, alias, hasValue, defaultValue );
arguments.put( argument, arg );
if ( alias != null )
{
arguments.put( alias, arg );
}
}
public String getArgumentValue( String argument )
{
Argument arg = (Argument) arguments.get( argument );
String value = null;
if ( arg != null )
{
value = arg.getValue();
}
return value;
}
public List getParameters()
{
return parameters;
}
public void addArgument( String argument, String description, boolean hasValue, String defaultValue )
{
addArgument( argument, description, null, hasValue, defaultValue );
}
private static class Argument
{
private final String name;
private final String description;
private final String alias;
private String value;
private final boolean hasValue;
private final String defaultValue;
public Argument( String name, String description, String alias, boolean hasValue, String defaultValue )
{
this.name = name;
this.description = description;
this.alias = alias;
this.hasValue = hasValue;
this.defaultValue = defaultValue;
}
public String getName()
{
return name;
}
public String getDescription()
{
return description;
}
public String getAlias()
{
return alias;
}
public String getValue()
{
if ( value == null )
{
return defaultValue;
}
return value;
}
public void setValue( String value )
{
this.value = value;
}
public boolean isHasValue()
{
return hasValue;
}
}
}