Adding read/writeSettings() api to the maven-tools and the embedder along with unit tests in each place, and adding a cache object for the settings that is based on the build-context.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@510335 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2007-02-22 01:01:54 +00:00
parent 086c589776
commit b8289153a1
17 changed files with 751 additions and 28 deletions

View File

@ -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()
{

View File

@ -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 )
{

View File

@ -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
{

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -47,6 +47,11 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-build-context</artifactId>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>

View File

@ -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 );
}
/**

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -13,6 +13,10 @@
<requirement>
<role>org.apache.maven.settings.validation.SettingsValidator</role>
</requirement>
<requirement>
<role>org.apache.maven.context.BuildContextManager</role>
<role-hint>default</role-hint>
</requirement>
</requirements>
<!--
<configuration>

View File

@ -9,6 +9,10 @@ import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.settings.MavenSettingsBuilder;
import org.apache.maven.settings.RuntimeInfo;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.jdom.SettingsJDOMWriter;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.apache.maven.settings.validation.SettingsValidationResult;
import org.apache.maven.settings.validation.SettingsValidator;
import org.apache.maven.model.Repository;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.RepositoryPolicy;
@ -20,9 +24,14 @@ import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
@ -39,6 +48,8 @@ public class DefaultMavenTools
private ArtifactRepositoryFactory artifactRepositoryFactory;
private MavenSettingsBuilder settingsBuilder;
private SettingsValidator settingsValidator;
private PlexusContainer container;
@ -344,4 +355,51 @@ public class DefaultMavenTools
{
container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
}
public void writeSettings( Settings settings, Writer w )
throws IOException
{
SettingsValidationResult validationResult = settingsValidator.validate( settings );
if ( validationResult.getMessageCount() > 0 )
{
throw new IOException( "Failed to validate Settings.\n" + validationResult.render("\n") );
}
Element root = new Element( "settings" );
Document doc = new Document( root );
SettingsJDOMWriter writer = new SettingsJDOMWriter();
String encoding = settings.getModelEncoding() != null ? settings.getModelEncoding() : "UTF-8";
Format format = Format.getPrettyFormat().setEncoding( encoding );
writer.write( settings, doc, w, format );
}
public Settings readSettings( Reader r )
throws IOException, SettingsConfigurationException
{
SettingsXpp3Reader reader = new SettingsXpp3Reader();
try
{
Settings settings = reader.read( r );
SettingsValidationResult validationResult = settingsValidator.validate( settings );
if ( validationResult.getMessageCount() > 0 )
{
throw new IOException( "Failed to validate Settings.\n" + validationResult.render("\n") );
}
return settings;
}
catch ( XmlPullParserException e )
{
throw new SettingsConfigurationException( "Failed to parse settings.", e );
}
}
}

View File

@ -4,9 +4,13 @@ import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.Repository;
import org.apache.maven.settings.MavenSettingsBuilder;
import org.apache.maven.settings.Settings;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.List;
/**
@ -26,9 +30,9 @@ public interface MavenTools
// Settings
// ----------------------------------------------------------------------
File defaultUserSettingsFile = new File( userMavenConfigurationHome, "settings.xml" );
File defaultUserSettingsFile = MavenSettingsBuilder.DEFAULT_USER_SETTINGS_FILE;
File defaultGlobalSettingsFile = new File( mavenHome, "conf/settings.xml" );
File defaultGlobalSettingsFile = MavenSettingsBuilder.DEFAULT_GLOBAL_SETTINGS_FILE;
String ALT_USER_SETTINGS_XML_LOCATION = "org.apache.maven.user-settings";
@ -69,7 +73,7 @@ public interface MavenTools
// ----------------------------------------------------------------------------
File getUserSettingsPath( String optionalSettingsPath );
File getGlobalSettingsPath();
String getLocalRepositoryPath( Settings settings );
@ -90,4 +94,10 @@ public interface MavenTools
ArtifactRepository buildArtifactRepository( Repository repo )
throws InvalidRepositoryException;
void writeSettings( Settings settings, Writer writer )
throws IOException;
Settings readSettings( Reader reader )
throws IOException, SettingsConfigurationException;
}

View File

@ -14,6 +14,9 @@
<requirement>
<role>org.apache.maven.settings.MavenSettingsBuilder</role>
</requirement>
<requirement>
<role>org.apache.maven.settings.validation.SettingsValidator</role>
</requirement>
</requirements>
</component>
</components>

View File

@ -0,0 +1,192 @@
package org.apache.maven;
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.PlexusTestCase;
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;
public abstract class AbstractMavenToolsTest
extends PlexusTestCase
{
private MavenTools mavenTools;
public void setUp()
throws Exception
{
super.setUp();
mavenTools = (MavenTools) lookup( MavenTools.ROLE, getMavenToolsRoleHint() );
}
protected abstract String getMavenToolsRoleHint();
public void testReadSettings()
throws IOException, SettingsConfigurationException
{
Settings s = new Settings();
s.setOffline( true );
String localRepoPath = "/path/to/local/repo";
s.setLocalRepository( localRepoPath );
File settingsFile = File.createTempFile( "mavenTools-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 = mavenTools.readSettings( reader );
assertEquals( localRepoPath, result.getLocalRepository() );
assertTrue( result.isOffline() );
}
finally
{
IOUtil.close( reader );
}
}
public void testReadSettings_shouldFailToValidate()
throws IOException, SettingsConfigurationException
{
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( "mavenTools-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 );
mavenTools.readSettings( 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, XmlPullParserException
{
Settings s = new Settings();
s.setOffline( true );
String localRepoPath = "/path/to/local/repo";
s.setLocalRepository( localRepoPath );
File settingsFile = File.createTempFile( "mavenTools-test.settings.", "" );
settingsFile.deleteOnExit();
FileWriter writer = null;
try
{
writer = new FileWriter( settingsFile );
mavenTools.writeSettings( s, writer );
}
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
{
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( "mavenTools-test.settings.", "" );
settingsFile.deleteOnExit();
FileWriter writer = null;
try
{
writer = new FileWriter( settingsFile );
mavenTools.writeSettings( s, writer );
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 );
}
}
}

View File

@ -0,0 +1,12 @@
package org.apache.maven;
public class DefaultMavenToolsTest
extends AbstractMavenToolsTest
{
protected String getMavenToolsRoleHint()
{
return "";
}
}