From 3a689d4f4d635d58c76caf4adc90bd322b6b09a5 Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Mon, 15 Dec 2008 00:20:04 +0000 Subject: [PATCH] MNG-3905: merged all the artifact/repository handling bits into MavenTools o clean up hand-written component descriptors git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@726584 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/maven/DefaultMavenTools.java | 158 +++++++++++- .../java/org/apache/maven/MavenTools.java | 12 + .../project/DefaultMavenProjectBuilder.java | 38 +-- .../project/DefaultRepositoryHelper.java | 226 ------------------ .../maven/project/RepositoryHelper.java | 23 -- .../maven/project/ProjectClasspathTest.xml | 43 ---- .../canonical/CanonicalProjectBuilderTest.xml | 37 --- 7 files changed, 189 insertions(+), 348 deletions(-) delete mode 100644 maven-project/src/main/java/org/apache/maven/project/DefaultRepositoryHelper.java delete mode 100644 maven-project/src/main/java/org/apache/maven/project/RepositoryHelper.java diff --git a/maven-project/src/main/java/org/apache/maven/DefaultMavenTools.java b/maven-project/src/main/java/org/apache/maven/DefaultMavenTools.java index 7a7613c98d..403dee4821 100644 --- a/maven-project/src/main/java/org/apache/maven/DefaultMavenTools.java +++ b/maven-project/src/main/java/org/apache/maven/DefaultMavenTools.java @@ -22,33 +22,56 @@ package org.apache.maven; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.InvalidRepositoryException; +import org.apache.maven.artifact.factory.ArtifactFactory; 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; +import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.model.DeploymentRepository; +import org.apache.maven.model.Model; import org.apache.maven.model.Repository; import org.apache.maven.model.RepositoryPolicy; import org.apache.maven.project.MissingRepositoryElementException; +import org.apache.maven.project.ProjectBuildingException; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.logging.LogEnabled; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.util.StringUtils; /** * @author Jason van Zyl */ @Component(role = MavenTools.class) public class DefaultMavenTools - implements MavenTools + implements MavenTools, LogEnabled { + @Requirement + private ArtifactFactory artifactFactory; + + @Requirement + private ArtifactResolver artifactResolver; + @Requirement private ArtifactRepositoryFactory artifactRepositoryFactory; @Requirement private ArtifactRepositoryLayout defaultArtifactRepositoryLayout; + + @Requirement + private Logger logger; + + private static HashMap cache = new HashMap(); // ---------------------------------------------------------------------------- // Code snagged from ProjectUtils: this will have to be moved somewhere else @@ -219,5 +242,136 @@ public class DefaultMavenTools public void setGlobalChecksumPolicy( String policy ) { artifactRepositoryFactory.setGlobalChecksumPolicy( policy ); - } + } + + // Taken from RepositoryHelper + + public void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, ArtifactRepository localRepository ) + throws ProjectBuildingException + { + + if ( cache.containsKey( artifact.getId() ) ) + { + artifact.setFile( cache.get( artifact.getId() ).getFile() ); + } + + String projectId = safeVersionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); + remoteArtifactRepositories = normalizeToArtifactRepositories( remoteArtifactRepositories, projectId ); + + Artifact projectArtifact; + + // if the artifact is not a POM, we need to construct a POM artifact based on the artifact parameter given. + if ( "pom".equals( artifact.getType() ) ) + { + projectArtifact = artifact; + } + else + { + logger.debug( "Attempting to build MavenProject instance for Artifact (" + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + ") of type: " + + artifact.getType() + "; constructing POM artifact instead." ); + + projectArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope() ); + } + + try + { + artifactResolver.resolve( projectArtifact, remoteArtifactRepositories, localRepository ); + + File file = projectArtifact.getFile(); + artifact.setFile( file ); + cache.put( artifact.getId(), artifact ); + } + catch ( ArtifactResolutionException e ) + { + throw new ProjectBuildingException( projectId, "Error getting POM for '" + projectId + "' from the repository: " + e.getMessage(), e ); + } + catch ( ArtifactNotFoundException e ) + { + throw new ProjectBuildingException( projectId, "POM '" + projectId + "' not found in repository: " + e.getMessage(), e ); + } + } + + public List buildArtifactRepositories( Model model ) + throws ProjectBuildingException + { + try + { + return buildArtifactRepositories( model.getRepositories() ); + } + catch ( InvalidRepositoryException e ) + { + String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); + + throw new ProjectBuildingException( projectId, e.getMessage(), e ); + } + } + + private List normalizeToArtifactRepositories( List remoteArtifactRepositories, String projectId ) + throws ProjectBuildingException + { + List normalized = new ArrayList( remoteArtifactRepositories.size() ); + + boolean normalizationNeeded = false; + for ( Iterator it = remoteArtifactRepositories.iterator(); it.hasNext(); ) + { + Object item = it.next(); + + if ( item instanceof ArtifactRepository ) + { + normalized.add( item ); + } + else if ( item instanceof Repository ) + { + Repository repo = (Repository) item; + try + { + item = buildArtifactRepository( repo ); + + normalized.add( item ); + normalizationNeeded = true; + } + catch ( InvalidRepositoryException e ) + { + throw new ProjectBuildingException( projectId, "Error building artifact repository for id: " + repo.getId(), e ); + } + } + else + { + throw new ProjectBuildingException( projectId, "Error building artifact repository from non-repository information item: " + item ); + } + } + + if ( normalizationNeeded ) + { + return normalized; + } + else + { + return remoteArtifactRepositories; + } + } + + private String safeVersionlessKey( String groupId, String artifactId ) + { + String gid = groupId; + + if ( StringUtils.isEmpty( gid ) ) + { + gid = "unknown"; + } + + String aid = artifactId; + + if ( StringUtils.isEmpty( aid ) ) + { + aid = "unknown"; + } + + return ArtifactUtils.versionlessKey( gid, aid ); + } + + public void enableLogging( Logger logger ) + { + this.logger = logger; + } } diff --git a/maven-project/src/main/java/org/apache/maven/MavenTools.java b/maven-project/src/main/java/org/apache/maven/MavenTools.java index 392ad916c6..0475dc6655 100644 --- a/maven-project/src/main/java/org/apache/maven/MavenTools.java +++ b/maven-project/src/main/java/org/apache/maven/MavenTools.java @@ -19,12 +19,15 @@ package org.apache.maven; * under the License. */ +import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.model.DeploymentRepository; +import org.apache.maven.model.Model; import org.apache.maven.model.Repository; +import org.apache.maven.project.ProjectBuildingException; import java.io.IOException; import java.util.List; @@ -57,4 +60,13 @@ public interface MavenTools void setGlobalUpdatePolicy( String policy ); void setGlobalChecksumPolicy( String policy ); + + // Taken from RepositoryHelper + + void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, ArtifactRepository localRepository ) + throws ProjectBuildingException; + + List buildArtifactRepositories( Model model ) + throws ProjectBuildingException; + } diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 0df24e14b0..701995d137 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -19,8 +19,20 @@ package org.apache.maven.project; * under the License. */ +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.apache.maven.MavenTools; -import org.apache.maven.shared.model.InterpolatorProperty; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.InvalidRepositoryException; @@ -41,9 +53,13 @@ import org.apache.maven.profiles.activation.ProfileActivationContext; import org.apache.maven.profiles.activation.ProfileActivationException; import org.apache.maven.profiles.build.ProfileAdvisor; import org.apache.maven.project.artifact.InvalidDependencyVersionException; -import org.apache.maven.project.builder.*; +import org.apache.maven.project.builder.Interpolator; +import org.apache.maven.project.builder.PomArtifactResolver; +import org.apache.maven.project.builder.PomInterpolatorTag; +import org.apache.maven.project.builder.ProjectBuilder; import org.apache.maven.project.validation.ModelValidationResult; import org.apache.maven.project.validation.ModelValidator; +import org.apache.maven.shared.model.InterpolatorProperty; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.LogEnabled; @@ -54,15 +70,6 @@ import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; -import java.net.URL; -import java.util.*; -import java.text.SimpleDateFormat; - /** * @version $Id$ @@ -85,9 +92,6 @@ public class DefaultMavenProjectBuilder @Requirement private ProjectBuilder projectBuilder; - - @Requirement - private RepositoryHelper repositoryHelper; private MavenXpp3Reader modelReader; @@ -135,7 +139,7 @@ public class DefaultMavenProjectBuilder public MavenProject build( File projectDescriptor, ProjectBuilderConfiguration config ) throws ProjectBuildingException { - MavenProject project = readModelFromLocalPath( "unknown", projectDescriptor, new PomArtifactResolver( config.getLocalRepository(), repositoryHelper + MavenProject project = readModelFromLocalPath( "unknown", projectDescriptor, new PomArtifactResolver( config.getLocalRepository(), mavenTools .buildArtifactRepositories( getSuperProject( config, projectDescriptor, true ).getModel() ), artifactResolver ), config ); project.setFile( projectDescriptor ); @@ -189,12 +193,12 @@ public class DefaultMavenProjectBuilder } File f = (artifact.getFile() != null) ? artifact.getFile() : new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) ); - repositoryHelper.findModelFromRepository( artifact, remoteArtifactRepositories, localRepository ); + mavenTools.findModelFromRepository( artifact, remoteArtifactRepositories, localRepository ); ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setLocalRepository( localRepository ); List artifactRepositories = new ArrayList( remoteArtifactRepositories ); - artifactRepositories.addAll( repositoryHelper.buildArtifactRepositories( getSuperProject( config, artifact.getFile(), false ).getModel() ) ); + artifactRepositories.addAll( mavenTools.buildArtifactRepositories( getSuperProject( config, artifact.getFile(), false ).getModel() ) ); project = readModelFromLocalPath( "unknown", artifact.getFile(), new PomArtifactResolver( config.getLocalRepository(), artifactRepositories, artifactResolver ), config ); project = buildWithProfiles( project.getModel(), config, artifact.getFile(), project.getParentFile(), false ); diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultRepositoryHelper.java b/maven-project/src/main/java/org/apache/maven/project/DefaultRepositoryHelper.java deleted file mode 100644 index 244ef5c54b..0000000000 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultRepositoryHelper.java +++ /dev/null @@ -1,226 +0,0 @@ -package org.apache.maven.project; - -/* - * 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.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; - -import org.apache.maven.MavenTools; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.artifact.InvalidRepositoryException; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -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.model.Model; -import org.apache.maven.model.Repository; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.profiles.build.ProfileAdvisor; -import org.codehaus.plexus.component.annotations.Component; -import org.codehaus.plexus.component.annotations.Requirement; -import org.codehaus.plexus.logging.LogEnabled; -import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; -import org.codehaus.plexus.util.StringUtils; - -/** - * This is a temporary class. These methods are originally from the DefaultMavenProjectHelper. This class will be - * eliminated when Mercury is integrated. - */ -@Component(role = RepositoryHelper.class) -public class DefaultRepositoryHelper - implements RepositoryHelper, Initializable, LogEnabled -{ - @Requirement - private ArtifactFactory artifactFactory; - - @Requirement - private ArtifactResolver artifactResolver; - - @Requirement - private MavenTools mavenTools; - - @Requirement - private ProfileAdvisor profileAdvisor; - - private Logger logger; - - public static final String MAVEN_MODEL_VERSION = "4.0.0"; - - private MavenXpp3Reader modelReader; - - private static HashMap cache = new HashMap(); - - private Logger getLogger() - { - return logger; - } - - public void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, - ArtifactRepository localRepository ) - throws ProjectBuildingException - { - - if(cache.containsKey(artifact.getId())) - { - artifact.setFile(cache.get(artifact.getId()).getFile()); - } - - String projectId = safeVersionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); - remoteArtifactRepositories = normalizeToArtifactRepositories( remoteArtifactRepositories, projectId ); - - Artifact projectArtifact; - - // if the artifact is not a POM, we need to construct a POM artifact based on the artifact parameter given. - if ( "pom".equals( artifact.getType() ) ) - { - projectArtifact = artifact; - } - else - { - getLogger().debug( "Attempting to build MavenProject instance for Artifact (" + artifact.getGroupId() + ":" + - artifact.getArtifactId() + ":" + artifact.getVersion() + ") of type: " + artifact.getType() + - "; constructing POM artifact instead." ); - - projectArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), - artifact.getVersion(), artifact.getScope() ); - } - - try - { - artifactResolver.resolve( projectArtifact, remoteArtifactRepositories, localRepository ); - - File file = projectArtifact.getFile(); - artifact.setFile( file ); - cache.put(artifact.getId(), artifact); - } - catch ( ArtifactResolutionException e ) - { - throw new ProjectBuildingException( projectId, "Error getting POM for '" + projectId + - "' from the repository: " + e.getMessage(), e ); - } - catch ( ArtifactNotFoundException e ) - { - throw new ProjectBuildingException( projectId, - "POM '" + projectId + "' not found in repository: " + e.getMessage(), - e ); - } - } - - public List buildArtifactRepositories( Model model ) - throws ProjectBuildingException - { - try - { - return mavenTools.buildArtifactRepositories( model.getRepositories() ); - } - catch ( InvalidRepositoryException e ) - { - String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); - - throw new ProjectBuildingException( projectId, e.getMessage(), e ); - } - } - - private List normalizeToArtifactRepositories( List remoteArtifactRepositories, String projectId ) - throws ProjectBuildingException - { - List normalized = new ArrayList( remoteArtifactRepositories.size() ); - - boolean normalizationNeeded = false; - for ( Iterator it = remoteArtifactRepositories.iterator(); it.hasNext(); ) - { - Object item = it.next(); - - if ( item instanceof ArtifactRepository ) - { - normalized.add( item ); - } - else if ( item instanceof Repository ) - { - Repository repo = (Repository) item; - try - { - item = mavenTools.buildArtifactRepository( repo ); - - normalized.add( item ); - normalizationNeeded = true; - } - catch ( InvalidRepositoryException e ) - { - throw new ProjectBuildingException( projectId, - "Error building artifact repository for id: " + repo.getId(), - e ); - } - } - else - { - throw new ProjectBuildingException( projectId, - "Error building artifact repository from non-repository information item: " + - item ); - } - } - - if ( normalizationNeeded ) - { - return normalized; - } - else - { - return remoteArtifactRepositories; - } - } - - private String safeVersionlessKey( String groupId, String artifactId ) - { - String gid = groupId; - - if ( StringUtils.isEmpty( gid ) ) - { - gid = "unknown"; - } - - String aid = artifactId; - - if ( StringUtils.isEmpty( aid ) ) - { - aid = "unknown"; - } - - return ArtifactUtils.versionlessKey( gid, aid ); - } - - public void initialize() - throws InitializationException - { - modelReader = new MavenXpp3Reader(); - } - - public void enableLogging( Logger logger ) - { - this.logger = logger; - } -} \ No newline at end of file diff --git a/maven-project/src/main/java/org/apache/maven/project/RepositoryHelper.java b/maven-project/src/main/java/org/apache/maven/project/RepositoryHelper.java deleted file mode 100644 index 3864d74ca5..0000000000 --- a/maven-project/src/main/java/org/apache/maven/project/RepositoryHelper.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.apache.maven.project; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.model.Model; -import org.apache.maven.profiles.activation.ProfileActivationContext; - -import java.io.File; -import java.util.LinkedHashSet; -import java.util.List; - -/** - * This is a temporary class. These methods are originally from the DefaultMavenProjectHelper. This - * class will be eliminated when Mercury is integrated. - */ -public interface RepositoryHelper -{ - void findModelFromRepository( Artifact artifact, List remoteArtifactRepositories, ArtifactRepository localRepository ) - throws ProjectBuildingException; - - List buildArtifactRepositories( Model model ) - throws ProjectBuildingException; -} diff --git a/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml b/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml index ca250a8d33..ffbaedb372 100644 --- a/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml +++ b/maven-project/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml @@ -19,41 +19,6 @@ under the License. - - - org.apache.maven.project.RepositoryHelper - org.apache.maven.project.DefaultRepositoryHelper - - - org.apache.maven.profiles.build.ProfileAdvisor - default - - - org.apache.maven.artifact.factory.ArtifactFactory - - - org.apache.maven.artifact.resolver.ArtifactResolver - default - - - org.apache.maven.MavenTools - - - - org.apache.maven.profiles.injection.ProfileInjector test @@ -80,11 +45,6 @@ under the License. org.apache.maven.project.TestArtifactResolver org.apache.maven.project.TestArtifactResolver - org.apache.maven.artifact.factory.ArtifactFactory @@ -114,9 +74,6 @@ under the License. test org.apache.maven.project.TestProjectBuilder - - org.apache.maven.project.RepositoryHelper - org.apache.maven.project.builder.ProjectBuilder diff --git a/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml b/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml index cad1bd921b..94e55d84db 100644 --- a/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml +++ b/maven-project/src/test/resources/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.xml @@ -19,40 +19,6 @@ under the License. - - org.apache.maven.project.RepositoryHelper - org.apache.maven.project.DefaultRepositoryHelper - - - org.apache.maven.profiles.build.ProfileAdvisor - - - org.apache.maven.artifact.factory.ArtifactFactory - - - org.apache.maven.artifact.resolver.ArtifactResolver - default - - - org.apache.maven.MavenTools - - - - - org.apache.maven.profiles.injection.ProfileInjector test @@ -111,9 +77,6 @@ under the License. test org.apache.maven.project.TestProjectBuilder - - org.apache.maven.project.RepositoryHelper - org.apache.maven.project.builder.ProjectBuilder