[MNG-5754] Toolchains should be read during initialization

MavenExecutionRequest has been extended with toolchains, which is filled by MavenCli
Interfaces have been extended with new methods, assuming only Maven provides implementations
This commit is contained in:
Robert Scholte 2015-01-17 15:45:53 +01:00
parent 99f763decd
commit f75008743b
5 changed files with 138 additions and 5 deletions

View File

@ -22,7 +22,9 @@ package org.apache.maven.execution;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.maven.artifact.repository.ArtifactRepository;
@ -32,6 +34,7 @@ import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.toolchain.model.ToolchainModel;
import org.eclipse.aether.DefaultRepositoryCache;
import org.eclipse.aether.RepositoryCache;
import org.eclipse.aether.repository.WorkspaceReader;
@ -140,6 +143,8 @@ public class DefaultMavenExecutionRequest
private String builderId = "singlethreaded";
private Map<String, List<ToolchainModel>> toolchains;
/**
* Suppress SNAPSHOT updates.
*
@ -1127,4 +1132,21 @@ public class DefaultMavenExecutionRequest
{
return builderId;
}
@Override
public Map<String, List<ToolchainModel>> getToolchains()
{
if ( toolchains == null )
{
toolchains = new HashMap<String, List<ToolchainModel>>();
}
return toolchains;
}
@Override
public MavenExecutionRequest setToolchains( Map<String, List<ToolchainModel>> toolchains )
{
this.toolchains = toolchains;
return this;
}
}

View File

@ -20,8 +20,11 @@ package org.apache.maven.execution;
*/
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.artifact.InvalidRepositoryException;
@ -34,6 +37,8 @@ import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.SettingsUtils;
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;
import org.codehaus.plexus.util.StringUtils;
@ -46,6 +51,7 @@ public class DefaultMavenExecutionRequestPopulator
@Requirement
private RepositorySystem repositorySystem;
@Override
public MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
throws MavenExecutionRequestPopulationException
{
@ -134,6 +140,29 @@ public class DefaultMavenExecutionRequestPopulator
return request;
}
@Override
public MavenExecutionRequest populateFromToolchains( MavenExecutionRequest request, PersistedToolchains toolchains )
throws MavenExecutionRequestPopulationException
{
if ( toolchains != null )
{
Map<String, List<ToolchainModel>> groupedToolchains = new HashMap<String, List<ToolchainModel>>( 2 );
for ( ToolchainModel model : toolchains.getToolchains() )
{
if ( !groupedToolchains.containsKey( model.getType() ) )
{
groupedToolchains.put( model.getType(), new ArrayList<ToolchainModel>() );
}
groupedToolchains.get( model.getType() ).add( model );
}
request.setToolchains( groupedToolchains );
}
return request;
}
private void populateDefaultPluginGroups( MavenExecutionRequest request )
{
request.addPluginGroup( "org.apache.maven.plugins" );

View File

@ -22,6 +22,7 @@ package org.apache.maven.execution;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.maven.artifact.repository.ArtifactRepository;
@ -31,6 +32,7 @@ import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.toolchain.model.ToolchainModel;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.aether.RepositoryCache;
import org.eclipse.aether.repository.WorkspaceReader;
@ -394,4 +396,19 @@ public interface MavenExecutionRequest
*/
String getBuilderId();
/**
*
* @param toolchains all toolchains grouped by type
* @return this request
* @since 3.2.6
*/
MavenExecutionRequest setToolchains( Map<String, List<ToolchainModel>> toolchains );
/**
*
* @return all toolchains grouped by type, never {@code null}
* @since 3.2.6
*/
Map<String, List<ToolchainModel>> getToolchains();
}

View File

