Author: mkleint Date: Thu Apr 20 13:17:27 2006 New Revision: 395691 URL: http://svn.apache.org/viewcvs?rev=395691&view=rev Log: MNG-1937, MNG-1665 allow custom configuration of embedder. correct settings location handling for embedder's project reading methods
Added: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/ContainerCustomizer.java (with props) maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/DefaultMavenEmbedRequest.java (with props) maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedRequest.java (with props) Modified: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java maven/components/trunk/maven-tools/src/main/java/org/apache/maven/DefaultMavenTools.java maven/components/trunk/maven-tools/src/main/java/org/apache/maven/MavenTools.java Added: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/ContainerCustomizer.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/ContainerCustomizer.java?rev=395691&view=auto ============================================================================== --- maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/ContainerCustomizer.java (added) +++ maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/ContainerCustomizer.java Thu Apr 20 13:17:27 2006 @@ -0,0 +1,31 @@ +package org.apache.maven.embedder; +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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 org.codehaus.plexus.PlexusContainer; + +/** + * Instances of this interface can be user upon start of the embedder to customize + * the components in the plexus container. + * @author mkleint + */ +public interface ContainerCustomizer { + /** + * callback from embedder's start() method that allows to customize the components + * in the container. + */ + void customize(PlexusContainer container); + +} Propchange: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/ContainerCustomizer.java ------------------------------------------------------------------------------ svn:eol-style = native Added: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/DefaultMavenEmbedRequest.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/DefaultMavenEmbedRequest.java?rev=395691&view=auto ============================================================================== --- maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/DefaultMavenEmbedRequest.java (added) +++ maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/DefaultMavenEmbedRequest.java Thu Apr 20 13:17:27 2006 @@ -0,0 +1,107 @@ +package org.apache.maven.embedder; +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.net.URL; +import java.util.ArrayList; +import java.util.List; +import org.apache.maven.settings.Settings; + +/** + * Default implementation of MavenEmbedRequest intefrace. + * @author mkleint + */ +public class DefaultMavenEmbedRequest implements MavenEmbedRequest { + + private List inactives; + + private List actives; + + private Settings settings; + + private File userSettings; + + private File globalSettings; + + private ContainerCustomizer customizer; + + /** Creates a new instance of DefaultMavenEmbedRequest */ + public DefaultMavenEmbedRequest() { + } + + public MavenEmbedRequest addActiveProfile(String profile) { + getActiveProfiles().add(profile); + return this; + } + + public MavenEmbedRequest addInactiveProfile(String profile) { + getInactiveProfiles().add(profile); + return this; + } + + public MavenEmbedRequest addActiveProfiles(List profiles) { + getActiveProfiles().addAll(profiles); + return this; + } + + public MavenEmbedRequest addInactiveProfiles(List profiles) { + getInactiveProfiles().addAll(profiles); + return this; + } + + public List getActiveProfiles() { + if (actives == null) { + actives = new ArrayList(); + } + return actives; + } + + public List getInactiveProfiles() { + if (inactives == null) { + inactives = new ArrayList(); + } + return inactives; + } + + public MavenEmbedRequest setUserSettingsFile(File user) { + userSettings = user; + return this; + } + + public MavenEmbedRequest setGlobalSettingsFile(File global) { + globalSettings = global; + return this; + } + + public File getUserSettingsFile() { + return userSettings; + } + + public File getGlobalSettingsFile() { + return globalSettings; + } + + public MavenEmbedRequest setConfigurationCustomizer(ContainerCustomizer customizer) { + this.customizer = customizer; + return this; + } + + public ContainerCustomizer getContainerCustomizer() { + return customizer; + } + +} Propchange: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/DefaultMavenEmbedRequest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedRequest.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedRequest.java?rev=395691&view=auto ============================================================================== --- maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedRequest.java (added) +++ maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedRequest.java Thu Apr 20 13:17:27 2006 @@ -0,0 +1,73 @@ +package org.apache.maven.embedder; +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.net.URL; +import java.util.List; +import org.apache.maven.settings.Settings; + +/** + * Configuration of embedder, used when starting up. + * @author mkleint + */ +public interface MavenEmbedRequest { + + /* + * Add profile to activate. + */ + MavenEmbedRequest addActiveProfile( String profile ); + + /* + * Add profile to inactivate. + */ + MavenEmbedRequest addInactiveProfile( String profile ); + /* + * Add a list of String instances with names of profiles to activate. + */ + MavenEmbedRequest addActiveProfiles( List profiles ); + /* + * Add a list of String instances with names of profiles to inactivate. + */ + MavenEmbedRequest addInactiveProfiles( List profiles ); + + /* + * Set location of the user settings file to use for the embedder. + */ + MavenEmbedRequest setUserSettingsFile(File user); + + /* + * Set location of the global settings file to use for the embedder. + */ + MavenEmbedRequest setGlobalSettingsFile(File global); + + /** + * Set a customizer callback implemetation that will be given a chance to modify the plexus container + * on startup. + */ + MavenEmbedRequest setConfigurationCustomizer(ContainerCustomizer customizer); + + List getActiveProfiles(); + + List getInactiveProfiles(); + + File getUserSettingsFile(); + + File getGlobalSettingsFile(); + + ContainerCustomizer getContainerCustomizer(); + +} Propchange: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedRequest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java?rev=395691&r1=395690&r2=395691&view=diff ============================================================================== --- maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java (original) +++ maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java Thu Apr 20 13:17:27 2006 @@ -43,7 +43,6 @@ import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.reactor.MavenExecutionException; -import org.apache.maven.settings.MavenSettingsBuilder; import org.apache.maven.settings.Settings; import org.apache.maven.wagon.events.TransferListener; import org.codehaus.classworlds.ClassWorld; @@ -92,7 +91,6 @@ private ArtifactRepositoryFactory artifactRepositoryFactory; - private MavenSettingsBuilder settingsBuilder; private LifecycleExecutor lifecycleExecutor; @@ -150,6 +148,7 @@ /** * This option determines whether the embedder is to be aligned to the user * installation. + * @deprecated not used */ private boolean alignWithUserInstallation; @@ -157,41 +156,65 @@ // Accessors // ---------------------------------------------------------------------- + /** + * @deprecated not used. + */ public void setInteractiveMode( boolean interactiveMode ) { this.interactiveMode = interactiveMode; } + /** + * @deprecated not used. + */ public boolean isInteractiveMode() { return interactiveMode; } + /** + * @deprecated not used. + */ public void setOffline( boolean offline ) { this.offline = offline; } + /** + * @deprecated not used. + */ public boolean isOffline() { return offline; } + /** + * @deprecated not used. + */ public void setGlobalChecksumPolicy( String globalChecksumPolicy ) { this.globalChecksumPolicy = globalChecksumPolicy; } + /** + * @deprecated not used. + */ public String getGlobalChecksumPolicy() { return globalChecksumPolicy; } + /** + * @deprecated not used. + */ public boolean isAlignWithUserInstallation() { return alignWithUserInstallation; } + /** + * @deprecated not used + */ public void setAlignWithUserInstallation( boolean alignWithUserInstallation ) { this.alignWithUserInstallation = alignWithUserInstallation; @@ -222,11 +245,17 @@ return classWorld; } + /** + * @deprecated not used. + */ public void setLocalRepositoryDirectory( File localRepositoryDirectory ) { this.localRepositoryDirectory = localRepositoryDirectory; } + /** + * @deprecated not used. + */ public File getLocalRepositoryDirectory() { return localRepositoryDirectory; @@ -429,7 +458,7 @@ public ArtifactRepository createLocalRepository( Settings settings ) { - return createLocalRepository( settings.getLocalRepository(), DEFAULT_LOCAL_REPO_ID ); + return createLocalRepository( mavenTools.getLocalRepositoryPath( settings ), DEFAULT_LOCAL_REPO_ID ); } public ArtifactRepository createLocalRepository( String url, String repositoryId ) @@ -493,7 +522,13 @@ public void start() throws MavenEmbedderException { - detectUserInstallation(); + start(new DefaultMavenEmbedRequest()); + + } + + public void start(MavenEmbedRequest req) + throws MavenEmbedderException + { // ---------------------------------------------------------------------- // Set the maven.home system property which is need by components like @@ -506,7 +541,6 @@ } embedder = new Embedder(); - if ( logger != null ) { embedder.setLoggerManager( new MavenEmbedderLoggerManager( new PlexusLoggerAdapter( logger ) ) ); @@ -522,7 +556,12 @@ } embedder.start( classWorld ); - + + if (req.getContainerCustomizer() != null) + { + req.getContainerCustomizer().customize(embedder.getContainer()); + } + // ---------------------------------------------------------------------- // Lookup each of the components we need to provide the desired // client interface. @@ -539,6 +578,10 @@ pluginDescriptorBuilder = new PluginDescriptorBuilder(); profileManager = new DefaultProfileManager( embedder.getContainer() ); + + profileManager.explicitlyActivate(req.getActiveProfiles()); + + profileManager.explicitlyDeactivate(req.getInactiveProfiles()); mavenProjectBuilder = (MavenProjectBuilder) embedder.lookup( MavenProjectBuilder.ROLE ); @@ -557,8 +600,15 @@ lifecycleExecutor = (LifecycleExecutor) embedder.lookup( LifecycleExecutor.ROLE ); wagonManager = (WagonManager) embedder.lookup( WagonManager.ROLE ); + + settings = mavenTools.buildSettings( req.getUserSettingsFile(), + req.getGlobalSettingsFile(), + null ); profileManager.loadSettingsProfiles( settings ); + + localRepository = createLocalRepository( settings ); + } catch ( PlexusContainerException e ) { @@ -571,21 +621,14 @@ catch ( ComponentLookupException e ) { throw new MavenEmbedderException( "Cannot lookup required component.", e ); - } - } - - // ---------------------------------------------------------------------- - // - // ---------------------------------------------------------------------- - - private void detectUserInstallation() - { - if ( new File( userHome, ".m2" ).exists() ) + } + catch (SettingsConfigurationException e ) { - alignWithUserInstallation = true; + throw new MavenEmbedderException( "Cannot create settings configuration", e ); } } + // ---------------------------------------------------------------------- // Lifecycle // ---------------------------------------------------------------------- @@ -599,8 +642,6 @@ embedder.release( artifactRepositoryFactory ); - embedder.release( settingsBuilder ); - embedder.release( lifecycleExecutor ); } catch ( ComponentLifecycleException e ) @@ -634,6 +675,17 @@ usePluginRegistry, pluginUpdateOverride ); } + + public Settings buildSettings( File userSettingsPath, + File globalSettingsPath, + Boolean pluginUpdateOverride ) + throws SettingsConfigurationException + { + return mavenTools.buildSettings( userSettingsPath, + globalSettingsPath, + pluginUpdateOverride ); + } + public File getUserSettingsPath( String optionalSettingsPath ) { Modified: maven/components/trunk/maven-tools/src/main/java/org/apache/maven/DefaultMavenTools.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-tools/src/main/java/org/apache/maven/DefaultMavenTools.java?rev=395691&r1=395690&r2=395691&view=diff ============================================================================== --- maven/components/trunk/maven-tools/src/main/java/org/apache/maven/DefaultMavenTools.java (original) +++ maven/components/trunk/maven-tools/src/main/java/org/apache/maven/DefaultMavenTools.java Thu Apr 20 13:17:27 2006 @@ -102,6 +102,26 @@ Boolean pluginUpdateOverride ) throws SettingsConfigurationException { + Settings settings = buildSettings(userSettingsPath, + globalSettingsPath, + pluginUpdateOverride); + if ( offline ) + { + settings.setOffline( true ); + } + + settings.setInteractiveMode( interactive ); + + settings.setUsePluginRegistry( usePluginRegistry ); + + return settings; + } + + public Settings buildSettings( File userSettingsPath, + File globalSettingsPath, + Boolean pluginUpdateOverride ) + throws SettingsConfigurationException + { Settings settings; try @@ -117,15 +137,6 @@ throw new SettingsConfigurationException( e.getMessage(), e.getDetail(), e.getLineNumber(), e.getColumnNumber() ); } - - if ( offline ) - { - settings.setOffline( true ); - } - - settings.setInteractiveMode( interactive ); - - settings.setUsePluginRegistry( usePluginRegistry ); RuntimeInfo runtimeInfo = new RuntimeInfo( settings ); Modified: maven/components/trunk/maven-tools/src/main/java/org/apache/maven/MavenTools.java URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-tools/src/main/java/org/apache/maven/MavenTools.java?rev=395691&r1=395690&r2=395691&view=diff ============================================================================== --- maven/components/trunk/maven-tools/src/main/java/org/apache/maven/MavenTools.java (original) +++ maven/components/trunk/maven-tools/src/main/java/org/apache/maven/MavenTools.java Thu Apr 20 13:17:27 2006 @@ -58,6 +58,11 @@ boolean usePluginRegistry, Boolean pluginUpdateOverride ) throws SettingsConfigurationException; + + Settings buildSettings( File userSettingsPath, + File globalSettingsPath, + Boolean pluginUpdateOverride ) + throws SettingsConfigurationException; // ---------------------------------------------------------------------------- // Methods taken from CLI