o push all pom format specific handling up to the front-end, internally inside maven we do not care what the source of POM information is

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@824098 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2009-10-11 16:18:10 +00:00
parent 737bc0f63b
commit 07398e00bc
5 changed files with 382 additions and 537 deletions

View File

@ -167,30 +167,6 @@ public class DefaultMavenExecutionRequestPopulator
} }
} }
private void pom( MavenExecutionRequest request )
{
if ( request.getPom() != null && !request.getPom().isAbsolute() )
{
request.setPom( request.getPom().getAbsoluteFile() );
}
if ( ( request.getPom() != null ) && ( request.getPom().getParentFile() != null ) )
{
request.setBaseDirectory( request.getPom().getParentFile() );
}
else if ( ( request.getPom() == null ) && ( request.getBaseDirectory() != null ) )
{
File pom = modelLocator.locatePom( new File( request.getBaseDirectory() ) );
request.setPom( pom );
}
// TODO: Is this correct?
else if ( request.getBaseDirectory() == null )
{
request.setBaseDirectory( new File( System.getProperty( "user.dir" ) ) );
}
}
private void populateDefaultPluginGroups( MavenExecutionRequest request ) private void populateDefaultPluginGroups( MavenExecutionRequest request )
{ {
request.addPluginGroup( "org.apache.maven.plugins" ); request.addPluginGroup( "org.apache.maven.plugins" );
@ -318,8 +294,6 @@ public class DefaultMavenExecutionRequestPopulator
public MavenExecutionRequest populateDefaults( MavenExecutionRequest request ) public MavenExecutionRequest populateDefaults( MavenExecutionRequest request )
throws MavenExecutionRequestPopulationException throws MavenExecutionRequestPopulationException
{ {
pom( request );
localRepository( request ); localRepository( request );
populateDefaultPluginGroups( request ); populateDefaultPluginGroups( request );

View File

@ -1,389 +0,0 @@
package org.apache.maven.cli;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Map.Entry;
import org.apache.commons.cli.CommandLine;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.repository.ArtifactTransferListener;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineUtils;
final class CLIRequestUtils
{
private CLIRequestUtils()
{
}
public static void populateProperties( MavenExecutionRequest request, CommandLine commandLine )
{
Properties systemProperties = new Properties();
Properties userProperties = new Properties();
populateProperties( commandLine, systemProperties, userProperties );
request.setUserProperties( userProperties );
request.setSystemProperties( systemProperties );
}
public static MavenExecutionRequest populateRequest( MavenExecutionRequest request, CommandLine commandLine,
String workingDirectory, boolean debug, boolean quiet,
boolean showErrors )
{
// ----------------------------------------------------------------------
// Now that we have everything that we need we will fire up plexus and
// bring the maven component to life for use.
// ----------------------------------------------------------------------
if ( commandLine.hasOption( CLIManager.BATCH_MODE ) )
{
request.setInteractiveMode( false );
}
boolean pluginUpdateOverride = false;
if ( commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES )
|| commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES2 ) )
{
pluginUpdateOverride = true;
}
else if ( commandLine.hasOption( CLIManager.SUPPRESS_PLUGIN_UPDATES ) )
{
pluginUpdateOverride = false;
}
boolean noSnapshotUpdates = false;
if ( commandLine.hasOption( CLIManager.SUPRESS_SNAPSHOT_UPDATES ) )
{
noSnapshotUpdates = true;
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
List<String> goals = commandLine.getArgList();
boolean recursive = true;
// this is the default behavior.
String reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
if ( commandLine.hasOption( CLIManager.NON_RECURSIVE ) )
{
recursive = false;
}
if ( commandLine.hasOption( CLIManager.FAIL_FAST ) )
{
reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
}
else if ( commandLine.hasOption( CLIManager.FAIL_AT_END ) )
{
reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_AT_END;
}
else if ( commandLine.hasOption( CLIManager.FAIL_NEVER ) )
{
reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_NEVER;
}
if ( commandLine.hasOption( CLIManager.OFFLINE ) )
{
request.setOffline( true );
}
boolean updateSnapshots = false;
if ( commandLine.hasOption( CLIManager.UPDATE_SNAPSHOTS ) )
{
updateSnapshots = true;
}
String globalChecksumPolicy = null;
if ( commandLine.hasOption( CLIManager.CHECKSUM_FAILURE_POLICY ) )
{
globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_FAIL;
}
else if ( commandLine.hasOption( CLIManager.CHECKSUM_WARNING_POLICY ) )
{
globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_WARN;
}
File baseDirectory = new File( workingDirectory, "" ).getAbsoluteFile();
// ----------------------------------------------------------------------
// Profile Activation
// ----------------------------------------------------------------------
List<String> activeProfiles = new ArrayList<String>();
List<String> inactiveProfiles = new ArrayList<String>();
if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) )
{
String [] profileOptionValues = commandLine.getOptionValues( CLIManager.ACTIVATE_PROFILES );
if ( profileOptionValues != null )
{
for ( int i = 0; i < profileOptionValues.length; ++i )
{
StringTokenizer profileTokens = new StringTokenizer( profileOptionValues[i] , "," );
while ( profileTokens.hasMoreTokens() )
{
String profileAction = profileTokens.nextToken().trim();
if ( profileAction.startsWith( "-" ) || profileAction.startsWith( "!" ) )
{
inactiveProfiles.add( profileAction.substring( 1 ) );
}
else if ( profileAction.startsWith( "+" ) )
{
activeProfiles.add( profileAction.substring( 1 ) );
}
else
{
activeProfiles.add( profileAction );
}
}
}
}
}
ArtifactTransferListener transferListener;
if ( request.isInteractiveMode() )
{
transferListener = new ConsoleMavenTransferListener();
}
else
{
transferListener = new BatchModeMavenTransferListener();
}
transferListener.setShowChecksumEvents( false );
String alternatePomFile = null;
if ( commandLine.hasOption( CLIManager.ALTERNATE_POM_FILE ) )
{
alternatePomFile = commandLine.getOptionValue( CLIManager.ALTERNATE_POM_FILE );
}
int loggingLevel;
if ( debug )
{
loggingLevel = MavenExecutionRequest.LOGGING_LEVEL_DEBUG;
}
else if ( quiet )
{
// TODO: we need to do some more work here. Some plugins use sys out or log errors at info level.
// Ideally, we could use Warn across the board
loggingLevel = MavenExecutionRequest.LOGGING_LEVEL_ERROR;
// TODO:Additionally, we can't change the mojo level because the component key includes the version and it isn't known ahead of time. This seems worth changing.
}
else
{
loggingLevel = MavenExecutionRequest.LOGGING_LEVEL_INFO;
}
Properties systemProperties = new Properties();
Properties userProperties = new Properties();
populateProperties( commandLine, systemProperties, userProperties );
File userToolchainsFile;
if ( commandLine.hasOption( CLIManager.ALTERNATE_USER_TOOLCHAINS ) )
{
userToolchainsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_USER_TOOLCHAINS ) );
userToolchainsFile = resolveFile( userToolchainsFile, workingDirectory );
}
else
{
userToolchainsFile = MavenCli.DEFAULT_USER_TOOLCHAINS_FILE;
}
request
.setBaseDirectory( baseDirectory )
.setGoals( goals )
.setSystemProperties( systemProperties )
.setUserProperties( userProperties )
.setReactorFailureBehavior( reactorFailureBehaviour ) // default: fail fast
.setRecursive( recursive ) // default: true
.setShowErrors( showErrors ) // default: false
.setUsePluginUpdateOverride( pluginUpdateOverride )
.addActiveProfiles( activeProfiles ) // optional
.addInactiveProfiles( inactiveProfiles ) // optional
.setLoggingLevel( loggingLevel ) // default: info
.setTransferListener( transferListener ) // default: batch mode which goes along with interactive
.setUpdateSnapshots( updateSnapshots ) // default: false
.setNoSnapshotUpdates( noSnapshotUpdates ) // default: false
.setGlobalChecksumPolicy( globalChecksumPolicy ) // default: warn
.setUserToolchainsFile( userToolchainsFile );
if ( alternatePomFile != null )
{
request.setPom( resolveFile( new File( alternatePomFile ), workingDirectory ) );
}
if ( commandLine.hasOption( CLIManager.RESUME_FROM ) )
{
request.setResumeFrom( commandLine.getOptionValue( CLIManager.RESUME_FROM ) );
}
if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) )
{
String projectList = commandLine.getOptionValue( CLIManager.PROJECT_LIST );
String[] projects = StringUtils.split( projectList, "," );
request.setSelectedProjects( Arrays.asList( projects ) );
}
if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && !commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
{
request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_UPSTREAM );
}
else if ( !commandLine.hasOption( CLIManager.ALSO_MAKE )
&& commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
{
request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_DOWNSTREAM );
}
else if ( commandLine.hasOption( CLIManager.ALSO_MAKE )
&& commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
{
request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_BOTH );
}
String localRepoProperty = request.getUserProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY );
if ( localRepoProperty == null )
{
localRepoProperty = request.getSystemProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY );
}
if ( localRepoProperty != null )
{
request.setLocalRepositoryPath( localRepoProperty );
}
return request;
}
static File resolveFile( File file, String workingDirectory )
{
if ( file == null )
{
return null;
}
else if ( file.isAbsolute() )
{
return file;
}
else if ( file.getPath().startsWith( File.separator ) )
{
// drive-relative Windows path
return file.getAbsoluteFile();
}
else
{
return new File( workingDirectory, file.getPath() );
}
}
// ----------------------------------------------------------------------
// System properties handling
// ----------------------------------------------------------------------
static void populateProperties( CommandLine commandLine, Properties systemProperties, Properties userProperties )
{
// add the env vars to the property set, with the "env." prefix
// XXX support for env vars should probably be removed from the ModelInterpolator
try
{
Properties envVars = CommandLineUtils.getSystemEnvVars();
for ( Entry<Object, Object> e : envVars.entrySet() )
{
systemProperties.setProperty( "env." + e.getKey().toString(), e.getValue().toString() );
}
}
catch ( IOException e )
{
System.err.println( "Error getting environment vars for profile activation: " + e );
}
// ----------------------------------------------------------------------
// Options that are set on the command line become system properties
// and therefore are set in the session properties. System properties
// are most dominant.
// ----------------------------------------------------------------------
if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) )
{
String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY );
if ( defStrs != null )
{
for ( int i = 0; i < defStrs.length; ++i )
{
setCliProperty( defStrs[i], userProperties );
}
}
}
systemProperties.putAll( System.getProperties() );
}
private static void setCliProperty( String property, Properties properties )
{
String name;
String value;
int i = property.indexOf( "=" );
if ( i <= 0 )
{
name = property.trim();
value = "true";
}
else
{
name = property.substring( 0, i ).trim();
value = property.substring( i + 1 ).trim();
}
properties.setProperty( name, value );
// ----------------------------------------------------------------------
// I'm leaving the setting of system properties here as not to break
// the SystemPropertyProfileActivator. This won't harm embedding. jvz.
// ----------------------------------------------------------------------
System.setProperty( name, value );
}
}

