From 10b55add40adcbb90e4efef536e163a74a27acda Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Fri, 18 Mar 2005 22:02:09 +0000 Subject: [PATCH] o Created non-abstract base class for DefaultArtifactFactory to extend, called ArtifactConstructionSupport, which provides centralized means for constructing artifacts consistently without all the maven-core specific methods. These maven-core specifics not delegate to the generic methods provided in this new class. o Adjusted the maven-archetype stuff to work with the new artifact creation/resolution/etc. methods in maven-artifact and maven-core. o Removed all direct construction of DefaultArtifact and replaced with ArtifactConstructionSupport where it would have involved putting the DefaultArtifactFactory in the plexus.xml, and where the code doesn't need dependency-oriented methods. o Archetype works now, using the example provided in plexus/plexus-site/src/site/apt/building-plexus-applications.apt git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163615 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/maven/archetype/Archetype.java | 4 +- .../maven/archetype/DefaultArchetype.java | 19 +++-- .../resources/META-INF/plexus/components.xml | 3 - .../apache/maven/archetype/ArchetypeTest.java | 14 ++-- .../maven-archetype-plugin/pom.xml | 2 +- .../archetype/MavenArchetypePlugin.java | 6 +- .../maven/artifact/DefaultArtifact.java | 6 ++ .../ArtifactConstructionSupport.java | 71 +++++++++++++++++++ .../resolver/DefaultArtifactResolver.java | 14 ++-- .../java/org/apache/maven/DefaultMaven.java | 2 +- .../factory/DefaultArtifactFactory.java | 34 +-------- .../project/DefaultMavenProjectBuilder.java | 12 +++- .../apache/maven/project/MavenProject.java | 15 ++-- .../maven/project/MavenProjectBuilder.java | 8 ++- maven-mboot2/src/main/java/MBoot.java | 1 + 15 files changed, 140 insertions(+), 71 deletions(-) create mode 100644 maven-artifact/src/main/java/org/apache/maven/artifact/construction/ArtifactConstructionSupport.java diff --git a/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/Archetype.java b/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/Archetype.java index 6367f45ff6..ac986321be 100644 --- a/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/Archetype.java +++ b/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/Archetype.java @@ -16,8 +16,8 @@ package org.apache.maven.archetype; * limitations under the License. */ +import java.util.List; import java.util.Map; -import java.util.Set; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -36,6 +36,6 @@ public interface Archetype String ARCHETYPE_POM = "pom.xml"; void createArchetype( String archetypeGroupId, String archetypeArtifactId, String archetypeVersion, - ArtifactRepository localRepository, Set remoteRepositories, Map parameters ) + ArtifactRepository localRepository, List remoteRepositories, Map parameters ) throws ArchetypeNotFoundException, ArchetypeDescriptorException, ArchetypeTemplateProcessingException; } diff --git a/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java b/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java index 9853906947..569a6e5a1c 100644 --- a/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java +++ b/maven-archetype/maven-archetype-core/src/main/java/org/apache/maven/archetype/DefaultArchetype.java @@ -26,12 +26,11 @@ import java.net.URLClassLoader; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import org.apache.maven.archetype.descriptor.ArchetypeDescriptor; import org.apache.maven.archetype.descriptor.ArchetypeDescriptorBuilder; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.manager.WagonManager; +import org.apache.maven.artifact.construction.ArtifactConstructionSupport; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolver; @@ -54,33 +53,33 @@ public class DefaultArchetype // ---------------------------------------------------------------------- private VelocityComponent velocity; - - private WagonManager wagonManager; - + private ArtifactResolver artifactResolver; // ---------------------------------------------------------------------- // Implementation // ---------------------------------------------------------------------- + + private ArtifactConstructionSupport artifactConstructionSupport = new ArtifactConstructionSupport(); // groupId = maven // artifactId = maven-foo-archetype // version = latest public void createArchetype( String archetypeGroupId, String archetypeArtifactId, String archetypeVersion, - ArtifactRepository localRepository, Set remoteRepositories, Map parameters ) + ArtifactRepository localRepository, List remoteRepositories, Map parameters ) throws ArchetypeNotFoundException, ArchetypeDescriptorException, ArchetypeTemplateProcessingException { // ---------------------------------------------------------------------- // Download the archetype // ---------------------------------------------------------------------- - Artifact archetypeJar = - wagonManager.createArtifact( archetypeGroupId, archetypeArtifactId, archetypeVersion, "jar" ); + Artifact archetypeArtifact = + artifactConstructionSupport.createArtifact( archetypeGroupId, archetypeArtifactId, archetypeVersion, Artifact.SCOPE_RUNTIME, "jar" ); try { - artifactResolver.resolve( archetypeJar, remoteRepositories, localRepository ); + artifactResolver.resolve( archetypeArtifact, remoteRepositories, localRepository ); } catch ( ArtifactResolutionException e ) { @@ -107,7 +106,7 @@ public class DefaultArchetype { URL[] urls = new URL[1]; - urls[0] = archetypeJar.getFile().toURL(); + urls[0] = archetypeArtifact.getFile().toURL(); archetypeJarLoader = new URLClassLoader( urls ); diff --git a/maven-archetype/maven-archetype-core/src/main/resources/META-INF/plexus/components.xml b/maven-archetype/maven-archetype-core/src/main/resources/META-INF/plexus/components.xml index dae9a288ac..337af97d95 100644 --- a/maven-archetype/maven-archetype-core/src/main/resources/META-INF/plexus/components.xml +++ b/maven-archetype/maven-archetype-core/src/main/resources/META-INF/plexus/components.xml @@ -7,9 +7,6 @@ org.codehaus.plexus.velocity.VelocityComponent - - org.apache.maven.artifact.manager.WagonManager - org.apache.maven.artifact.resolver.ArtifactResolver diff --git a/maven-archetype/maven-archetype-core/src/test/java/org/apache/maven/archetype/ArchetypeTest.java b/maven-archetype/maven-archetype-core/src/test/java/org/apache/maven/archetype/ArchetypeTest.java index cd405c25e2..f7224197ed 100644 --- a/maven-archetype/maven-archetype-core/src/test/java/org/apache/maven/archetype/ArchetypeTest.java +++ b/maven-archetype/maven-archetype-core/src/test/java/org/apache/maven/archetype/ArchetypeTest.java @@ -16,16 +16,16 @@ package org.apache.maven.archetype; * limitations under the License. */ -import org.codehaus.plexus.PlexusTestCase; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.codehaus.plexus.PlexusTestCase; -import java.util.Map; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; -import java.util.Properties; import java.io.File; import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; /** * @author Jason van Zyl @@ -65,7 +65,7 @@ public class ArchetypeTest ArtifactRepository localRepository = new ArtifactRepository( "local", "file://" + mavenProperties.getProperty( "maven.repo.local" ) ); - Set remoteRepositories = new HashSet(); + List remoteRepositories = new ArrayList(); ArtifactRepository remoteRepository = new ArtifactRepository( "remote", "http://repo1.maven.org" ); diff --git a/maven-archetype/maven-archetype-plugin/pom.xml b/maven-archetype/maven-archetype-plugin/pom.xml index 6edd5c7e2a..aa70968df1 100644 --- a/maven-archetype/maven-archetype-plugin/pom.xml +++ b/maven-archetype/maven-archetype-plugin/pom.xml @@ -7,7 +7,7 @@ 4.0.0 maven maven-archetype-plugin - plugin + maven-plugin Maven Archetype Plugin 1.0-SNAPSHOT diff --git a/maven-archetype/maven-archetype-plugin/src/main/java/org/apache/maven/plugin/archetype/MavenArchetypePlugin.java b/maven-archetype/maven-archetype-plugin/src/main/java/org/apache/maven/plugin/archetype/MavenArchetypePlugin.java index 737ee3dc23..04a5a765cf 100644 --- a/maven-archetype/maven-archetype-plugin/src/main/java/org/apache/maven/plugin/archetype/MavenArchetypePlugin.java +++ b/maven-archetype/maven-archetype-plugin/src/main/java/org/apache/maven/plugin/archetype/MavenArchetypePlugin.java @@ -22,8 +22,8 @@ import org.apache.maven.plugin.AbstractPlugin; import org.apache.maven.plugin.PluginExecutionRequest; import org.apache.maven.plugin.PluginExecutionResponse; -import java.util.HashSet; -import java.util.Set; +import java.util.ArrayList; +import java.util.List; /** * @goal create @@ -131,7 +131,7 @@ public class MavenArchetypePlugin ArtifactRepository localRepository = (ArtifactRepository) request.getParameter( "localRepository" ); - Set remoteRepositories = new HashSet(); + List remoteRepositories = new ArrayList(); ArtifactRepository remoteRepository = new ArtifactRepository( "remote", "http://repo1.maven.org" ); diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java b/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java index 580448cdd9..dc5f3894f4 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java @@ -25,6 +25,7 @@ import java.io.File; public class DefaultArtifact implements Artifact { + // ---------------------------------------------------------------------- // These are the only things i need to specify // ---------------------------------------------------------------------- @@ -48,6 +49,11 @@ public class DefaultArtifact */ public DefaultArtifact( String groupId, String artifactId, String version, String scope, String type, String extension ) { + if(type == null) + { + throw new NullPointerException("Artifact type cannot be null."); + } + this.groupId = groupId; this.artifactId = artifactId; diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/construction/ArtifactConstructionSupport.java b/maven-artifact/src/main/java/org/apache/maven/artifact/construction/ArtifactConstructionSupport.java new file mode 100644 index 0000000000..70c3fbd49a --- /dev/null +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/construction/ArtifactConstructionSupport.java @@ -0,0 +1,71 @@ +package org.apache.maven.artifact.construction; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; + +/* ==================================================================== + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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. + * ==================================================================== + */ + +/** + * @author jdcasey + */ +public class ArtifactConstructionSupport +{ + + public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type ) + { + return createArtifact( groupId, artifactId, version, scope, type, type, null ); + } + + public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type, String extension ) + { + return createArtifact( groupId, artifactId, version, scope, type, extension, null ); + } + + public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type, + String extension, String inheritedScope ) + { + // TODO: can refactor, use scope handler + + // if this artifact is test, and the dependency is test, don't transitively create + if ( Artifact.SCOPE_TEST.equals( inheritedScope ) && Artifact.SCOPE_TEST.equals( scope ) ) + { + return null; + } + + // TODO: localRepository not used (should be used here to resolve path? + String desiredScope = Artifact.SCOPE_RUNTIME; + if ( Artifact.SCOPE_COMPILE.equals( scope ) && inheritedScope == null ) + { + desiredScope = Artifact.SCOPE_COMPILE; + } + + // vvv added to retain compile scope. Remove if you want compile inherited as runtime + else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) ) + { + desiredScope = Artifact.SCOPE_COMPILE; + } + // ^^^ added to retain compile scope. Remove if you want compile inherited as runtime + + if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_TEST.equals( inheritedScope ) ) + { + desiredScope = Artifact.SCOPE_TEST; + } + + return new DefaultArtifact( groupId, artifactId, version, desiredScope, type, extension ); + } +} diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java index 3d482c9b84..32a5c1b3ed 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java @@ -2,7 +2,7 @@ package org.apache.maven.artifact.resolver; import org.apache.maven.artifact.AbstractArtifactComponent; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.construction.ArtifactConstructionSupport; import org.apache.maven.artifact.handler.manager.ArtifactHandlerNotFoundException; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; @@ -45,6 +45,8 @@ public class DefaultArtifactResolver // ---------------------------------------------------------------------- // Implementation // ---------------------------------------------------------------------- + + private ArtifactConstructionSupport artifactConstructionSupport = new ArtifactConstructionSupport(); public Artifact resolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) throws ArtifactResolutionException @@ -228,10 +230,12 @@ public class DefaultArtifactResolver if ( updateScope ) { // TODO: Artifact factory? - Artifact artifact = new DefaultArtifact( knownArtifact.getGroupId(), - knownArtifact.getArtifactId(), knownVersion, - newArtifact.getScope(), knownArtifact.getType(), - knownArtifact.getExtension() ); + // TODO: [jc] Is this a better way to centralize artifact construction here? + Artifact artifact = artifactConstructionSupport.createArtifact( knownArtifact.getGroupId(), + knownArtifact.getArtifactId(), + knownVersion, newArtifact.getScope(), + knownArtifact.getType(), + knownArtifact.getExtension() ); resolvedArtifacts.put( artifact.getConflictId(), artifact ); } } diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 64b3335ef6..f2a5cf3aea 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -101,7 +101,7 @@ public class DefaultMaven if ( projects.isEmpty() ) { - projects.add( projectBuilder.buildSuperProject( request.getLocalRepository() ) ); + projects.add( projectBuilder.buildStandaloneSuperProject( request.getLocalRepository() ) ); } } catch ( IOException e ) diff --git a/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java b/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java index 1908907abb..2b6248ada5 100644 --- a/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java +++ b/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java @@ -17,7 +17,7 @@ package org.apache.maven.artifact.factory; */ import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.construction.ArtifactConstructionSupport; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Dependency; @@ -29,6 +29,7 @@ import java.util.Set; // TODO: packaging is very confusing - this isn't in artifact after all public class DefaultArtifactFactory + extends ArtifactConstructionSupport implements ArtifactFactory { public Set createArtifacts( List dependencies, ArtifactRepository localRepository, String inheritedScope ) @@ -55,35 +56,4 @@ public class DefaultArtifactFactory dependency.getScope(), dependency.getType(), dependency.getType(), inheritedScope ); } - public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type, - String extension, String inheritedScope ) - { - // TODO: can refactor, use scope handler - - // if this artifact is test, and the dependency is test, don't transitively create - if ( Artifact.SCOPE_TEST.equals( inheritedScope ) && Artifact.SCOPE_TEST.equals( scope ) ) - { - return null; - } - - // TODO: localRepository not used (should be used here to resolve path? - String desiredScope = Artifact.SCOPE_RUNTIME; - if ( Artifact.SCOPE_COMPILE.equals( scope ) && inheritedScope == null ) - { - desiredScope = Artifact.SCOPE_COMPILE; - } - - // vvv added to retain compile scope. Remove if you want compile inherited as runtime - else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) ) - { - desiredScope = Artifact.SCOPE_COMPILE; - } - // ^^^ added to retain compile scope. Remove if you want compile inherited as runtime - - if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_TEST.equals( inheritedScope ) ) - { - desiredScope = Artifact.SCOPE_TEST; - } - return new DefaultArtifact( groupId, artifactId, version, desiredScope, type, extension ); - } } diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 6e2b7d4b44..fbf17acd31 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -377,10 +377,18 @@ public class DefaultMavenProjectBuilder return groupId + ":" + artifactId + ":" + version; } - public MavenProject buildSuperProject( ArtifactRepository localRepository ) + public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository ) throws ProjectBuildingException { - MavenProject project = new MavenProject( getSuperModel() ); + Model superModel = getSuperModel(); + + superModel.setGroupId( STANDALONE_SUPERPOM_GROUPID ); + + superModel.setArtifactId( STANDALONE_SUPERPOM_ARTIFACTID ); + + superModel.setVersion( STANDALONE_SUPERPOM_VERSION ); + + MavenProject project = new MavenProject( superModel ); try { diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 9fe95d513f..f5829516c0 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -18,7 +18,7 @@ package org.apache.maven.project; */ import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.construction.ArtifactConstructionSupport; import org.apache.maven.model.Build; import org.apache.maven.model.CiManagement; import org.apache.maven.model.Contributor; @@ -74,6 +74,8 @@ public class MavenProject private Set artifacts; private List collectedProjects = Collections.EMPTY_LIST; + + private ArtifactConstructionSupport artifactConstructionSupport = new ArtifactConstructionSupport(); public MavenProject( Model model ) { @@ -682,9 +684,14 @@ public class MavenProject if ( updateScope ) { // TODO: Artifact factory? - Artifact artifact = new DefaultArtifact( existing.getGroupId(), existing.getArtifactId(), - existing.getVersion(), a.getScope(), existing.getType(), - existing.getExtension() ); + // TODO: [jc] Is this a better way to centralize artifact construction here? + Artifact artifact = artifactConstructionSupport.createArtifact( existing.getGroupId(), + existing.getArtifactId(), + existing.getVersion(), + a.getScope(), + existing.getType(), + existing.getExtension() ); + artifacts.put( id, artifact ); } } diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java index 3f8b6418d3..5113b7f1ce 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java @@ -26,6 +26,12 @@ import java.io.File; public interface MavenProjectBuilder { String ROLE = MavenProjectBuilder.class.getName(); + + static final String STANDALONE_SUPERPOM_GROUPID = "maven"; + + static final String STANDALONE_SUPERPOM_ARTIFACTID = "super-pom"; + + static final String STANDALONE_SUPERPOM_VERSION = "2.0"; MavenProject build( File project, ArtifactRepository localRepository ) throws ProjectBuildingException; @@ -36,7 +42,7 @@ public interface MavenProjectBuilder MavenProject buildFromRepository( Artifact artifact, ArtifactRepository localRepository ) throws ProjectBuildingException; - MavenProject buildSuperProject( ArtifactRepository localRepository ) + MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository ) throws ProjectBuildingException; Model getCachedModel( String groupId, String artifactId, String version ); diff --git a/maven-mboot2/src/main/java/MBoot.java b/maven-mboot2/src/main/java/MBoot.java index 45cbcbaece..95f64ff952 100644 --- a/maven-mboot2/src/main/java/MBoot.java +++ b/maven-mboot2/src/main/java/MBoot.java @@ -54,6 +54,7 @@ public class MBoot String[] pluginGeneratorDeps = new String[]{"plexus/jars/plexus-container-default-1.0-alpha-2-SNAPSHOT.jar", "maven/jars/maven-core-2.0-SNAPSHOT.jar", + "maven/jars/maven-artifact-2.0-SNAPSHOT.jar", "maven/jars/maven-model-2.0-SNAPSHOT.jar", "maven/jars/maven-plugin-2.0-SNAPSHOT.jar", "maven/jars/maven-plugin-tools-api-2.0-SNAPSHOT.jar",