From cd1c9e7b297d68d196f5838014682ddc5bbdfaf1 Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Sun, 31 Dec 2006 21:56:33 +0000 Subject: [PATCH] MNG-2721: Determine a standard way to pass proxy information into the embedder http://jira.codehaus.org/browse/MNG-2721 git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@491499 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/DefaultMaven.java | 108 +------------- .../apache/maven/embedder/MavenEmbedder.java | 62 +------- ...avenExecutionRequestDefaultsPopulator.java | 133 +++++++++++++++++- 3 files changed, 133 insertions(+), 170 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 1eb24a64fe..e5bfdac6fc 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -20,16 +20,15 @@ package org.apache.maven; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.execution.BuildFailure; +import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.ReactorManager; import org.apache.maven.execution.RuntimeInformation; -import org.apache.maven.execution.MavenExecutionResult; -import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.monitor.event.DefaultEventDispatcher; @@ -49,7 +48,6 @@ import org.apache.maven.settings.Mirror; import org.apache.maven.settings.Proxy; import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; -import org.apache.maven.usability.SystemWarnings; import org.apache.maven.usability.diagnostics.ErrorDiagnostics; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; @@ -260,52 +258,6 @@ public class DefaultMaven private ReactorManager doExecute( MavenExecutionRequest request, EventDispatcher dispatcher ) throws MavenExecutionException, BuildFailureException, LifecycleExecutionException { - if ( request.getSettings().isOffline() ) - { - getLogger().info( SystemWarnings.getOfflineWarning() ); - - WagonManager wagonManager = null; - - try - { - wagonManager = (WagonManager) container.lookup( WagonManager.ROLE ); - - wagonManager.setOnline( false ); - } - catch ( ComponentLookupException e ) - { - throw new MavenExecutionException( "Cannot retrieve WagonManager in order to set offline mode.", e ); - } - finally - { - try - { - container.release( wagonManager ); - } - catch ( ComponentLifecycleException e ) - { - getLogger().warn( "Cannot release WagonManager.", e ); - } - } - } - - try - { - resolveParameters( request.getSettings() ); - } - catch ( ComponentLookupException e ) - { - throw new MavenExecutionException( "Unable to configure Maven for execution", e ); - } - catch ( ComponentLifecycleException e ) - { - throw new MavenExecutionException( "Unable to configure Maven for execution", e ); - } - catch ( SettingsConfigurationException e ) - { - throw new MavenExecutionException( "Unable to configure Maven for execution", e ); - } - ProfileManager globalProfileManager = new DefaultProfileManager( container, request.getProperties() ); globalProfileManager.loadSettingsProfiles( request.getSettings() ); @@ -583,61 +535,6 @@ public class DefaultMaven request.getStartTime() ); } - /** - * @todo [BP] this might not be required if there is a better way to pass - * them in. It doesn't feel quite right. - * @todo [JC] we should at least provide a mapping of protocol-to-proxy for - * the wagons, shouldn't we? - */ - private void resolveParameters( Settings settings ) - throws ComponentLookupException, ComponentLifecycleException, SettingsConfigurationException - { - WagonManager wagonManager = (WagonManager) container.lookup( WagonManager.ROLE ); - - try - { - Proxy proxy = settings.getActiveProxy(); - - if ( proxy != null ) - { - if ( proxy.getHost() == null ) - { - throw new SettingsConfigurationException( "Proxy in settings.xml has no host" ); - } - - wagonManager.addProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), - proxy.getPassword(), proxy.getNonProxyHosts() ); - } - - for ( Iterator i = settings.getServers().iterator(); i.hasNext(); ) - { - Server server = (Server) i.next(); - - wagonManager.addAuthenticationInfo( server.getId(), server.getUsername(), server.getPassword(), - server.getPrivateKey(), server.getPassphrase() ); - - wagonManager.addPermissionInfo( server.getId(), server.getFilePermissions(), - server.getDirectoryPermissions() ); - - if ( server.getConfiguration() != null ) - { - wagonManager.addConfiguration( server.getId(), (Xpp3Dom) server.getConfiguration() ); - } - } - - for ( Iterator i = settings.getMirrors().iterator(); i.hasNext(); ) - { - Mirror mirror = (Mirror) i.next(); - - wagonManager.addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() ); - } - } - finally - { - container.release( wagonManager ); - } - } - // ---------------------------------------------------------------------- // Lifecylce Management // ---------------------------------------------------------------------- @@ -660,6 +557,7 @@ public class DefaultMaven throw new InitializationException( "Cannot lookup logger manager.", e ); } } + // ---------------------------------------------------------------------- // Reporting / Logging // ---------------------------------------------------------------------- diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java index f7d8289c16..1bc84e7674 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java @@ -21,7 +21,6 @@ import org.apache.maven.MavenTools; import org.apache.maven.SettingsConfigurationException; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; @@ -45,9 +44,6 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.reactor.MavenExecutionException; -import org.apache.maven.settings.Mirror; -import org.apache.maven.settings.Proxy; -import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; import org.apache.maven.wagon.events.TransferListener; import org.codehaus.plexus.DefaultPlexusContainer; @@ -55,13 +51,11 @@ 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.component.repository.ComponentDescriptor; import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; -import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; @@ -72,7 +66,6 @@ import java.io.InputStreamReader; import java.io.Writer; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; /** @@ -90,8 +83,6 @@ public class MavenEmbedder private MavenProjectBuilder mavenProjectBuilder; - private WagonManager wagonManager; - private MavenXpp3Reader modelReader; private MavenXpp3Writer modelWriter; @@ -502,15 +493,12 @@ public class MavenEmbedder defaultArtifactRepositoryLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, DEFAULT_LAYOUT_ID ); - wagonManager = (WagonManager) container.lookup( WagonManager.ROLE ); - defaultsPopulator = (MavenExecutionRequestDefaultsPopulator) container.lookup( MavenExecutionRequestDefaultsPopulator.ROLE ); + // These three things can be cached for a single session of the embedder settings = mavenTools.buildSettings( req.getUserSettingsFile(), req.getGlobalSettingsFile(), false ); - resolveParameters( settings ); - localRepository = createLocalRepository( settings ); profileManager.loadSettingsProfiles( settings ); @@ -525,54 +513,6 @@ public class MavenEmbedder } } - /** - * MKLEINT: copied from DefaultMaven. the wagonManager was not injected with proxy info - * when called in non-execute mode.. - * - * @todo [BP] this might not be required if there is a better way to pass - * them in. It doesn't feel quite right. - * @todo [JC] we should at least provide a mapping of protocol-to-proxy for - * the wagons, shouldn't we? - */ - private void resolveParameters( Settings settings ) - throws SettingsConfigurationException - { - Proxy proxy = settings.getActiveProxy(); - - if ( proxy != null ) - { - if ( proxy.getHost() == null ) - { - throw new SettingsConfigurationException( "Proxy in settings.xml has no host" ); - } - wagonManager.addProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), - proxy.getPassword(), proxy.getNonProxyHosts() ); - } - - for ( Iterator i = settings.getServers().iterator(); i.hasNext(); ) - { - Server server = (Server) i.next(); - - wagonManager.addAuthenticationInfo( server.getId(), server.getUsername(), server.getPassword(), - server.getPrivateKey(), server.getPassphrase() ); - - wagonManager.addPermissionInfo( server.getId(), server.getFilePermissions(), - server.getDirectoryPermissions() ); - - if ( server.getConfiguration() != null ) - { - wagonManager.addConfiguration( server.getId(), (Xpp3Dom) server.getConfiguration() ); - } - } - - for ( Iterator i = settings.getMirrors().iterator(); i.hasNext(); ) - { - Mirror mirror = (Mirror) i.next(); - - wagonManager.addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() ); - } - } - // ---------------------------------------------------------------------- // Lifecycle // ---------------------------------------------------------------------- diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestDefaultsPopulator.java b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestDefaultsPopulator.java index 34d0e29097..d56d5b97a5 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestDefaultsPopulator.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestDefaultsPopulator.java @@ -1,24 +1,42 @@ package org.apache.maven.embedder.execution; import java.io.File; +import java.util.Iterator; import org.apache.maven.MavenTools; import org.apache.maven.SettingsConfigurationException; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.Proxy; +import org.apache.maven.settings.Server; +import org.apache.maven.settings.Mirror; +import org.apache.maven.reactor.MavenExecutionException; +import org.apache.maven.usability.SystemWarnings; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; +import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.embedder.MavenEmbedderException; import org.apache.maven.execution.MavenExecutionRequest; import org.codehaus.plexus.logging.AbstractLogEnabled; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; +import org.codehaus.plexus.context.ContextException; +import org.codehaus.plexus.context.Context; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.PlexusConstants; +import org.codehaus.plexus.util.xml.Xpp3Dom; public class DefaultMavenExecutionRequestDefaultsPopulator extends AbstractLogEnabled - implements MavenExecutionRequestDefaultsPopulator + implements MavenExecutionRequestDefaultsPopulator, Contextualizable { private MavenTools mavenTools; private ArtifactRepositoryFactory artifactRepositoryFactory; - - public MavenExecutionRequest populateDefaults(MavenExecutionRequest request) + + private PlexusContainer container; + + public MavenExecutionRequest populateDefaults(MavenExecutionRequest request) throws MavenEmbedderException { // Settings @@ -82,6 +100,113 @@ public class DefaultMavenExecutionRequestDefaultsPopulator artifactRepositoryFactory.setGlobalChecksumPolicy( request.getGlobalChecksumPolicy() ); + // Wagon + + if ( request.getSettings().isOffline() ) + { + getLogger().info( SystemWarnings.getOfflineWarning() ); + + WagonManager wagonManager = null; + + try + { + wagonManager = (WagonManager) container.lookup( WagonManager.ROLE ); + + wagonManager.setOnline( false ); + } + catch ( ComponentLookupException e ) + { + throw new MavenEmbedderException( "Cannot retrieve WagonManager in order to set offline mode.", e ); + } + finally + { + try + { + container.release( wagonManager ); + } + catch ( ComponentLifecycleException e ) + { + getLogger().warn( "Cannot release WagonManager.", e ); + } + } + } + + try + { + resolveParameters( request.getSettings() ); + } + catch ( ComponentLookupException e ) + { + throw new MavenEmbedderException( "Unable to configure Maven for execution", e ); + } + catch ( ComponentLifecycleException e ) + { + throw new MavenEmbedderException( "Unable to configure Maven for execution", e ); + } + catch ( SettingsConfigurationException e ) + { + throw new MavenEmbedderException( "Unable to configure Maven for execution", e ); + } + return request; - } + } + + private void resolveParameters( Settings settings ) + throws ComponentLookupException, ComponentLifecycleException, SettingsConfigurationException + { + WagonManager wagonManager = (WagonManager) container.lookup( WagonManager.ROLE ); + + try + { + Proxy proxy = settings.getActiveProxy(); + + if ( proxy != null ) + { + if ( proxy.getHost() == null ) + { + throw new SettingsConfigurationException( "Proxy in settings.xml has no host" ); + } + + wagonManager.addProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), + proxy.getPassword(), proxy.getNonProxyHosts() ); + } + + for ( Iterator i = settings.getServers().iterator(); i.hasNext(); ) + { + Server server = (Server) i.next(); + + wagonManager.addAuthenticationInfo( server.getId(), server.getUsername(), server.getPassword(), + server.getPrivateKey(), server.getPassphrase() ); + + wagonManager.addPermissionInfo( server.getId(), server.getFilePermissions(), + server.getDirectoryPermissions() ); + + if ( server.getConfiguration() != null ) + { + wagonManager.addConfiguration( server.getId(), (Xpp3Dom) server.getConfiguration() ); + } + } + + for ( Iterator i = settings.getMirrors().iterator(); i.hasNext(); ) + { + Mirror mirror = (Mirror) i.next(); + + wagonManager.addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() ); + } + } + finally + { + container.release( wagonManager ); + } + } + + // ---------------------------------------------------------------------------- + // Lifecycle + // ---------------------------------------------------------------------------- + + public void contextualize( Context context ) + throws ContextException + { + container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); + } }