View File

@ -20,6 +20,12 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Map.Entry;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
@ -31,6 +37,8 @@ import org.apache.maven.execution.MavenExecutionRequestPopulationException;
import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequestPopulator; import org.apache.maven.execution.MavenExecutionRequestPopulator;
import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.locator.ModelLocator;
import org.apache.maven.repository.ArtifactTransferListener;
import org.apache.maven.settings.MavenSettingsBuilder; import org.apache.maven.settings.MavenSettingsBuilder;
import org.apache.maven.settings.Settings; import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
@ -45,6 +53,8 @@ import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger; import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher; import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher; import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
@ -75,6 +85,8 @@ public class MavenCli
private PrintStreamLogger logger; private PrintStreamLogger logger;
private ModelLocator modelLocator;
public static void main( String[] args ) public static void main( String[] args )
{ {
int result = main( args, null ); int result = main( args, null );
@ -189,7 +201,7 @@ public class MavenCli
if ( commandLine.hasOption( CLIManager.LOG_FILE ) ) if ( commandLine.hasOption( CLIManager.LOG_FILE ) )
{ {
File logFile = new File( commandLine.getOptionValue( CLIManager.LOG_FILE ) ); File logFile = new File( commandLine.getOptionValue( CLIManager.LOG_FILE ) );
logFile = CLIRequestUtils.resolveFile( logFile, workingDirectory ); logFile = resolveFile( logFile, workingDirectory );
try try
{ {
@ -214,6 +226,8 @@ public class MavenCli
try try
{ {
maven = container.lookup( Maven.class ); maven = container.lookup( Maven.class );
modelLocator = container.lookup( ModelLocator.class );
} }
catch ( ComponentLookupException e ) catch ( ComponentLookupException e )
{ {
@ -229,7 +243,7 @@ public class MavenCli
request.setGlobalSettingsFile( configuration.getGlobalSettingsFile() ); request.setGlobalSettingsFile( configuration.getGlobalSettingsFile() );
request.setUserSettingsFile( configuration.getUserSettingsFile() ); request.setUserSettingsFile( configuration.getUserSettingsFile() );
CLIRequestUtils.populateProperties( request, commandLine ); populateProperties( request, commandLine );
Settings settings; Settings settings;
@ -305,7 +319,7 @@ public class MavenCli
return 1; return 1;
} }
CLIRequestUtils.populateRequest( request, commandLine, workingDirectory, debug, quiet, showErrors ); populateRequest( request, commandLine, workingDirectory, debug, quiet, showErrors );
request.setExecutionListener( new ExecutionEventLogger( logger ) ); request.setExecutionListener( new ExecutionEventLogger( logger ) );
@ -470,7 +484,7 @@ public class MavenCli
if ( commandLine.hasOption( CLIManager.ALTERNATE_USER_SETTINGS ) ) if ( commandLine.hasOption( CLIManager.ALTERNATE_USER_SETTINGS ) )
{ {
userSettingsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_USER_SETTINGS ) ); userSettingsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_USER_SETTINGS ) );
userSettingsFile = CLIRequestUtils.resolveFile( userSettingsFile, workingDirectory ); userSettingsFile = resolveFile( userSettingsFile, workingDirectory );
} }
else else
{ {
@ -482,7 +496,7 @@ public class MavenCli
if ( commandLine.hasOption( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) ) if ( commandLine.hasOption( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) )
{ {
globalSettingsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) ); globalSettingsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) );
globalSettingsFile = CLIRequestUtils.resolveFile( globalSettingsFile, workingDirectory ); globalSettingsFile = resolveFile( globalSettingsFile, workingDirectory );
} }
else else
{ {
@ -566,4 +580,367 @@ public class MavenCli
return result; return result;
} }
public void populateProperties( MavenExecutionRequest request, CommandLine commandLine )
{
Properties systemProperties = new Properties();
Properties userProperties = new Properties();
populateProperties( commandLine, systemProperties, userProperties );
request.setUserProperties( userProperties );
request.setSystemProperties( systemProperties );
}
public MavenExecutionRequest populateRequest( MavenExecutionRequest request, CommandLine commandLine,
String workingDirectory, boolean debug, boolean quiet, boolean showErrors )
{
// ----------------------------------------------------------------------
// Now that we have everything that we need we will fire up plexus and
// bring the maven component to life for use.
// ----------------------------------------------------------------------
if ( commandLine.hasOption( CLIManager.BATCH_MODE ) )
{
request.setInteractiveMode( false );
}
boolean pluginUpdateOverride = false;
if ( commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES )
|| commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES2 ) )
{
pluginUpdateOverride = true;
}
else if ( commandLine.hasOption( CLIManager.SUPPRESS_PLUGIN_UPDATES ) )
{
pluginUpdateOverride = false;
}
boolean noSnapshotUpdates = false;
if ( commandLine.hasOption( CLIManager.SUPRESS_SNAPSHOT_UPDATES ) )
{
noSnapshotUpdates = true;
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
List<String> goals = commandLine.getArgList();
boolean recursive = true;
// this is the default behavior.
String reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
if ( commandLine.hasOption( CLIManager.NON_RECURSIVE ) )
{
recursive = false;
}
if ( commandLine.hasOption( CLIManager.FAIL_FAST ) )
{
reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
}
else if ( commandLine.hasOption( CLIManager.FAIL_AT_END ) )
{
reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_AT_END;
}
else if ( commandLine.hasOption( CLIManager.FAIL_NEVER ) )
{
reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_NEVER;
}
if ( commandLine.hasOption( CLIManager.OFFLINE ) )
{
request.setOffline( true );
}
boolean updateSnapshots = false;
if ( commandLine.hasOption( CLIManager.UPDATE_SNAPSHOTS ) )
{
updateSnapshots = true;
}
String globalChecksumPolicy = null;
if ( commandLine.hasOption( CLIManager.CHECKSUM_FAILURE_POLICY ) )
{
globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_FAIL;
}
else if ( commandLine.hasOption( CLIManager.CHECKSUM_WARNING_POLICY ) )
{
globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_WARN;
}
File baseDirectory = new File( workingDirectory, "" ).getAbsoluteFile();
// ----------------------------------------------------------------------
// Profile Activation
// ----------------------------------------------------------------------
List<String> activeProfiles = new ArrayList<String>();
List<String> inactiveProfiles = new ArrayList<String>();
if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) )
{
String [] profileOptionValues = commandLine.getOptionValues( CLIManager.ACTIVATE_PROFILES );
if ( profileOptionValues != null )
{
for ( int i = 0; i < profileOptionValues.length; ++i )
{
StringTokenizer profileTokens = new StringTokenizer( profileOptionValues[i] , "," );
while ( profileTokens.hasMoreTokens() )
{
String profileAction = profileTokens.nextToken().trim();
if ( profileAction.startsWith( "-" ) || profileAction.startsWith( "!" ) )
{
inactiveProfiles.add( profileAction.substring( 1 ) );
}
else if ( profileAction.startsWith( "+" ) )
{
activeProfiles.add( profileAction.substring( 1 ) );
}
else
{
activeProfiles.add( profileAction );
}
}
}
}
}
ArtifactTransferListener transferListener;
if ( request.isInteractiveMode() )
{
transferListener = new ConsoleMavenTransferListener();
}
else
{
transferListener = new BatchModeMavenTransferListener();
}
transferListener.setShowChecksumEvents( false );
String alternatePomFile = null;
if ( commandLine.hasOption( CLIManager.ALTERNATE_POM_FILE ) )
{
alternatePomFile = commandLine.getOptionValue( CLIManager.ALTERNATE_POM_FILE );
}
int loggingLevel;
if ( debug )
{
loggingLevel = MavenExecutionRequest.LOGGING_LEVEL_DEBUG;
}
else if ( quiet )
{
// TODO: we need to do some more work here. Some plugins use sys out or log errors at info level.
// Ideally, we could use Warn across the board
loggingLevel = MavenExecutionRequest.LOGGING_LEVEL_ERROR;
// TODO:Additionally, we can't change the mojo level because the component key includes the version and it isn't known ahead of time. This seems worth changing.
}
else
{
loggingLevel = MavenExecutionRequest.LOGGING_LEVEL_INFO;
}
Properties systemProperties = new Properties();
Properties userProperties = new Properties();
populateProperties( commandLine, systemProperties, userProperties );
File userToolchainsFile;
if ( commandLine.hasOption( CLIManager.ALTERNATE_USER_TOOLCHAINS ) )
{
userToolchainsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_USER_TOOLCHAINS ) );
userToolchainsFile = resolveFile( userToolchainsFile, workingDirectory );
}
else
{
userToolchainsFile = MavenCli.DEFAULT_USER_TOOLCHAINS_FILE;
}
request
.setBaseDirectory( baseDirectory )
.setGoals( goals )
.setSystemProperties( systemProperties )
.setUserProperties( userProperties )
.setReactorFailureBehavior( reactorFailureBehaviour ) // default: fail fast
.setRecursive( recursive ) // default: true
.setShowErrors( showErrors ) // default: false
.setUsePluginUpdateOverride( pluginUpdateOverride )
.addActiveProfiles( activeProfiles ) // optional
.addInactiveProfiles( inactiveProfiles ) // optional
.setLoggingLevel( loggingLevel ) // default: info
.setTransferListener( transferListener ) // default: batch mode which goes along with interactive
.setUpdateSnapshots( updateSnapshots ) // default: false
.setNoSnapshotUpdates( noSnapshotUpdates ) // default: false
.setGlobalChecksumPolicy( globalChecksumPolicy ) // default: warn
.setUserToolchainsFile( userToolchainsFile );
if ( alternatePomFile != null )
{
request.setPom( resolveFile( new File( alternatePomFile ), workingDirectory ) );
}
else if ( request.getPom() != null && !request.getPom().isAbsolute() )
{
request.setPom( request.getPom().getAbsoluteFile() );
}
if ( ( request.getPom() != null ) && ( request.getPom().getParentFile() != null ) )
{
request.setBaseDirectory( request.getPom().getParentFile() );
}
else if ( ( request.getPom() == null ) && ( request.getBaseDirectory() != null ) )
{
File pom = modelLocator.locatePom( new File( request.getBaseDirectory() ) );
request.setPom( pom );
}
// TODO: Is this correct?
else if ( request.getBaseDirectory() == null )
{
request.setBaseDirectory( new File( System.getProperty( "user.dir" ) ) );
}
if ( commandLine.hasOption( CLIManager.RESUME_FROM ) )
{
request.setResumeFrom( commandLine.getOptionValue( CLIManager.RESUME_FROM ) );
}
if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) )
{
String projectList = commandLine.getOptionValue( CLIManager.PROJECT_LIST );
String[] projects = StringUtils.split( projectList, "," );
request.setSelectedProjects( Arrays.asList( projects ) );
}
if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && !commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
{
request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_UPSTREAM );
}
else if ( !commandLine.hasOption( CLIManager.ALSO_MAKE )
&& commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
{
request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_DOWNSTREAM );
}
else if ( commandLine.hasOption( CLIManager.ALSO_MAKE )
&& commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) )
{
request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_BOTH );
}
String localRepoProperty = request.getUserProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY );
if ( localRepoProperty == null )
{
localRepoProperty = request.getSystemProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY );
}
if ( localRepoProperty != null )
{
request.setLocalRepositoryPath( localRepoProperty );
}
return request;
}
static File resolveFile( File file, String workingDirectory )
{
if ( file == null )
{
return null;
}
else if ( file.isAbsolute() )
{
return file;
}
else if ( file.getPath().startsWith( File.separator ) )
{
// drive-relative Windows path
return file.getAbsoluteFile();
}
else
{
return new File( workingDirectory, file.getPath() );
}
}
// ----------------------------------------------------------------------
// System properties handling
// ----------------------------------------------------------------------
static void populateProperties( CommandLine commandLine, Properties systemProperties, Properties userProperties )
{
// add the env vars to the property set, with the "env." prefix
// XXX support for env vars should probably be removed from the ModelInterpolator
try
{
Properties envVars = CommandLineUtils.getSystemEnvVars();
for ( Entry<Object, Object> e : envVars.entrySet() )
{
systemProperties.setProperty( "env." + e.getKey().toString(), e.getValue().toString() );
}
}
catch ( IOException e )
{
System.err.println( "Error getting environment vars for profile activation: " + e );
}
// ----------------------------------------------------------------------
// Options that are set on the command line become system properties
// and therefore are set in the session properties. System properties
// are most dominant.
// ----------------------------------------------------------------------
if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) )
{
String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY );
if ( defStrs != null )
{
for ( int i = 0; i < defStrs.length; ++i )
{
setCliProperty( defStrs[i], userProperties );
}
}
}
systemProperties.putAll( System.getProperties() );
}
private static void setCliProperty( String property, Properties properties )
{
String name;
String value;
int i = property.indexOf( "=" );
if ( i <= 0 )
{
name = property.trim();
value = "true";
}
else
{
name = property.substring( 0, i ).trim();
value = property.substring( i + 1 ).trim();
}
properties.setProperty( name, value );
// ----------------------------------------------------------------------
// I'm leaving the setting of system properties here as not to break
// the SystemPropertyProfileActivator. This won't harm embedding. jvz.
// ----------------------------------------------------------------------
System.setProperty( name, value );
}
} }

