From f75008743b2c0fa19ca83c46535c24bb5e2c9215 Mon Sep 17 00:00:00 2001 From: Robert Scholte Date: Sat, 17 Jan 2015 15:45:53 +0100 Subject: [PATCH] [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 --- .../DefaultMavenExecutionRequest.java | 22 +++++++ ...DefaultMavenExecutionRequestPopulator.java | 29 +++++++++ .../execution/MavenExecutionRequest.java | 17 ++++++ .../MavenExecutionRequestPopulator.java | 15 +++++ .../java/org/apache/maven/cli/MavenCli.java | 60 +++++++++++++++++-- 5 files changed, 138 insertions(+), 5 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java index b417900dac..d88024d390 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java @@ -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; @@ -139,6 +142,8 @@ public class DefaultMavenExecutionRequest private int degreeOfConcurrency = 1; private String builderId = "singlethreaded"; + + private Map> toolchains; /** * Suppress SNAPSHOT updates. @@ -1127,4 +1132,21 @@ public class DefaultMavenExecutionRequest { return builderId; } + + @Override + public Map> getToolchains() + { + if ( toolchains == null ) + { + toolchains = new HashMap>(); + } + return toolchains; + } + + @Override + public MavenExecutionRequest setToolchains( Map> toolchains ) + { + this.toolchains = toolchains; + return this; + } } diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequestPopulator.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequestPopulator.java index de5fa69589..4d79e14c62 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequestPopulator.java +++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequestPopulator.java @@ -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> groupedToolchains = new HashMap>( 2 ); + + for ( ToolchainModel model : toolchains.getToolchains() ) + { + if ( !groupedToolchains.containsKey( model.getType() ) ) + { + groupedToolchains.put( model.getType(), new ArrayList() ); + } + + groupedToolchains.get( model.getType() ).add( model ); + } + + request.setToolchains( groupedToolchains ); + } + return request; + } + private void populateDefaultPluginGroups( MavenExecutionRequest request ) { request.addPluginGroup( "org.apache.maven.plugins" ); diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java index 7daac890b2..15e20825e2 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java @@ -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> toolchains ); + + /** + * + * @return all toolchains grouped by type, never {@code null} + * @since 3.2.6 + */ + Map> getToolchains(); + } diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequestPopulator.java b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequestPopulator.java index 7c20cb8389..8eb805c87a 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequestPopulator.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequestPopulator.java @@ -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. * diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index c7b048526e..13108f29cf 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -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; @@ -837,18 +845,60 @@ 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 )