[ARCHETYPE-518] Archetype cannot be created from project if parent pom.xml evaluates...
authorrfscholte <rfscholte@apache.org>
Tue, 14 Feb 2017 21:43:51 +0000 (22:43 +0100)
committerrfscholte <rfscholte@apache.org>
Tue, 14 Feb 2017 21:43:51 +0000 (22:43 +0100)
Maven3 is more efficient compared to Maven2, no workarounds required

archetype-common/src/main/java/org/apache/maven/archetype/ArchetypeCreationRequest.java
archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
archetype-testing/archetype-final/src/test/java/org/apache/maven/archetype/creator/DefaultArchetypeCreatorIT.java
maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/CreateArchetypeFromProjectMojo.java

index e85e6a8..c5cc23a 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.maven.archetype;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuildingRequest;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -30,6 +31,8 @@ import java.util.Properties;
 /** @author Jason van Zyl */
 public class ArchetypeCreationRequest
 {
+    private ProjectBuildingRequest projectBuildingRequest;
+
     private ArtifactRepository localRepository;
 
     private MavenProject project;
@@ -80,6 +83,18 @@ public class ArchetypeCreationRequest
         return this;
     }
 
+    public ProjectBuildingRequest getProjectBuildingRequest()
+    {
+        return projectBuildingRequest;
+    }
+
+    public ArchetypeCreationRequest setProjectBuildingRequest( ProjectBuildingRequest projectBuildingRequest )
+    {
+        this.projectBuildingRequest = projectBuildingRequest;
+        
+        return this;
+    }
+
     public MavenProject getProject()
     {
         return project;
index 0022341..85b1359 100644 (file)
@@ -54,8 +54,6 @@ import org.apache.maven.archetype.metadata.FileSet;
 import org.apache.maven.archetype.metadata.ModuleDescriptor;
 import org.apache.maven.archetype.metadata.RequiredProperty;
 import org.apache.maven.archetype.metadata.io.xpp3.ArchetypeDescriptorXpp3Writer;
-import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.model.Build;
 import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Extension;
@@ -64,10 +62,7 @@ import org.apache.maven.model.Plugin;
 import org.apache.maven.model.PluginManagement;
 import org.apache.maven.model.Profile;
 import org.apache.maven.model.Resource;
-import org.apache.maven.project.DefaultProjectBuildingRequest;
 import org.apache.maven.project.MavenProject;
-import org.apache.maven.project.ProjectBuilder;
-import org.apache.maven.project.ProjectBuildingException;
 import org.apache.maven.project.ProjectBuildingRequest;
 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
 import org.apache.maven.shared.invoker.InvocationRequest;
@@ -102,9 +97,6 @@ public class FilesetArchetypeCreator
 
     @Requirement
     private PomManager pomManager;
-
-    @Requirement
-    private ProjectBuilder projectBuilder;
     
     @Requirement
     private Invoker invoker;
@@ -118,7 +110,6 @@ public class FilesetArchetypeCreator
         boolean preserveCData = request.isPreserveCData();
         boolean keepParent = request.isKeepParent();
         boolean partialArchetype = request.isPartialArchetype();
-        ArtifactRepository localRepository = request.getLocalRepository();
         File outputDirectory = request.getOutputDirectory();
         File basedir = project.getBasedir();
 
@@ -143,8 +134,8 @@ public class FilesetArchetypeCreator
 
         try
         {
-            File archetypePomFile =
-                createArchetypeProjectPom( project, localRepository, configurationProperties, outputDirectory );
+            File archetypePomFile = createArchetypeProjectPom( project, request.getProjectBuildingRequest(),
+                                                               configurationProperties, outputDirectory );
 
             File archetypeResourcesDirectory = new File( outputDirectory, getTemplateOutputDirectory() );
 
@@ -395,7 +386,7 @@ public class FilesetArchetypeCreator
     /**
      * Create the archetype project pom.xml file, that will be used to build the archetype.
      */
-    private File createArchetypeProjectPom( MavenProject project, ArtifactRepository localRepository,
+    private File createArchetypeProjectPom( MavenProject project, ProjectBuildingRequest buildingRequest,
                                             Properties configurationProperties, File projectDir )
         throws TemplateCreationException, IOException
     {
@@ -428,37 +419,20 @@ public class FilesetArchetypeCreator
 
         if ( project.getParent() != null )
         {
-            Artifact pa = project.getParentArtifact();
+            MavenProject p = project.getParent();
 
-            try
+            if ( p.getDistributionManagement() != null )
             {
-                ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
-                buildingRequest.setLocalRepository( localRepository );
-                buildingRequest.setRemoteRepositories( project.getRemoteArtifactRepositories() );
-//                buildingRequest.setSystemProperties( System.getProperties() );
-//                buildingRequest.setUserProperties( configurationProperties );
-                
-                MavenProject p = projectBuilder.build( pa, buildingRequest ).getProject();
-
-                if ( p.getDistributionManagement() != null )
-                {
-                    model.setDistributionManagement( p.getDistributionManagement() );
-                }
+                model.setDistributionManagement( p.getDistributionManagement() );
+            }
 
-                if ( p.getBuildExtensions() != null )
+            if ( p.getBuildExtensions() != null )
+            {
+                for ( Extension be : p.getBuildExtensions() )
                 {
-                    for ( Extension be : p.getBuildExtensions() )
-                    {
-                        model.getBuild().addExtension( be );
-                    }
+                    model.getBuild().addExtension( be );
                 }
             }
-            catch ( ProjectBuildingException e )
-            {
-                throw new TemplateCreationException(
-                    "Error reading parent POM of project: " + pa.getGroupId() + ":" + pa.getArtifactId() + ":"
-                        + pa.getVersion() );
-            }
         }
 
         Extension extension = new Extension();
index f6156e1..58f9b81 100644 (file)
@@ -128,7 +128,8 @@ public class DefaultArchetypeCreatorIT
             .setPreserveCData( false )
             .setKeepParent( false )
             .setPostPhase( "verify" )
-            .setLocalRepository( localRepository );
+            .setLocalRepository( localRepository )
+            .setProjectBuildingRequest( buildingRequest );
 
         ArchetypeCreationResult result = new ArchetypeCreationResult();
 
@@ -191,17 +192,16 @@ public class DefaultArchetypeCreatorIT
         assertExists(template1);
     }
 
-//    @Ignore("ARCHETYPE-518")
-//    public void testSystemPropertiesAreIncluded()
-//                    throws Exception
-//    {
-//        String project = "included-system-properties";
-//
-//        createFilesetArchetype( project );
-//
-//        File template1 = getTemplateFile( project, "src/main/java/App.java" );
-//        assertExists(template1);
-//    }
+    public void testSystemPropertiesAreIncluded()
+                    throws Exception
+    {
+        String project = "included-system-properties";
+
+        createFilesetArchetype( project );
+
+        File template1 = getTemplateFile( project, "src/main/java/App.java" );
+        assertExists(template1);
+    }
 
     public void testCreateFilesetArchetype1()
         throws Exception
index e57726f..97c7740 100644 (file)
@@ -250,7 +250,7 @@ public class CreateArchetypeFromProjectMojo
                 /* Should be refactored to use some ant patterns */.setFiltereds( filtereds )
                 /* This should be correctly handled */.setPreserveCData( preserveCData ).setKeepParent(
                     keepParent ).setPartialArchetype( partialArchetype )
-                .setLocalRepository( localRepository )
+                .setLocalRepository( localRepository ).setProjectBuildingRequest( session.getProjectBuildingRequest() )
                 /* this should be resolved and asked for user to verify */.setPackageName( packageName ).setPostPhase(
                     archetypePostPhase ).setOutputDirectory( outputDirectory );