From e9b6657f4c12099ebf8098b6032e1ef8b699130c Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Fri, 7 Sep 2007 07:54:11 +0000 Subject: [PATCH] o scrub of the settings building, was able to reduce to the need of the build context and use the execution request directly. eventually i will get it to be the session, along with the profile tools, then all the tools can also share a common interpolator, which can then be shared by other components instead of having 5 interpolators lying around causing a great deal of inconsistency. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@573494 13f79535-47bb-0310-9956-ffa450edef68 --- .../DefaultMavenExecutionRequest.java | 38 +++- .../execution/MavenExecutionRequest.java | 6 + .../settings/DefaultMavenSettingsBuilder.java | 102 +++++----- .../maven/settings/MavenSettingsBuilder.java | 20 +- .../apache/maven/settings/RuntimeInfo.java | 180 ----------------- .../apache/maven/settings/SettingsUtils.java | 27 +-- maven-core/src/main/mdo/settings.mdo | 12 -- .../maven/settings/SettingsUtilsTest.java | 4 - .../apache/maven/embedder/MavenEmbedder.java | 102 +--------- ...DefaultMavenExecutionRequestPopulator.java | 182 ++++++++++++++++-- .../MavenExecutionRequestPopulator.java | 4 +- .../resources/META-INF/plexus/components.xml | 6 +- .../embedder/MavenEmbedderExampleTest.java | 2 +- 13 files changed, 272 insertions(+), 413 deletions(-) delete mode 100644 maven-core/src/main/java/org/apache/maven/settings/RuntimeInfo.java 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 8d25fc07f5..09856cc3ee 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 @@ -32,7 +32,7 @@ import java.util.List; import java.util.Properties; /** - * @author Jason van Zyl + * @author Jason van Zyl * @version $Id$ */ public class DefaultMavenExecutionRequest @@ -60,6 +60,16 @@ public class DefaultMavenExecutionRequest private boolean isProjectPresent = true; + // ---------------------------------------------------------------------------- + // We need to allow per execution user and global settings as the embedder + // might be running in a mode where its executing many threads with totally + // different settings. + // ---------------------------------------------------------------------------- + + private File userSettingsFile; + + private File globalSettingsFile; + // ---------------------------------------------------------------------------- // Request // ---------------------------------------------------------------------------- @@ -531,4 +541,30 @@ public class DefaultMavenExecutionRequest return this; } + + // Settings files + + public File getUserSettingsFile() + { + return userSettingsFile; + } + + public MavenExecutionRequest setUserSettingsFile( File userSettingsFile ) + { + this.userSettingsFile = userSettingsFile; + + return this; + } + + public File getGlobalSettingsFile() + { + return globalSettingsFile; + } + + public MavenExecutionRequest setGlobalSettingsFile( File globalSettingsFile ) + { + this.globalSettingsFile = globalSettingsFile; + + return this; + } } 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 78153652ea..28bb52d4b2 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 @@ -189,4 +189,10 @@ public interface MavenExecutionRequest boolean isProjectPresent(); MavenExecutionRequest setProjectPresent( boolean isProjectPresent ); + + File getUserSettingsFile(); + MavenExecutionRequest setUserSettingsFile( File userSettingsFile ); + + File getGlobalSettingsFile(); + MavenExecutionRequest setGlobalSettingsFile( File globalSettingsFile ); } diff --git a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java index 2586b8f446..154466c3f4 100644 --- a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java @@ -19,8 +19,7 @@ package org.apache.maven.settings; * under the License. */ -import org.apache.maven.context.BuildContextManager; -import org.apache.maven.context.SystemBuildContext; +import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer; import org.apache.maven.settings.validation.SettingsValidationResult; @@ -49,18 +48,18 @@ public class DefaultMavenSettingsBuilder { private SettingsValidator validator; - private BuildContextManager manager; - - /** - * @since 2.1 - */ - public Settings buildSettings( File userSettingsFile, File globalSettingsFile ) + /** @since 2.1 */ + public Settings buildSettings( MavenExecutionRequest request ) throws IOException, XmlPullParserException { + File userSettingsFile = request.getUserSettingsFile(); + + File globalSettingsFile = request.getGlobalSettingsFile(); + if ( ( globalSettingsFile == null ) && ( userSettingsFile == null ) ) { getLogger().debug( - "No settings files provided, and default locations are disabled for this request. Returning empty Settings instance." ); + "No settings files provided, and default locations are disabled for this request. Returning empty Settings instance." ); return new Settings(); } @@ -82,37 +81,46 @@ public class DefaultMavenSettingsBuilder userSettings = new Settings(); } - validateSettings( globalSettings, globalSettingsFile ); + validateSettings( + globalSettings, + globalSettingsFile ); - validateSettings( userSettings, userSettingsFile ); + validateSettings( + userSettings, + userSettingsFile ); - SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL ); + SettingsUtils.merge( + userSettings, + globalSettings, + TrackableBase.GLOBAL_LEVEL ); - userSettings = interpolate( userSettings ); + userSettings = interpolate( userSettings, request ); return userSettings; } - private Settings interpolate( Settings settings ) + private Settings interpolate( Settings settings, MavenExecutionRequest request ) throws IOException, XmlPullParserException { List activeProfiles = settings.getActiveProfiles(); StringWriter writer = new StringWriter(); - - new SettingsXpp3Writer().write( writer, settings ); + + new SettingsXpp3Writer().write( + writer, + settings ); String serializedSettings = writer.toString(); - SystemBuildContext sysContext = SystemBuildContext.getSystemBuildContext( manager, true ); - RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); - interpolator.addValueSource( new PropertiesBasedValueSource( sysContext.getSystemProperties() ) ); + interpolator.addValueSource( new PropertiesBasedValueSource( request.getProperties() ) ); interpolator.addValueSource( new EnvarBasedValueSource() ); - serializedSettings = interpolator.interpolate( serializedSettings, "settings" ); + serializedSettings = interpolator.interpolate( + serializedSettings, + "settings" ); Settings result = new SettingsXpp3Reader().read( new StringReader( serializedSettings ) ); @@ -127,52 +135,44 @@ public class DefaultMavenSettingsBuilder if ( settingsFile == null ) { getLogger().debug( "Settings file is null. Returning." ); + return null; } Settings settings = null; - if ( settingsFile.exists() && settingsFile.isFile() ) + FileReader reader = null; + + try { - getLogger().debug( "Settings file is a proper file. Reading." ); + reader = new FileReader( settingsFile ); - FileReader reader = null; - try - { - reader = new FileReader( settingsFile ); + SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); - SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); + settings = modelReader.read( reader ); + } + catch ( XmlPullParserException e ) + { + getLogger().error( "Failed to read settings from: " + settingsFile + ". Throwing XmlPullParserException..." ); - settings = modelReader.read( reader ); + throw e; + } + catch ( IOException e ) + { + getLogger().error( "Failed to read settings from: " + settingsFile + ". Throwing IOException..." ); - RuntimeInfo rtInfo = new RuntimeInfo( settings ); - - rtInfo.addLocation( settingsFile.getAbsolutePath() ); - - settings.setRuntimeInfo( rtInfo ); - } - catch ( XmlPullParserException e ) - { - getLogger().error( "Failed to read settings from: " + settingsFile + ". Throwing XmlPullParserException..." ); - - throw e; - } - catch ( IOException e ) - { - getLogger().error( "Failed to read settings from: " + settingsFile + ". Throwing IOException..." ); - - throw e; - } - finally - { - IOUtil.close( reader ); - } + throw e; + } + finally + { + IOUtil.close( reader ); } return settings; } - private void validateSettings( Settings settings, File location ) + private void validateSettings( Settings settings, + File location ) throws IOException { SettingsValidationResult validationResult = validator.validate( settings ); diff --git a/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java b/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java index 7ac7045777..0bdeda0fff 100644 --- a/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java @@ -19,34 +19,20 @@ package org.apache.maven.settings; * under the License. */ +import org.apache.maven.execution.MavenExecutionRequest; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -import java.io.File; import java.io.IOException; /** - * Builder for the user or global settings. By default, the settings files are located: - * - * * @author jdcasey + * @author Jason van Zyl * @version $Id$ */ public interface MavenSettingsBuilder { String ROLE = MavenSettingsBuilder.class.getName(); - /** - * - * @param userSettingsFile - * @param globalSettingsFile - * @return a Settings object from the user and global settings file. - * @throws IOException if any - * @throws XmlPullParserException if any - * @since 2.1 - */ - Settings buildSettings( File userSettingsFile, File globalSettingsFile ) + Settings buildSettings( MavenExecutionRequest request ) throws IOException, XmlPullParserException; } diff --git a/maven-core/src/main/java/org/apache/maven/settings/RuntimeInfo.java b/maven-core/src/main/java/org/apache/maven/settings/RuntimeInfo.java deleted file mode 100644 index b8116a7dfc..0000000000 --- a/maven-core/src/main/java/org/apache/maven/settings/RuntimeInfo.java +++ /dev/null @@ -1,180 +0,0 @@ -package org.apache.maven.settings; - -/* - * 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.HashMap; -import java.util.List; -import java.util.Map; - -/** - * To handle runtime informations like local repository or profiles. - * - * @version $Id$ - */ -public class RuntimeInfo -{ - private List locations = new ArrayList(); - - // using Boolean for 3VL (null for not-set, otherwise override with value) - private Boolean pluginUpdateForced; - - // using Boolean for 3VL (null, true-to-all, false-to-all) - private Boolean applyToAllPluginUpdates; - -// private boolean pluginRegistryActive = true; - - // using Boolean for 3VL (null for not-set, otherwise override with value) -// private Boolean checkLatest; - - private Map activeProfileToSourceLevel = new HashMap(); - - private String localRepositorySourceLevel = TrackableBase.USER_LEVEL; - - private Map pluginGroupIdSourceLevels = new HashMap(); - - private final Settings settings; - - /** - * @param settings - */ - public RuntimeInfo( Settings settings ) - { - this.settings = settings; - } - - /** - * @param path - */ - public void addLocation( String path ) - { - this.locations.add( path ); - } - - /** - * @return - */ - public List getLocations() - { - return locations; - } - - /** - * @param pluginUpdateForced - */ - public void setPluginUpdateOverride( Boolean pluginUpdateForced ) - { - this.pluginUpdateForced = pluginUpdateForced; - } - - /** - * @return - */ - public Boolean getPluginUpdateOverride() - { - return pluginUpdateForced; - } - - /** - * @return - */ - public Boolean getApplyToAllPluginUpdates() - { - return applyToAllPluginUpdates; - } - - /** - * @param applyToAll - */ - public void setApplyToAllPluginUpdates( Boolean applyToAll ) - { - this.applyToAllPluginUpdates = applyToAll; - } - - /** - * @param activeProfile - * @param sourceLevel - */ - public void setActiveProfileSourceLevel( String activeProfile, String sourceLevel ) - { - activeProfileToSourceLevel.put( activeProfile, sourceLevel ); - } - - /** - * @param activeProfile - * @return - */ - public String getSourceLevelForActiveProfile( String activeProfile ) - { - String sourceLevel = (String) activeProfileToSourceLevel.get( activeProfile ); - - if ( sourceLevel != null ) - { - return sourceLevel; - } - else - { - return settings.getSourceLevel(); - } - } - - /** - * @param pluginGroupId - * @param sourceLevel - */ - public void setPluginGroupIdSourceLevel( String pluginGroupId, String sourceLevel ) - { - pluginGroupIdSourceLevels.put( pluginGroupId, sourceLevel ); - } - - /** - * @param pluginGroupId - * @return - */ - public String getSourceLevelForPluginGroupId( String pluginGroupId ) - { - String sourceLevel = (String) pluginGroupIdSourceLevels.get( pluginGroupId ); - - if ( sourceLevel != null ) - { - return sourceLevel; - } - else - { - return settings.getSourceLevel(); - } - } - - /** - * @param localRepoSourceLevel - */ - public void setLocalRepositorySourceLevel( String localRepoSourceLevel ) - { - this.localRepositorySourceLevel = localRepoSourceLevel; - } - - /** - * @return - */ - public String getLocalRepositorySourceLevel() - { - return localRepositorySourceLevel; - } -} diff --git a/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java b/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java index 3971460ab2..519c3dec98 100644 --- a/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java +++ b/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java @@ -73,27 +73,12 @@ public final class SettingsUtils if ( !dominantActiveProfiles.contains( profileId ) ) { dominantActiveProfiles.add( profileId ); - - if ( dominant.getRuntimeInfo() != null ) - { - dominant.getRuntimeInfo().setActiveProfileSourceLevel( profileId, recessiveSourceLevel ); - } } } } - if ( dominant.getRuntimeInfo() != null && recessive.getRuntimeInfo() != null ) - { - List recessiveLocations = recessive.getRuntimeInfo().getLocations(); - for ( Iterator it = recessiveLocations.iterator(); it.hasNext(); ) - { - String path = (String) it.next(); - - dominant.getRuntimeInfo().addLocation( path ); - } - } - List dominantPluginGroupIds = dominant.getPluginGroups(); + List recessivePluginGroupIds = recessive.getPluginGroups(); if ( recessivePluginGroupIds != null ) @@ -111,11 +96,6 @@ public final class SettingsUtils if ( !dominantPluginGroupIds.contains( pluginGroupId ) ) { dominantPluginGroupIds.add( pluginGroupId ); - - if ( dominant.getRuntimeInfo() != null ) - { - dominant.getRuntimeInfo().setPluginGroupIdSourceLevel( pluginGroupId, recessiveSourceLevel ); - } } } } @@ -123,11 +103,6 @@ public final class SettingsUtils if ( StringUtils.isEmpty( dominant.getLocalRepository() ) ) { dominant.setLocalRepository( recessive.getLocalRepository() ); - - if ( dominant.getRuntimeInfo() != null ) - { - dominant.getRuntimeInfo().setLocalRepositorySourceLevel( recessiveSourceLevel ); - } } shallowMergeById( dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel ); diff --git a/maven-core/src/main/mdo/settings.mdo b/maven-core/src/main/mdo/settings.mdo index 3c5293cda3..45895800d7 100644 --- a/maven-core/src/main/mdo/settings.mdo +++ b/maven-core/src/main/mdo/settings.mdo @@ -392,18 +392,6 @@ return profileMap; } - - private RuntimeInfo runtimeInfo; - - public void setRuntimeInfo( RuntimeInfo runtimeInfo ) - { - this.runtimeInfo = runtimeInfo; - } - - public RuntimeInfo getRuntimeInfo() - { - return runtimeInfo; - } ]]> diff --git a/maven-core/src/test/java/org/apache/maven/settings/SettingsUtilsTest.java b/maven-core/src/test/java/org/apache/maven/settings/SettingsUtilsTest.java index 763a7a9800..4c93c5374d 100644 --- a/maven-core/src/test/java/org/apache/maven/settings/SettingsUtilsTest.java +++ b/maven-core/src/test/java/org/apache/maven/settings/SettingsUtilsTest.java @@ -32,14 +32,10 @@ public class SettingsUtilsTest Settings dominant = new Settings(); dominant.addPluginGroup( "org.apache.maven.plugins" ); dominant.addPluginGroup( "org.codehaus.modello" ); - - dominant.setRuntimeInfo(new RuntimeInfo(dominant)); Settings recessive = new Settings(); recessive.addPluginGroup( "org.codehaus.plexus" ); - recessive.setRuntimeInfo(new RuntimeInfo(recessive)); - SettingsUtils.merge( dominant, recessive, Settings.GLOBAL_LEVEL ); List pluginGroups = dominant.getPluginGroups(); 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 f77791a83e..fcf4c239e6 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 @@ -20,14 +20,12 @@ package org.apache.maven.embedder; */ import org.apache.maven.Maven; -import org.apache.maven.settings.SettingsConfigurationException; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; 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.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; @@ -58,6 +56,7 @@ import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.MavenProjectBuildingResult; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.settings.Settings; +import org.apache.maven.settings.SettingsConfigurationException; import org.apache.maven.settings.io.jdom.SettingsJDOMWriter; import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; import org.apache.maven.settings.validation.DefaultSettingsValidator; @@ -78,7 +77,6 @@ import org.codehaus.plexus.component.repository.exception.ComponentRepositoryExc import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.logging.LoggerManager; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.jdom.Document; import org.jdom.Element; @@ -406,7 +404,7 @@ public class MavenEmbedder try { - request = populator.populateDefaults( request, this ); + request = populator.populateDefaults( request, configuration ); // This is necessary to make the MavenEmbedderProjectWithExtensionReadingTest work which uses // a custom type for a dependency like this: @@ -654,7 +652,7 @@ public class MavenEmbedder // simply cascade values in from requests used for individual executions. request = new DefaultMavenExecutionRequest(); - populator.populateDefaults( request, this ); + populator.populateDefaults( request, configuration ); } catch ( ComponentLookupException e ) { @@ -785,98 +783,6 @@ public class MavenEmbedder return result; } - // ---------------------------------------------------------------------- - // Local Repository - // ---------------------------------------------------------------------- - - public ArtifactRepository createLocalRepository( Settings settings ) - throws MavenEmbedderException - { - String localRepositoryPath = null; - - if ( configuration.getLocalRepository() != null ) - { - localRepositoryPath = configuration.getLocalRepository().getAbsolutePath(); - } - - if ( StringUtils.isEmpty( localRepositoryPath ) ) - { - localRepositoryPath = settings.getLocalRepository(); - } - - if ( StringUtils.isEmpty( localRepositoryPath ) ) - { - localRepositoryPath = MavenEmbedder.defaultUserLocalRepository.getAbsolutePath(); - } - - return createLocalRepository( localRepositoryPath, MavenEmbedder.DEFAULT_LOCAL_REPO_ID ); - } - - public ArtifactRepository createLocalRepository( String url, - String repositoryId ) - throws MavenEmbedderException - { - try - { - return createRepository( canonicalFileUrl( url ), repositoryId ); - } - catch ( IOException e ) - { - throw new MavenEmbedderException( "Unable to resolve canonical path for local repository " + url, e ); - } - } - - private String canonicalFileUrl( String url ) - throws IOException - { - if ( !url.startsWith( "file:" ) ) - { - url = "file://" + url; - } - else if ( url.startsWith( "file:" ) && !url.startsWith( "file://" ) ) - { - url = "file://" + url.substring( "file:".length() ); - } - - // So now we have an url of the form file:// - - // We want to eliminate any relative path nonsense and lock down the path so we - // need to fully resolve it before any sub-modules use the path. This can happen - // when you are using a custom settings.xml that contains a relative path entry - // for the local repository setting. - - File localRepository = new File( url.substring( "file://".length() ) ); - - if ( !localRepository.isAbsolute() ) - { - url = "file://" + localRepository.getCanonicalPath(); - } - - return url; - } - - public ArtifactRepository createRepository( String url, - String repositoryId ) - { - // snapshots vs releases - // offline = to turning the update policy off - - //TODO: we'll need to allow finer grained creation of repositories but this will do for now - - String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS; - - String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN; - - ArtifactRepositoryPolicy snapshotsPolicy = - new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag ); - - ArtifactRepositoryPolicy releasesPolicy = - new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag ); - - return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout, - snapshotsPolicy, releasesPolicy ); - } - // ---------------------------------------------------------------------------- // Configuration // ---------------------------------------------------------------------------- @@ -902,7 +808,7 @@ public class MavenEmbedder try { - request = populator.populateDefaults( request, this ); + request = populator.populateDefaults( request, configuration ); } catch ( MavenEmbedderException e ) { diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java index 820e8ed86f..6f776f039d 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java @@ -20,11 +20,12 @@ package org.apache.maven.embedder.execution; */ import org.apache.maven.Maven; -import org.apache.maven.settings.SettingsConfigurationException; 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.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.embedder.Configuration; import org.apache.maven.embedder.MavenEmbedder; import org.apache.maven.embedder.MavenEmbedderException; import org.apache.maven.execution.MavenExecutionRequest; @@ -38,6 +39,7 @@ 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.settings.SettingsConfigurationException; import org.apache.maven.settings.SettingsUtils; import org.apache.maven.wagon.repository.RepositoryPermissions; import org.codehaus.plexus.PlexusConstants; @@ -49,9 +51,11 @@ import org.codehaus.plexus.context.ContextException; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; +import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; import java.io.File; +import java.io.IOException; import java.util.Iterator; import java.util.List; @@ -62,7 +66,8 @@ import java.util.List; */ public class DefaultMavenExecutionRequestPopulator extends AbstractLogEnabled - implements MavenExecutionRequestPopulator, Contextualizable + implements MavenExecutionRequestPopulator, + Contextualizable { private ArtifactRepositoryFactory artifactRepositoryFactory; @@ -75,33 +80,41 @@ public class DefaultMavenExecutionRequestPopulator private MavenSettingsBuilder settingsBuilder; public MavenExecutionRequest populateDefaults( MavenExecutionRequest request, - MavenEmbedder embedder ) + Configuration configuration ) throws MavenEmbedderException { // Actual POM File if ( request.getPomFile() == null && request.getBaseDirectory() != null ) { - File pom = new File( request.getBaseDirectory(), Maven.RELEASE_POMv4 ); + File pom = new File( + request.getBaseDirectory(), + Maven.RELEASE_POMv4 ); if ( !pom.exists() ) { - pom = new File( request.getBaseDirectory(), Maven.POMv4 ); + pom = new File( + request.getBaseDirectory(), + Maven.POMv4 ); } request.setPomFile( pom.getAbsolutePath() ); } + request.setGlobalSettingsFile( configuration.getGlobalSettingsFile() ); + + request.setUserSettingsFile( configuration.getUserSettingsFile() ); + if ( request.getSettings() == null ) { try { request.setSettings( settingsBuilder.buildSettings( - embedder.getConfiguration().getUserSettingsFile(), - embedder.getConfiguration().getGlobalSettingsFile() ) ); + configuration.getUserSettingsFile(), + configuration.getGlobalSettingsFile() ) ); } - catch( Exception e ) + catch ( Exception e ) { request.setSettings( new Settings() ); } @@ -109,7 +122,7 @@ public class DefaultMavenExecutionRequestPopulator if ( request.getLocalRepository() == null ) { - request.setLocalRepository( embedder.createLocalRepository( request.getSettings() ) ); + request.setLocalRepository( createLocalRepository( request.getSettings(), configuration ) ); } // Repository update policies @@ -161,7 +174,9 @@ public class DefaultMavenExecutionRequestPopulator } catch ( Exception e ) { - throw new MavenEmbedderException( "Unable to configure Maven for execution", e ); + throw new MavenEmbedderException( + "Unable to configure Maven for execution", + e ); } // BaseDirectory in MavenExecutionRequest @@ -186,7 +201,9 @@ public class DefaultMavenExecutionRequestPopulator ProfileManager globalProfileManager = new DefaultProfileManager( container ); - loadSettingsProfiles( globalProfileManager, request.getSettings() ); + loadSettingsProfiles( + globalProfileManager, + request.getSettings() ); globalProfileManager.explicitlyActivate( request.getActiveProfiles() ); @@ -213,27 +230,41 @@ public class DefaultMavenExecutionRequestPopulator throw new SettingsConfigurationException( "Proxy in settings.xml has no host" ); } - wagonManager.addProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), - proxy.getPassword(), proxy.getNonProxyHosts() ); + 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.addAuthenticationInfo( + server.getId(), + server.getUsername(), + server.getPassword(), + server.getPrivateKey(), + server.getPassphrase() ); - wagonManager.addPermissionInfo( server.getId(), server.getFilePermissions(), server.getDirectoryPermissions() ); + wagonManager.addPermissionInfo( + server.getId(), + server.getFilePermissions(), + server.getDirectoryPermissions() ); if ( server.getConfiguration() != null ) { - wagonManager.addConfiguration( server.getId(), (Xpp3Dom) server.getConfiguration() ); + wagonManager.addConfiguration( + server.getId(), + (Xpp3Dom) server.getConfiguration() ); } } RepositoryPermissions defaultPermissions = new RepositoryPermissions(); - + defaultPermissions.setDirectoryMode( "775" ); defaultPermissions.setFileMode( "664" ); @@ -244,7 +275,10 @@ public class DefaultMavenExecutionRequestPopulator { Mirror mirror = (Mirror) i.next(); - wagonManager.addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() ); + wagonManager.addMirror( + mirror.getId(), + mirror.getMirrorOf(), + mirror.getUrl() ); } } finally @@ -263,7 +297,8 @@ public class DefaultMavenExecutionRequestPopulator container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); } - public void loadSettingsProfiles( ProfileManager profileManager, Settings settings ) + public void loadSettingsProfiles( ProfileManager profileManager, + Settings settings ) { List settingsProfiles = settings.getProfiles(); @@ -284,4 +319,111 @@ public class DefaultMavenExecutionRequestPopulator } } + // ---------------------------------------------------------------------- + // Local Repository + // ---------------------------------------------------------------------- + + public ArtifactRepository createLocalRepository( Settings settings, Configuration configuration ) + throws MavenEmbedderException + { + String localRepositoryPath = null; + + if ( configuration.getLocalRepository() != null ) + { + localRepositoryPath = configuration.getLocalRepository().getAbsolutePath(); + } + + if ( StringUtils.isEmpty( localRepositoryPath ) ) + { + localRepositoryPath = settings.getLocalRepository(); + } + + if ( StringUtils.isEmpty( localRepositoryPath ) ) + { + localRepositoryPath = MavenEmbedder.defaultUserLocalRepository.getAbsolutePath(); + } + + return createLocalRepository( + localRepositoryPath, + MavenEmbedder.DEFAULT_LOCAL_REPO_ID ); + } + + public ArtifactRepository createLocalRepository( String url, + String repositoryId ) + throws MavenEmbedderException + { + try + { + return createRepository( + canonicalFileUrl( url ), + repositoryId ); + } + catch ( IOException e ) + { + throw new MavenEmbedderException( + "Unable to resolve canonical path for local repository " + url, + e ); + } + } + + private String canonicalFileUrl( String url ) + throws IOException + { + if ( !url.startsWith( "file:" ) ) + { + url = "file://" + url; + } + else if ( url.startsWith( "file:" ) && !url.startsWith( "file://" ) ) + { + url = "file://" + url.substring( "file:".length() ); + } + + // So now we have an url of the form file:// + + // We want to eliminate any relative path nonsense and lock down the path so we + // need to fully resolve it before any sub-modules use the path. This can happen + // when you are using a custom settings.xml that contains a relative path entry + // for the local repository setting. + + File localRepository = new File( url.substring( "file://".length() ) ); + + if ( !localRepository.isAbsolute() ) + { + url = "file://" + localRepository.getCanonicalPath(); + } + + return url; + } + + public ArtifactRepository createRepository( String url, + String repositoryId ) + { + // snapshots vs releases + // offline = to turning the update policy off + + //TODO: we'll need to allow finer grained creation of repositories but this will do for now + + String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS; + + String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN; + + ArtifactRepositoryPolicy snapshotsPolicy = + new ArtifactRepositoryPolicy( + true, + updatePolicyFlag, + checksumPolicyFlag ); + + ArtifactRepositoryPolicy releasesPolicy = + new ArtifactRepositoryPolicy( + true, + updatePolicyFlag, + checksumPolicyFlag ); + + return artifactRepositoryFactory.createArtifactRepository( + repositoryId, + url, + defaultArtifactRepositoryLayout, + snapshotsPolicy, + releasesPolicy ); + } } diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestPopulator.java b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestPopulator.java index 73c5235662..4e6fc078d0 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestPopulator.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestPopulator.java @@ -19,7 +19,7 @@ package org.apache.maven.embedder.execution; * under the License. */ -import org.apache.maven.embedder.MavenEmbedder; +import org.apache.maven.embedder.Configuration; import org.apache.maven.embedder.MavenEmbedderException; import org.apache.maven.execution.MavenExecutionRequest; @@ -27,6 +27,6 @@ public interface MavenExecutionRequestPopulator { String ROLE = MavenExecutionRequestPopulator.class.getName(); - MavenExecutionRequest populateDefaults( MavenExecutionRequest request, MavenEmbedder embedder ) + MavenExecutionRequest populateDefaults( MavenExecutionRequest request, Configuration configuration ) throws MavenEmbedderException; } diff --git a/maven-embedder/src/main/resources/META-INF/plexus/components.xml b/maven-embedder/src/main/resources/META-INF/plexus/components.xml index 8f6e9cf7b0..bc361da78a 100644 --- a/maven-embedder/src/main/resources/META-INF/plexus/components.xml +++ b/maven-embedder/src/main/resources/META-INF/plexus/components.xml @@ -27,11 +27,15 @@ under the License. org.apache.maven.artifact.manager.WagonManager default + + org.apache.maven.settings.MavenSettingsBuilder + org.apache.maven.artifact.repository.ArtifactRepositoryFactory - org.apache.maven.settings.MavenSettingsBuilder + org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout + default diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderExampleTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderExampleTest.java index 578717f5f1..461decbd2a 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderExampleTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderExampleTest.java @@ -59,7 +59,7 @@ public class MavenEmbedderExampleTest if ( result.hasExceptions() ) { - // Notify user that exceptions have occured. + fail( ((Exception)result.getExceptions().get( 0 )).getMessage() ); } // ----------------------------------------------------------------------------