From e2e1a3830c43212fd383e56b59798fdd71853865 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Wed, 8 Jun 2005 00:59:53 +0000 Subject: [PATCH] PR: MNG-440 read settings.xml for the local repository git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@189484 13f79535-47bb-0310-9956-ffa450edef68 --- maven-artifact-ant/pom.xml | 5 ++ .../artifact/ant/AbstractArtifactTask.java | 65 ++++++++++++++++++- .../maven/artifact/ant/DependenciesTask.java | 4 +- .../apache/maven/artifact/ant/DeployTask.java | 4 +- .../maven/artifact/ant/InstallTask.java | 2 +- .../resources/META-INF/plexus/components.xml | 3 + 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/maven-artifact-ant/pom.xml b/maven-artifact-ant/pom.xml index abd77da2a3..670313a525 100755 --- a/maven-artifact-ant/pom.xml +++ b/maven-artifact-ant/pom.xml @@ -23,6 +23,11 @@ + + org.apache.maven + maven-settings + 2.0-SNAPSHOT + org.apache.maven maven-project diff --git a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java index e07b893ac0..7d8df65096 100755 --- a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java +++ b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java @@ -19,13 +19,21 @@ package org.apache.maven.artifact.ant; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.codehaus.plexus.PlexusContainerException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.embed.Embedder; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; +import java.io.FileReader; +import java.io.IOException; /** * Base class for artifact tasks. @@ -38,7 +46,9 @@ public abstract class AbstractArtifactTask { private Embedder embedder; - protected ArtifactRepository createArtifactRepository( LocalRepository repository ) + private Settings settings; + + protected ArtifactRepository createLocalArtifactRepository( LocalRepository repository ) { ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, repository.getLayout() ); @@ -49,7 +59,7 @@ public abstract class AbstractArtifactTask return new ArtifactRepository( "local", "file://" + repository.getLocation(), repositoryLayout ); } - protected ArtifactRepository createArtifactRepository( RemoteRepository repository ) + protected ArtifactRepository createRemoteArtifactRepository( RemoteRepository repository ) { ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, repository.getLayout() ); @@ -114,8 +124,57 @@ public abstract class AbstractArtifactTask protected LocalRepository getDefaultLocalRepository() { + Settings settings = getSettings(); LocalRepository localRepository = new LocalRepository(); - localRepository.setLocation( new File( System.getProperty( "user.home" ), ".m2/repository" ) ); + localRepository.setLocation( new File( settings.getLocalRepository() ) ); return localRepository; } + + protected synchronized Settings getSettings() + { + if ( settings == null ) + { + settings = new Settings(); + + File settingsFile = new File( System.getProperty( "user.home" ), ".ant/settings.xml" ); + if ( !settingsFile.exists() ) + { + settingsFile = new File( System.getProperty( "user.home" ), ".m2/settings.xml" ); + } + + if ( settingsFile.exists() ) + { + FileReader reader = null; + try + { + reader = new FileReader( settingsFile ); + + SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); + + settings = modelReader.read( reader ); + } + catch ( IOException e ) + { + log( "Error reading settings file '" + settingsFile + "' - ignoring. Error was: " + e.getMessage(), + Project.MSG_WARN ); + } + catch ( XmlPullParserException e ) + { + log( "Error parsing settings file '" + settingsFile + "' - ignoring. Error was: " + e.getMessage(), + Project.MSG_WARN ); + } + finally + { + IOUtil.close( reader ); + } + } + + if ( StringUtils.isEmpty( settings.getLocalRepository() ) ) + { + String location = new File( System.getProperty( "user.home" ), ".m2/repository" ).getAbsolutePath(); + settings.setLocalRepository( location ); + } + } + return settings; + } } diff --git a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DependenciesTask.java b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DependenciesTask.java index 86a2785e99..e2d4ae0c69 100755 --- a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DependenciesTask.java +++ b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DependenciesTask.java @@ -64,7 +64,7 @@ public class DependenciesTask localRepository = getDefaultLocalRepository(); } - ArtifactRepository localRepo = createArtifactRepository( localRepository ); + ArtifactRepository localRepo = createLocalArtifactRepository( localRepository ); ArtifactResolver resolver = (ArtifactResolver) lookup( ArtifactResolver.ROLE ); MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); @@ -157,7 +157,7 @@ public class DependenciesTask List list = new ArrayList(); for ( Iterator i = getRemoteRepositories().iterator(); i.hasNext(); ) { - list.add( createArtifactRepository( (RemoteRepository) i.next() ) ); + list.add( createRemoteArtifactRepository( (RemoteRepository) i.next() ) ); } return list; } diff --git a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DeployTask.java b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DeployTask.java index 014022f33e..bac02604a6 100755 --- a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DeployTask.java +++ b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/DeployTask.java @@ -52,10 +52,10 @@ public class DeployTask localRepository = getDefaultLocalRepository(); } - ArtifactRepository localRepo = createArtifactRepository( localRepository ); + ArtifactRepository localRepo = createLocalArtifactRepository( localRepository ); pom.initialise( (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ), localRepo ); - ArtifactRepository deploymentRepository = createArtifactRepository( remoteRepository ); + ArtifactRepository deploymentRepository = createRemoteArtifactRepository( remoteRepository ); // Deploy the POM Artifact artifact = new DefaultArtifact( pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), diff --git a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/InstallTask.java b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/InstallTask.java index 3e407fb21c..d4ba6329fa 100755 --- a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/InstallTask.java +++ b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/InstallTask.java @@ -51,7 +51,7 @@ public class InstallTask localRepository = getDefaultLocalRepository(); } - ArtifactRepository localRepo = createArtifactRepository( localRepository ); + ArtifactRepository localRepo = createLocalArtifactRepository( localRepository ); pom.initialise( (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ), localRepo ); Artifact artifact = new DefaultArtifact( pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), diff --git a/maven-artifact-ant/src/main/resources/META-INF/plexus/components.xml b/maven-artifact-ant/src/main/resources/META-INF/plexus/components.xml index 6a04c4aa7b..e05f5acf8a 100644 --- a/maven-artifact-ant/src/main/resources/META-INF/plexus/components.xml +++ b/maven-artifact-ant/src/main/resources/META-INF/plexus/components.xml @@ -51,6 +51,9 @@ org.apache.maven.artifact.transform.ArtifactTransformation artifactTransformations + + org.apache.maven.artifact.factory.ArtifactFactory +