[MNG-3714] Allow specification of the toolchains.xml location on the command line

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@761401 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-04-02 19:43:55 +00:00
parent d148da84da
commit 7a19b6bd5a
12 changed files with 337 additions and 186 deletions

View File

@ -71,6 +71,8 @@ public class DefaultMavenExecutionRequest
private File globalSettingsFile;
private File userToolchainsFile;
// ----------------------------------------------------------------------------
// Request
// ----------------------------------------------------------------------------
@ -138,6 +140,7 @@ public class DefaultMavenExecutionRequest
copy.setProjectPresent( original.isProjectPresent() );
copy.setUserSettingsFile( original.getUserSettingsFile() );
copy.setGlobalSettingsFile( original.getGlobalSettingsFile() );
copy.setUserToolchainsFile( original.getUserToolchainsFile() );
copy.setBaseDirectory( new File( original.getBaseDirectory() ) );
copy.setGoals( original.getGoals() );
copy.setUseReactor( original.useReactor() );
@ -652,7 +655,7 @@ public class DefaultMavenExecutionRequest
return this;
}
// Settin10gs files
// Settings files
public File getUserSettingsFile()
{
@ -678,6 +681,18 @@ public class DefaultMavenExecutionRequest
return this;
}
public File getUserToolchainsFile()
{
return userToolchainsFile;
}
public MavenExecutionRequest setUserToolchainsFile( File userToolchainsFile )
{
this.userToolchainsFile = userToolchainsFile;
return this;
}
public MavenExecutionRequest addRemoteRepository( ArtifactRepository repository )
{
if ( remoteRepositories == null )

View File

@ -230,5 +230,8 @@ public interface MavenExecutionRequest
MavenExecutionRequest setErrorReporter( CoreErrorReporter reporter );
CoreErrorReporter getErrorReporter();
File getUserToolchainsFile();
MavenExecutionRequest setUserToolchainsFile( File userToolchainsFile );
ProjectBuilderConfiguration getProjectBuildingConfiguration();
}

View File

@ -76,6 +76,8 @@ public class CLIManager
public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";
public static final char ALTERNATE_USER_TOOLCHAINS = 't';
public static final String FAIL_FAST = "ff";
public static final String FAIL_AT_END = "fae";
@ -111,6 +113,7 @@ public class CLIManager
options.addOption( OptionBuilder.withLongOpt( "lax-checksums" ).withDescription( "Warn if checksums don't match" ).create( CHECKSUM_WARNING_POLICY ) );
options.addOption( OptionBuilder.withLongOpt( "settings" ).withDescription( "Alternate path for the user settings file" ).hasArg().create( ALTERNATE_USER_SETTINGS ) );
options.addOption( OptionBuilder.withLongOpt( "global-settings" ).withDescription( "Alternate path for the global settings file" ).hasArg().create( ALTERNATE_GLOBAL_SETTINGS ) );
options.addOption( OptionBuilder.withLongOpt( "toolchains" ).withDescription( "Alternate path for the user toolchains file" ).hasArg().create( ALTERNATE_USER_TOOLCHAINS ) );
options.addOption( OptionBuilder.withLongOpt( "fail-fast" ).withDescription( "Stop at first failure in reactorized builds" ).create( FAIL_FAST ) );
options.addOption( OptionBuilder.withLongOpt( "fail-at-end" ).withDescription( "Only fail the build afterwards; allow all non-impacted builds to continue" ).create( FAIL_AT_END ) );
options.addOption( OptionBuilder.withLongOpt( "fail-never" ).withDescription( "NEVER fail the build, regardless of project result" ).create( FAIL_NEVER ) );

View File