@ -20,6 +20,7 @@ package org.apache.maven.execution;
*/
import org.apache.maven.settings.Settings;
import org.apache.maven.toolchain.model.PersistedToolchains;
/**
* Assists in populating an execution request for invocation of Maven.
@ -42,6 +43,20 @@ public interface MavenExecutionRequestPopulator
MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
throws MavenExecutionRequestPopulationException;
/**
* Copies the values from the given toolchains into the specified execution request. This method will replace any
* existing values in the execution request that are controlled by the toolchains. Hence, it is expected that this
* method is called on a new/empty execution request before the caller mutates it to fit its needs.
*
* @param request The execution request to populate, must not be {@code null}.
* @param toolchains The toolchains to copy into the execution request, may be {@code null}.
* @return The populated execution request, never {@code null}.
* @throws MavenExecutionRequestPopulationException If the execution request could not be populated.
* @since 3.2.6
*/
MavenExecutionRequest populateFromToolchains( MavenExecutionRequest request, PersistedToolchains toolchains )
throws MavenExecutionRequestPopulationException;
/**
* Injects default values like plugin groups or repositories into the specified execution request.
*

View File

@ -37,6 +37,8 @@ import org.apache.commons.cli.UnrecognizedOptionException;
import org.apache.maven.BuildAbort;
import org.apache.maven.InternalErrorException;
import org.apache.maven.Maven;
import org.apache.maven.building.FileSource;
import org.apache.maven.building.Problem;
import org.apache.maven.building.Source;
import org.apache.maven.cli.event.DefaultEventSpyContext;
import org.apache.maven.cli.event.ExecutionEventLogger;
@ -66,7 +68,11 @@ import org.apache.maven.settings.building.SettingsBuilder;
import org.apache.maven.settings.building.SettingsBuildingRequest;
import org.apache.maven.settings.building.SettingsBuildingResult;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.toolchain.MisconfiguredToolchainException;
import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest;
import org.apache.maven.toolchain.building.ToolchainsBuilder;
import org.apache.maven.toolchain.building.ToolchainsBuildingException;
import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;
@ -445,6 +451,8 @@ public class MavenCli
settingsBuilder = container.lookup( SettingsBuilder.class );
toolchainsBuilder = container.lookup( ToolchainsBuilder.class );
dispatcher = (DefaultSecDispatcher) container.lookup( SecDispatcher.class, "maven" );
return container;
@ -838,17 +846,59 @@ public class MavenCli
cliRequest.request.setGlobalToolchainsFile( globalToolchainsFile );
cliRequest.request.setUserToolchainsFile( userToolchainsFile );
// Unlike settings, the toolchains aren't built here.
// That's done by the maven-toolchains-plugin, by calling it from the project with the proper configuration
DefaultToolchainsBuildingRequest toolchainsRequest = new DefaultToolchainsBuildingRequest();
if ( globalToolchainsFile.isFile() )
{
toolchainsRequest.setGlobalToolchainsSource( new FileSource( globalToolchainsFile ) );
}
if ( userToolchainsFile.isFile() )
{
toolchainsRequest.setUserToolchainsSource( new FileSource( userToolchainsFile ) );
}
eventSpyDispatcher.onEvent( toolchainsRequest );
slf4jLogger.debug( "Reading global toolchains from "
+ getLocation( toolchainsRequest.getGlobalToolchainsSource(), globalToolchainsFile ) );
slf4jLogger.debug( "Reading user toolchains from "
+ getLocation( toolchainsRequest.getUserToolchainsSource(), userToolchainsFile ) );
ToolchainsBuildingResult toolchainsResult;
try
{
toolchainsResult = toolchainsBuilder.build( toolchainsRequest );
}
catch ( ToolchainsBuildingException e )
{
throw new MisconfiguredToolchainException( e.getMessage(), e );
}
eventSpyDispatcher.onEvent( toolchainsRequest );
executionRequestPopulator.populateFromToolchains( cliRequest.request,
toolchainsResult.getEffectiveToolchains() );
if ( !toolchainsResult.getProblems().isEmpty() && slf4jLogger.isWarnEnabled() )
{
slf4jLogger.warn( "" );
slf4jLogger.warn( "Some problems were encountered while building the effective toolchains" );
for ( Problem problem : toolchainsResult.getProblems() )
{
slf4jLogger.warn( problem.getMessage() + " @ " + problem.getLocation() );
}
slf4jLogger.warn( "" );
}
}
private Object getLocation( Source source, File file )
private Object getLocation( Source source, File defaultLocation )
{
if ( source != null )
{
return source.getLocation();
}
return file;
return defaultLocation;
}
private MavenExecutionRequest populateRequest( CliRequest cliRequest )