diff --git a/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java b/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java index 9096741495..32ee7e78f1 100644 --- a/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java +++ b/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java @@ -18,7 +18,8 @@ public class DefaultBuildContextManager protected static final String BUILD_CONTEXT_MAP_KEY = ROLE + ":" + ROLE_HINT + ":contextMap"; - private InheritableThreadLocal tl = new InheritableThreadLocal(); + // NOTE: this needs to be static so it can be found by new ctxMgr instances. + private static InheritableThreadLocal tl = new InheritableThreadLocal(); public DefaultBuildContextManager() { 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 e58548d0ee..5bfb066008 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 @@ -31,6 +31,7 @@ import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; +import org.apache.maven.context.BuildContextManager; import org.apache.maven.embedder.execution.MavenExecutionRequestDefaultsPopulator; import org.apache.maven.embedder.writer.WriterUtils; import org.apache.maven.execution.DefaultMavenExecutionResult; @@ -49,6 +50,7 @@ import org.apache.maven.profiles.ProfileManager; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.settings.MavenSettingsBuilder; import org.apache.maven.settings.Settings; import org.codehaus.plexus.DefaultPlexusContainer; import org.codehaus.plexus.MutablePlexusContainer; @@ -69,6 +71,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.io.Reader; import java.io.Writer; import java.net.URL; import java.util.ArrayList; @@ -144,6 +147,8 @@ public class MavenEmbedder private MavenEmbedderConfiguration embedderRequest; + private BuildContextManager buildContextManager; + // ---------------------------------------------------------------------------- // Constructors // ---------------------------------------------------------------------------- @@ -209,7 +214,7 @@ public class MavenEmbedder { return modelReader.read( new FileReader( model ) ); } - + public void writeModel( Writer writer, Model model, boolean namespaceDeclaration ) @@ -234,6 +239,112 @@ public class MavenEmbedder { return mavenProjectBuilder.build( mavenProject, localRepository, profileManager ); } + + // ---------------------------------------------------------------------- + // Settings + // ---------------------------------------------------------------------- + + public static Settings readSettingsFromFile( Reader reader ) + throws SettingsConfigurationException, MavenEmbedderException, IOException + { + return readSettingsFromFile( reader, null ); + } + + public static Settings readSettingsFromFile( Reader reader, MavenEmbedderLogger logger ) + throws SettingsConfigurationException, MavenEmbedderException, IOException + { + DefaultPlexusContainer container = null; + + try + { + try + { + container = new DefaultPlexusContainer(); + } + catch ( PlexusContainerException e ) + { + throw new MavenEmbedderException( "Error starting container.", e ); + } + + if ( logger != null ) + { + MavenEmbedderLoggerManager loggerManager = new MavenEmbedderLoggerManager( + new PlexusLoggerAdapter( + logger ) ); + + container.setLoggerManager( loggerManager ); + } + + MavenTools mavenTools; + try + { + mavenTools = (MavenTools) container.lookup( MavenTools.ROLE, container.getContainerRealm() ); + } + catch ( ComponentLookupException e ) + { + throw new MavenEmbedderException( "Error retrieving Maven tools.", e ); + } + + return mavenTools.readSettings( reader ); + } + finally + { + if ( container != null ) + { + container.dispose(); + } + } + } + + public static void writeSettings( Writer writer, Settings settings ) + throws IOException, MavenEmbedderException + { + writeSettings( writer, settings, null ); + } + + public static void writeSettings( Writer writer, Settings settings, MavenEmbedderLogger logger ) + throws IOException, MavenEmbedderException + { + DefaultPlexusContainer container = null; + + try + { + try + { + container = new DefaultPlexusContainer(); + } + catch ( PlexusContainerException e ) + { + throw new MavenEmbedderException( "Error starting container.", e ); + } + + if ( logger != null ) + { + MavenEmbedderLoggerManager loggerManager = new MavenEmbedderLoggerManager( new PlexusLoggerAdapter( logger ) ); + + container.setLoggerManager( loggerManager ); + } + + MavenTools mavenTools; + try + { + mavenTools = (MavenTools) container.lookup( MavenTools.ROLE, container.getContainerRealm() ); + } + catch ( ComponentLookupException e ) + { + throw new MavenEmbedderException( "Error retrieving Maven tools.", e ); + } + + mavenTools.writeSettings( settings, writer ); + } + finally + { + if ( container != null ) + { + container.dispose(); + } + } + } /** * mkleint: protected so that IDE integrations can selectively allow downloading artifacts @@ -312,7 +423,7 @@ public class MavenEmbedder try { - request = defaultsPopulator.populateDefaults( request ); + request = defaultsPopulator.populateDefaults( request, embedderRequest ); project = readProject( new File( request.getPomFile() ) ); //mkleint: copied from DefaultLifecycleExecutor @@ -440,6 +551,10 @@ public class MavenEmbedder public static final String DEFAULT_LAYOUT_ID = "default"; + public static final File DEFAULT_GLOBAL_SETTINGS_FILE = MavenSettingsBuilder.DEFAULT_GLOBAL_SETTINGS_FILE; + + public static final File DEFAULT_USER_SETTINGS_FILE = MavenSettingsBuilder.DEFAULT_USER_SETTINGS_FILE; + public ArtifactRepository createLocalRepository( File localRepository ) throws ComponentLookupException { @@ -562,6 +677,8 @@ public class MavenEmbedder profileManager.explicitlyDeactivate( req.getInactiveProfiles() ); mavenProjectBuilder = (MavenProjectBuilder) container.lookup( MavenProjectBuilder.ROLE ); + + buildContextManager = (BuildContextManager) container.lookup( BuildContextManager.ROLE ); // ---------------------------------------------------------------------- // Artifact related components @@ -646,6 +763,10 @@ public class MavenEmbedder { try { + buildContextManager.clearBuildContext(); + + container.release( buildContextManager ); + container.release( mavenProjectBuilder ); container.release( artifactRepositoryFactory ); @@ -671,7 +792,7 @@ public class MavenEmbedder try { - request = defaultsPopulator.populateDefaults( request ); + request = defaultsPopulator.populateDefaults( request, embedderRequest ); } catch ( MavenEmbedderException e ) { 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 28c1b2a4a1..d12b3268cf 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 @@ -23,6 +23,8 @@ import org.apache.maven.MavenTools; import org.apache.maven.SettingsConfigurationException; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; +import org.apache.maven.embedder.MavenEmbedder; +import org.apache.maven.embedder.MavenEmbedderConfiguration; import org.apache.maven.embedder.MavenEmbedderException; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.monitor.event.DefaultEventMonitor; @@ -34,7 +36,6 @@ import org.apache.maven.settings.Settings; import org.apache.maven.usability.SystemWarnings; import org.apache.maven.wagon.manager.RepositorySettings; import org.apache.maven.wagon.manager.WagonManager; -import org.apache.maven.wagon.repository.Repository; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; @@ -43,7 +44,6 @@ 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; @@ -66,7 +66,7 @@ public class DefaultMavenExecutionRequestDefaultsPopulator private WagonManager wagonManager; - public MavenExecutionRequest populateDefaults( MavenExecutionRequest request ) + public MavenExecutionRequest populateDefaults( MavenExecutionRequest request, MavenEmbedderConfiguration embedderConfiguration ) throws MavenEmbedderException { // Settings @@ -74,10 +74,27 @@ public class DefaultMavenExecutionRequestDefaultsPopulator if ( request.getSettings() == null ) { // A local repository set in the request should win over what's in a settings.xml file. + String userSettingsLocation = request.getSettingsFile(); + if ( userSettingsLocation == null ) + { + File userSettingsFile = embedderConfiguration.getUserSettingsFile(); + if ( userSettingsFile != null ) + { + userSettingsLocation = userSettingsFile.getAbsolutePath(); + } + } - File userSettingsPath = mavenTools.getUserSettingsPath( request.getSettingsFile() ); + File userSettingsPath = mavenTools.getUserSettingsPath( userSettingsLocation ); File globalSettingsFile = mavenTools.getGlobalSettingsPath(); + if ( globalSettingsFile.equals( MavenEmbedder.DEFAULT_GLOBAL_SETTINGS_FILE ) ) + { + File configGlobalSettings = embedderConfiguration.getGlobalSettingsFile(); + if ( configGlobalSettings != null ) + { + globalSettingsFile = configGlobalSettings; + } + } try { diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestDefaultsPopulator.java b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestDefaultsPopulator.java index 2c470fab67..ca2debdabc 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestDefaultsPopulator.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/MavenExecutionRequestDefaultsPopulator.java @@ -1,5 +1,6 @@ package org.apache.maven.embedder.execution; +import org.apache.maven.embedder.MavenEmbedderConfiguration; import org.apache.maven.embedder.MavenEmbedderException; import org.apache.maven.execution.MavenExecutionRequest; @@ -7,6 +8,6 @@ public interface MavenExecutionRequestDefaultsPopulator { String ROLE = MavenExecutionRequestDefaultsPopulator.class.getName(); - MavenExecutionRequest populateDefaults( MavenExecutionRequest request ) + MavenExecutionRequest populateDefaults( MavenExecutionRequest request, MavenEmbedderConfiguration embedderConfiguration ) throws MavenEmbedderException; } diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/writer/WriterUtils.java b/maven-embedder/src/main/java/org/apache/maven/embedder/writer/WriterUtils.java index 39b1e8826f..228be9f98e 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/writer/WriterUtils.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/writer/WriterUtils.java @@ -19,19 +19,15 @@ package org.apache.maven.embedder.writer; import org.apache.maven.model.Model; import org.apache.maven.model.io.jdom.MavenJDOMWriter; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.io.jdom.SettingsJDOMWriter; import org.jdom.Document; import org.jdom.Element; import org.jdom.Namespace; -import org.jdom.JDOMException; -import org.jdom.input.SAXBuilder; import org.jdom.output.Format; -import org.codehaus.plexus.util.IOUtil; import java.io.IOException; import java.io.Writer; -import java.io.OutputStreamWriter; -import java.io.InputStream; -import java.nio.channels.FileLock; //TODO: turn this into a component diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java index 4f42b88e54..0e9bda8985 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java @@ -1,14 +1,28 @@ package org.apache.maven.embedder; +import org.apache.maven.SettingsConfigurationException; import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.PluginManagerException; import org.apache.maven.project.MavenProject; +import org.apache.maven.settings.Profile; +import org.apache.maven.settings.Repository; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; +import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer; import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + import java.io.File; +import java.io.FileReader; import java.io.FileWriter; +import java.io.IOException; import java.io.Writer; import java.util.Arrays; import java.util.HashMap; @@ -16,11 +30,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; + import junit.framework.TestCase; -import org.apache.maven.artifact.handler.ArtifactHandler; -import org.apache.maven.artifact.handler.DefaultArtifactHandler; -import org.apache.maven.model.Plugin; -import org.apache.maven.plugin.PluginManagerException; public class MavenEmbedderTest extends TestCase @@ -277,6 +288,169 @@ public class MavenEmbedderTest assertEquals( "org.apache.maven.new", model.getGroupId() ); } + + // ---------------------------------------------------------------------- + // Settings-File Handling + // ---------------------------------------------------------------------- + + public void testReadSettings() + throws IOException, SettingsConfigurationException, MavenEmbedderException + { + Settings s = new Settings(); + s.setOffline( true ); + + String localRepoPath = "/path/to/local/repo"; + + s.setLocalRepository( localRepoPath ); + + File settingsFile = File.createTempFile( "embedder-test.settings.", "" ); + settingsFile.deleteOnExit(); + + FileWriter writer = null; + try + { + writer = new FileWriter( settingsFile ); + new SettingsXpp3Writer().write( writer, s ); + } + finally + { + IOUtil.close( writer ); + } + + FileReader reader = null; + try + { + reader = new FileReader( settingsFile ); + Settings result = MavenEmbedder.readSettingsFromFile( reader ); + + assertEquals( localRepoPath, result.getLocalRepository() ); + assertTrue( result.isOffline() ); + } + finally + { + IOUtil.close( reader ); + } + } + + public void testReadSettings_shouldFailToValidate() + throws IOException, SettingsConfigurationException, MavenEmbedderException + { + Settings s = new Settings(); + + Profile p = new Profile(); + + Repository r = new Repository(); + r.setUrl( "http://example.com" ); + + p.addRepository( r ); + s.addProfile( p ); + + File settingsFile = File.createTempFile( "embedder-test.settings.", "" ); + settingsFile.deleteOnExit(); + + FileWriter writer = null; + try + { + writer = new FileWriter( settingsFile ); + new SettingsXpp3Writer().write( writer, s ); + } + finally + { + IOUtil.close( writer ); + } + + FileReader reader = null; + try + { + reader = new FileReader( settingsFile ); + Settings result = MavenEmbedder.readSettingsFromFile( reader ); + + fail( "Settings should not pass validation when being read." ); + } + catch( IOException e ) + { + String message = e.getMessage(); + assertTrue( message.indexOf( "Failed to validate" ) > -1 ); + } + finally + { + IOUtil.close( reader ); + } + } + + public void testWriteSettings() + throws IOException, SettingsConfigurationException, MavenEmbedderException, XmlPullParserException + { + Settings s = new Settings(); + s.setOffline( true ); + + String localRepoPath = "/path/to/local/repo"; + + s.setLocalRepository( localRepoPath ); + + File settingsFile = File.createTempFile( "embedder-test.settings.", "" ); + settingsFile.deleteOnExit(); + + FileWriter writer = null; + try + { + writer = new FileWriter( settingsFile ); + MavenEmbedder.writeSettings( writer, s ); + } + finally + { + IOUtil.close( writer ); + } + + FileReader reader = null; + try + { + reader = new FileReader( settingsFile ); + Settings result = new SettingsXpp3Reader().read( reader ); + + assertEquals( localRepoPath, result.getLocalRepository() ); + assertTrue( result.isOffline() ); + } + finally + { + IOUtil.close( reader ); + } + } + + public void testWriteSettings_shouldFailToValidate() + throws IOException, SettingsConfigurationException, MavenEmbedderException + { + Settings s = new Settings(); + + Profile p = new Profile(); + + Repository r = new Repository(); + r.setUrl( "http://example.com" ); + + p.addRepository( r ); + s.addProfile( p ); + + File settingsFile = File.createTempFile( "embedder-test.settings.", "" ); + settingsFile.deleteOnExit(); + + FileWriter writer = null; + try + { + writer = new FileWriter( settingsFile ); + MavenEmbedder.writeSettings( writer, s ); + + fail( "Validation of settings should fail before settings are written." ); + } + catch ( IOException e ) + { + String message = e.getMessage(); + assertTrue( message.indexOf( "Failed to validate" ) > -1 ); + } + finally + { + IOUtil.close( writer ); + } + } // ---------------------------------------------------------------------- // Internal Utilities diff --git a/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java b/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java index 68a9e2e6fe..ac940cf672 100644 --- a/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java +++ b/maven-project/src/test/java/org/apache/maven/project/build/model/DefaultModelLineageBuilderTest.java @@ -39,6 +39,15 @@ public class DefaultModelLineageBuilderTest defaultLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); } + + public void tearDown() + throws Exception + { + BuildContextManager ctxMgr = (BuildContextManager) lookup( BuildContextManager.ROLE ); + ctxMgr.clearBuildContext(); + + super.tearDown(); + } public void testShouldReadSinglePomWithNoParents() throws IOException, ProjectBuildingException diff --git a/maven-settings/pom.xml b/maven-settings/pom.xml index 555ab79bea..f234ab7cb9 100644 --- a/maven-settings/pom.xml +++ b/maven-settings/pom.xml @@ -47,6 +47,11 @@ + + org.apache.maven + maven-build-context + 2.1-SNAPSHOT + org.apache.maven maven-model diff --git a/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java b/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java index 12c9081628..f6c93e8b25 100644 --- a/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java +++ b/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java @@ -16,6 +16,8 @@ package org.apache.maven.settings; * limitations under the License. */ +import org.apache.maven.context.BuildContextManager; +import org.apache.maven.settings.cache.SettingsCache; import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.IOUtil; @@ -43,21 +45,28 @@ public class DefaultMavenSettingsBuilder private SettingsValidator validator; - // ---------------------------------------------------------------------- - // MavenProfilesBuilder Implementation - // ---------------------------------------------------------------------- - + private BuildContextManager buildContextManager; + /** * @since 2.1 */ public Settings buildSettings( File userSettingsFile, File globalSettingsFile ) throws IOException, XmlPullParserException { + SettingsCache cache = SettingsCache.read( buildContextManager, userSettingsFile, globalSettingsFile ); + + if ( cache != null ) + { + return cache.getSettings(); + } + + // NOTE: We're allowing users to hang themselves here...if the global settings file is null, + // the default location is NOT read. Settings globalSettings = readSettings( globalSettingsFile ); if ( userSettingsFile == null ) { - userSettingsFile = new File( new File( System.getProperty( "user.home" ) ), ".m2/settings.xml" ); + userSettingsFile = DEFAULT_USER_SETTINGS_FILE; } Settings userSettings = readSettings( userSettingsFile ); @@ -81,6 +90,9 @@ public class DefaultMavenSettingsBuilder SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL ); activateDefaultProfiles( userSettings ); + + cache = new SettingsCache( userSettingsFile, globalSettingsFile, userSettings ); + cache.store( buildContextManager ); return userSettings; } @@ -91,7 +103,7 @@ public class DefaultMavenSettingsBuilder public Settings buildSettings() throws IOException, XmlPullParserException { - return buildSettings( new File( new File( System.getProperty( "user.home" ) ), ".m2/settings.xml" ) ); + return buildSettings( DEFAULT_USER_SETTINGS_FILE ); } /** diff --git a/maven-settings/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java b/maven-settings/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java index 7b2e8f4c5c..1625048aaa 100644 --- a/maven-settings/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java +++ b/maven-settings/src/main/java/org/apache/maven/settings/MavenSettingsBuilder.java @@ -29,6 +29,11 @@ public interface MavenSettingsBuilder { String ROLE = MavenSettingsBuilder.class.getName(); + File DEFAULT_USER_SETTINGS_FILE = new File( System.getProperty( "user.home" ), ".m2/settings.xml" ); + + File DEFAULT_GLOBAL_SETTINGS_FILE = new File( System + .getProperty( "maven.home", System.getProperty( "user.dir", "" ) ), "conf/settings.xml" ); + /** * @deprecated */ diff --git a/maven-settings/src/main/java/org/apache/maven/settings/cache/SettingsCache.java b/maven-settings/src/main/java/org/apache/maven/settings/cache/SettingsCache.java new file mode 100644 index 0000000000..35d74c4821 --- /dev/null +++ b/maven-settings/src/main/java/org/apache/maven/settings/cache/SettingsCache.java @@ -0,0 +1,103 @@ +package org.apache.maven.settings.cache; + +import org.apache.maven.context.BuildContext; +import org.apache.maven.context.BuildContextManager; +import org.apache.maven.context.ManagedBuildData; +import org.apache.maven.settings.Settings; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +public class SettingsCache + implements ManagedBuildData +{ + + private static final String BUILD_CONTEXT_KEY = SettingsCache.class.getName(); + + private static final String USER_SETTINGS_FILE = "user-settings"; + private static final String GLOBAL_SETTINGS_FILE = "global-settings"; + private static final String SETTINGS = "settings"; + + private File userSettingsFile; + private File globalSettingsFile; + + private Settings settings; + + private SettingsCache( File userSettingsFile, File globalSettingsFile ) + { + this( userSettingsFile, globalSettingsFile, null ); + } + + public SettingsCache( File userSettingsFile, File globalSettingsFile, Settings settings ) + { + this.userSettingsFile = userSettingsFile; + this.globalSettingsFile = globalSettingsFile; + this.settings = settings; + } + + public File getUserSettingsSource() + { + return userSettingsFile; + } + + public File getGlobalSettingsSource() + { + return globalSettingsFile; + } + + public Settings getSettings() + { + return settings; + } + + public Map getData() + { + Map data = new HashMap( 3 ); + + data.put( USER_SETTINGS_FILE, userSettingsFile ); + data.put( GLOBAL_SETTINGS_FILE, globalSettingsFile ); + data.put( SETTINGS, settings ); + + return data; + } + + public String getStorageKey() + { + return BUILD_CONTEXT_KEY + "/" + String.valueOf( userSettingsFile ) + "/" + String.valueOf( globalSettingsFile ); + } + + public void setData( Map data ) + { + this.userSettingsFile = (File) data.get( USER_SETTINGS_FILE ); + this.globalSettingsFile = (File) data.get( GLOBAL_SETTINGS_FILE ); + this.settings = (Settings) data.get( SETTINGS ); + } + + public void store( BuildContextManager buildContextManager ) + { + BuildContext buildContext = buildContextManager.readBuildContext( true ); + buildContext.store( this ); + buildContextManager.storeBuildContext( buildContext ); + } + + public static SettingsCache read( BuildContextManager buildContextManager, File userSettingsFile ) + { + return read( buildContextManager, userSettingsFile, null ); + } + + public static SettingsCache read( BuildContextManager buildContextManager, File userSettingsFile, File globalSettingsFile ) + { + BuildContext buildContext = buildContextManager.readBuildContext( true ); + + SettingsCache cache = new SettingsCache( userSettingsFile, globalSettingsFile ); + + if ( !buildContext.retrieve( cache ) ) + { + return null; + } + + return cache; + } + +} diff --git a/maven-settings/src/main/resources/META-INF/plexus/components.xml b/maven-settings/src/main/resources/META-INF/plexus/components.xml index 56e31fa628..821647fec4 100644 --- a/maven-settings/src/main/resources/META-INF/plexus/components.xml +++ b/maven-settings/src/main/resources/META-INF/plexus/components.xml @@ -13,6 +13,10 @@ org.apache.maven.settings.validation.SettingsValidator + + org.apache.maven.context.BuildContextManager + default +