@ -30,6 +30,7 @@ import java.util.Map.Entry;
import org.apache.commons.cli.CommandLine;
import org.apache.maven.MavenTransferListener;
import org.apache.maven.embedder.MavenEmbedder;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.codehaus.plexus.util.cli.CommandLineUtils;
@ -222,6 +223,16 @@ public final class CLIRequestUtils
Properties userProperties = new Properties();
populateProperties( commandLine, executionProperties, userProperties );
File userToolchainsFile;
if ( commandLine.hasOption( CLIManager.ALTERNATE_USER_TOOLCHAINS ) )
{
userToolchainsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_USER_TOOLCHAINS ) );
}
else
{
userToolchainsFile = MavenEmbedder.DEFAULT_USER_TOOLCHAINS_FILE;
}
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
.setBaseDirectory( baseDirectory )
.setGoals( goals )
@ -240,7 +251,9 @@ public final class CLIRequestUtils
.setTransferListener( transferListener ) // default: batch mode which goes along with interactive
.setUpdateSnapshots( updateSnapshots ) // default: false
.setNoSnapshotUpdates( noSnapshotUpdates ) // default: false
.setGlobalChecksumPolicy( globalChecksumPolicy ); // default: warn
.setGlobalChecksumPolicy( globalChecksumPolicy ) // default: warn
.setUserToolchainsFile( userToolchainsFile );
if ( alternatePomFile != null )
{

View File

@ -23,10 +23,8 @@ import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.Maven;
@ -80,11 +78,7 @@ import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException;
import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
@ -111,6 +105,8 @@ public class MavenEmbedder
public static final File DEFAULT_GLOBAL_SETTINGS_FILE =
new File( System.getProperty( "maven.home", System.getProperty( "user.dir", "" ) ), "conf/settings.xml" );
public static final File DEFAULT_USER_TOOLCHAINS_FILE = new File( userMavenConfigurationHome, "toolchains.xml" );
// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------

View File

@ -17,7 +17,6 @@ package org.apache.maven.embedder.execution;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@ -50,6 +49,7 @@ import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.SettingsUtils;
import org.apache.maven.toolchain.ToolchainsBuilder;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
@ -80,6 +80,9 @@ public class DefaultMavenExecutionRequestPopulator
@Requirement
private RepositorySystem repositorySystem;
@Requirement
private ToolchainsBuilder toolchainsBuilder;
// 2009-03-05 Oleg: this component is defined sub-classed in this package
@Requirement(hint = "maven")
private SecDispatcher securityDispatcher;
@ -99,6 +102,8 @@ public class DefaultMavenExecutionRequestPopulator
localRepository( request, configuration );
toolchains( request, configuration );
artifactTransferMechanism( request, configuration );
realmManager( request, configuration );
@ -526,4 +531,10 @@ public class DefaultMavenExecutionRequestPopulator
request.setProfileManager( globalProfileManager );
request.setProfileActivationContext( activationContext );
}
private void toolchains( MavenExecutionRequest request, Configuration configuration )
{
toolchainsBuilder.setUserToolchainsFile( request.getUserToolchainsFile() );
}
}

View File

@ -34,6 +34,18 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
<goal>generate-test-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>

View File