View File

@ -1,116 +0,0 @@
package org.apache.maven.cli;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.io.File;
import java.util.List;
import java.util.Properties;
import junit.framework.TestCase;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
public class CLIRequestUtilsTest
extends TestCase
{
private CommandLine parse( String... args )
throws ParseException
{
return new CLIManager().parse( args );
}
public void test_buildRequest_ParseCommandLineProperty()
throws ParseException
{
String key = "key";
String value = "value";
String[] args = {
"-D" + key + "=" + value
};
CommandLine commandLine = parse( args );
assertTrue( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) );
System.out.println( commandLine.getOptionValue( CLIManager.SET_SYSTEM_PROPERTY ) );
System.out.println( commandLine.getArgList() );
assertEquals( 1, commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY ).length );
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request = CLIRequestUtils.populateRequest( request, commandLine, null, false, false, false );
Properties userProperties = request.getUserProperties();
assertEquals( value, userProperties.getProperty( key ) );
List<String> goals = request.getGoals();
assertTrue( ( goals == null ) || goals.isEmpty() );
}
public void testGetExecutionProperties()
throws Exception
{
System.setProperty( "test.property.1", "1.0" );
System.setProperty( "test.property.2", "2.0" );
Properties execProperties = new Properties();
Properties userProperties = new Properties();
CLIRequestUtils.populateProperties( parse(
"-Dtest.property.2=2.1",
"-Dtest.property.3=3.0"
), execProperties, userProperties );
// assume that everybody has a PATH env var
String envPath = execProperties.getProperty( "env.PATH" );
String envPath2 = userProperties.getProperty( "env.PATH" );
if ( envPath == null )
{
envPath = execProperties.getProperty( "env.Path" );
envPath2 = userProperties.getProperty( "env.Path" );
}
assertNotNull( envPath );
assertNull( envPath2 );
assertEquals( "1.0", execProperties.getProperty( "test.property.1" ) );
assertNull( userProperties.getProperty( "test.property.1" ) );
assertEquals( "3.0", execProperties.getProperty( "test.property.3" ) );
assertEquals( "3.0", userProperties.getProperty( "test.property.3" ) );
}
public void testMavenRepoLocal()
throws Exception
{
String path = new File( "" ).getAbsolutePath();
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
CLIRequestUtils.populateRequest( request, parse( "-Dmaven.repo.local=" + path ), null, false, false, false );
assertEquals( path, request.getLocalRepositoryPath().getAbsolutePath() );
}
}

View File

@ -34,7 +34,6 @@ import org.apache.maven.model.Model;
*/ */
public interface ModelReader public interface ModelReader
{ {
/** /**
* The key for the option to enable strict parsing. This option is of type {@link Boolean} and defaults to {@code * The key for the option to enable strict parsing. This option is of type {@link Boolean} and defaults to {@code
* true}. If {@code false}, unknown elements will be ignored instead of causing a failure. * true}. If {@code false}, unknown elements will be ignored instead of causing a failure.