@ -1,3 +1,5 @@
package org.apache.maven.toolchain;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@ -17,214 +19,84 @@
* under the License.
*/
package org.apache.maven.toolchain;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.model.ToolchainModel;
import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.logging.Logger;
/**
*
* @author mkleint
*/
@Component(role=ToolchainManager.class)
public class DefaultToolchainManager extends AbstractLogEnabled
implements ToolchainManager,
ToolchainManagerPrivate
@Component( role = ToolchainManager.class )
public class DefaultToolchainManager
implements ToolchainManager
{
@Requirement
private PlexusContainer container;
public DefaultToolchainManager( )
{
}
@Requirement
Logger logger;
public ToolchainPrivate[] getToolchainsForType( String type )
throws MisconfiguredToolchainException
{
try
{
PersistedToolchains pers = readToolchainSettings ();
Map<String, ToolchainFactory> factories = container.lookupMap( ToolchainFactory.class );
List toRet = new ArrayList( );
if ( pers != null )
{
List lst = pers.getToolchains();
if ( lst != null )
{
Iterator it = lst.iterator();
while ( it.hasNext() )
{
ToolchainModel toolchainModel = (ToolchainModel) it.next();
ToolchainFactory fact = factories.get( toolchainModel.getType() );
if ( fact != null )
{
toRet.add( fact.createToolchain( toolchainModel ) );
}
else
{
getLogger().error("Missing toolchain factory for type:" + toolchainModel.getType() + ". Possibly caused by misconfigured project.");
}
}
}
}
for ( ToolchainFactory toolchainFactory : factories.values() )
{
ToolchainPrivate tool = toolchainFactory.createDefaultToolchain();
if ( tool != null )
{
toRet.add( tool );
}
}
ToolchainPrivate[] tc = new ToolchainPrivate[ toRet.size() ];
return (ToolchainPrivate[]) toRet.toArray(tc);
}
catch ( ComponentLookupException ex )
{
getLogger().fatalError("Error in component lookup", ex);
}
return new ToolchainPrivate[0];
}
@Requirement( role = ToolchainFactory.class )
Map<String, ToolchainFactory> factories;
public Toolchain getToolchainFromBuildContext( String type,
MavenSession session )
public Toolchain getToolchainFromBuildContext( String type, MavenSession session )
{
Map context = retrieveContext(session);
if ( "javac".equals( type ))
{
//HACK to make compiler plugin happy
type = "jdk";
}
Object obj = context.get( getStorageKey( type ) );
ToolchainModel model = (ToolchainModel)obj;
if ( model != null )
Map context = retrieveContext( session );
ToolchainModel model = (ToolchainModel) context.get( getStorageKey( type ) );
if ( model != null )
{
try
{
ToolchainFactory fact = container.lookup(ToolchainFactory.class, type);
return fact.createToolchain( model );
}
catch ( ComponentLookupException ex )
{
getLogger().fatalError("Error in component lookup", ex);
ToolchainFactory fact = factories.get( type );
if ( fact != null )
{
return fact.createToolchain( model );
}
else
{
logger.error( "Missing toolchain factory for type: " + type
+ ". Possibly caused by misconfigured project." );
}
}
catch ( MisconfiguredToolchainException ex )
{
getLogger().error("Misconfigured toolchain.", ex);
logger.error( "Misconfigured toolchain.", ex );
}
}
return null;
}
private MavenProject getCurrentProject(MavenSession session) {
//use reflection since MavenSession.getCurrentProject() is not part of 2.0.8
try
{
Method meth = session.getClass().getMethod("getCurrentProject", new Class[0]);
return (MavenProject) meth.invoke (session );
} catch (Exception ex)
{
//just ignore, we're running in pre- 2.0.9
}
return null;
}
private Map retrieveContext( MavenSession session )
Map retrieveContext( MavenSession session )
{
if (session == null)
{
return new HashMap();
}
PluginDescriptor desc = new PluginDescriptor();
desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId ("toolchains") );
MavenProject current = getCurrentProject(session);
if ( current != null )
{
return session.getPluginContext( desc, current );
}
return new HashMap();
}
Map context = null;
public void storeToolchainToBuildContext( ToolchainPrivate toolchain,
MavenSession session )
{
Map context = retrieveContext( session );
context.put( getStorageKey( toolchain.getType() ), toolchain.getModel () );
if ( session != null )
{
PluginDescriptor desc = new PluginDescriptor();
desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( "toolchains" ) );
MavenProject current = session.getCurrentProject();
if ( current != null )
{
context = session.getPluginContext( desc, current );
}
}
return ( context != null ) ? context : new HashMap();
}
public static final String getStorageKey( String type )
{
return "toolchain-" + type; //NOI18N
return "toolchain-" + type; // NOI18N
}
private PersistedToolchains readToolchainSettings( )
throws MisconfiguredToolchainException
{
//TODO how to point to the local path?
File tch = new File( System.getProperty( "user.home" ),
".m2/toolchains.xml" );
if ( tch.exists() )
{
MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader();
InputStreamReader in = null;
try
{
in = new InputStreamReader( new BufferedInputStream( new FileInputStream( tch ) ) );
return reader.read( in );
}
catch ( Exception ex )
{
throw new MisconfiguredToolchainException( "Cannot read toolchains file at " + tch.getAbsolutePath( ),
ex );
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException ex)
{ }
}
// IOUtil.close( in );
}
}
else
{
//TODO log the fact that no toolchains file was found.
}
return null;
}
}
}

View File

@ -0,0 +1,89 @@
package org.apache.maven.toolchain;
/*
* 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.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.model.ToolchainModel;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
/**
* @author mkleint
*/
@Component( role = ToolchainManagerPrivate.class )
public class DefaultToolchainManagerPrivate
extends DefaultToolchainManager
implements ToolchainManagerPrivate
{
@Requirement
private ToolchainsBuilder toolchainsBuilder;
public ToolchainPrivate[] getToolchainsForType( String type )
throws MisconfiguredToolchainException
{
PersistedToolchains pers = toolchainsBuilder.build();
List<ToolchainPrivate> toRet = new ArrayList<ToolchainPrivate>();
if ( pers != null )
{
List<ToolchainModel> lst = pers.getToolchains();
if ( lst != null )
{
for ( ToolchainModel toolchainModel : lst )
{
ToolchainFactory fact = factories.get( toolchainModel.getType() );
if ( fact != null )
{
toRet.add( fact.createToolchain( toolchainModel ) );
}
else
{
logger.error( "Missing toolchain factory for type: " + toolchainModel.getType()
+ ". Possibly caused by misconfigured project." );
}
}
}
}
for ( ToolchainFactory toolchainFactory : factories.values() )
{
ToolchainPrivate tool = toolchainFactory.createDefaultToolchain();
if ( tool != null )
{
toRet.add( tool );
}
}
return toRet.toArray( new ToolchainPrivate[toRet.size()] );
}
public void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession session )
{
Map context = retrieveContext( session );
context.put( getStorageKey( toolchain.getType() ), toolchain.getModel() );
}
}

View File

@ -0,0 +1,85 @@
package org.apache.maven.toolchain;
/*
* 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.Reader;
import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
/**
* @author Benjamin Bentmann
*/
@Component( role = ToolchainsBuilder.class, hint = "default" )
public class DefaultToolchainsBuilder
implements ToolchainsBuilder
{
@Requirement
private Logger logger;
/**
* The path to the user's toolchains file or <code>null</code> if not configured.
*/
private File userToolchainsFile;
public PersistedToolchains build()
throws MisconfiguredToolchainException
{
PersistedToolchains toolchains = null;
if ( userToolchainsFile != null && userToolchainsFile.isFile() )
{
Reader in = null;
try
{
in = ReaderFactory.newXmlReader( userToolchainsFile );
toolchains = new MavenToolchainsXpp3Reader().read( in );
}
catch ( Exception e )
{
throw new MisconfiguredToolchainException( "Cannot read toolchains file at "
+ userToolchainsFile.getAbsolutePath(), e );
}
finally
{
IOUtil.close( in );
}
}
else if ( userToolchainsFile != null )
{
logger.debug( "Toolchains configuration was not found at " + userToolchainsFile );
}
return toolchains;
}
public void setUserToolchainsFile( File userToolchainsFile )
{
this.userToolchainsFile = userToolchainsFile;
}
}

View File

@ -0,0 +1,52 @@
package org.apache.maven.toolchain;
/*
* 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 org.apache.maven.toolchain.model.PersistedToolchains;
/**
* Builds the toolchains model from a previously configured filesystem path to the toolchains file.
* <strong>Note:</strong> This is an internal component whose interface can change without prior notice.
*
* @author Benjamin Bentmann
*/
public interface ToolchainsBuilder
{
/**
* Builds the toolchains model from the configured toolchain files.
*
* @return The toolchains model or <code>null</code> if no toolchain file was configured or the configured files do
* not exist.
* @throws MisconfiguredToolchainException If the toolchain files exist but cannot be parsed.
*/
PersistedToolchains build()
throws MisconfiguredToolchainException;
/**
* Sets the path to the file from which to read the available toolchains.
*
* @param userToolchainsFile The path to the toolchains file, may be <code>null</code> to disable parsing.
*/
void setUserToolchainsFile( File userToolchainsFile );
}

View File

@ -35,7 +35,7 @@ import org.codehaus.plexus.util.xml.Xpp3Dom;
*
* @author mkleint
*/
@Component(role=ToolchainFactory.class)
@Component( role = ToolchainFactory.class, hint = "jdk" )
public class DefaultJavaToolchainFactory
implements ToolchainFactory, LogEnabled
{