You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2010/04/10 18:33:51 UTC

svn commit: r932758 - /maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java

Author: hboutemy
Date: Sat Apr 10 16:33:51 2010
New Revision: 932758

URL: http://svn.apache.org/viewvc?rev=932758&view=rev
Log:
code refactoring

Modified:
    maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java

Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java?rev=932758&r1=932757&r2=932758&view=diff
==============================================================================
--- maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java (original)
+++ maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java Sat Apr 10 16:33:51 2010
@@ -113,33 +113,7 @@ public class FilesetArchetypeCreator
             configurationProperties.putAll( request.getProperties() );
         }
 
-        if ( !properties.containsKey( Constants.GROUP_ID ) )
-        {
-            properties.setProperty( Constants.GROUP_ID, project.getGroupId() );
-        }
-        configurationProperties.setProperty( Constants.GROUP_ID, properties.getProperty( Constants.GROUP_ID ) );
-
-        if ( !properties.containsKey( Constants.ARTIFACT_ID ) )
-        {
-            properties.setProperty( Constants.ARTIFACT_ID, project.getArtifactId() );
-        }
-        configurationProperties.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
-
-        if ( !properties.containsKey( Constants.VERSION ) )
-        {
-            properties.setProperty( Constants.VERSION, project.getVersion() );
-        }
-        configurationProperties.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
-
-        if ( request.getPackageName() != null )
-        {
-            properties.setProperty( Constants.PACKAGE, request.getPackageName() );
-        }
-        else if ( !properties.containsKey( Constants.PACKAGE ) )
-        {
-            properties.setProperty( Constants.PACKAGE, project.getGroupId() );
-        }
-        configurationProperties.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
+        extractPropertiesFromProject( project, properties, configurationProperties, request.getPackageName() );
 
         File basedir = project.getBasedir();
         File generatedSourcesDirectory = FileUtils.resolveFile( basedir, getGeneratedSourcesDirectory() );
@@ -147,126 +121,48 @@ public class FilesetArchetypeCreator
 
         getLogger().debug( "Creating archetype in " + generatedSourcesDirectory );
 
-        Model model = new Model();
-        model.setModelVersion( "4.0.0" );
-        // these values should be retrieved from the request with sensible defaults
-        model.setGroupId( configurationProperties.getProperty( Constants.ARCHETYPE_GROUP_ID, project.getGroupId() ) );
-        model.setArtifactId( configurationProperties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, project.getArtifactId() ) );
-        model.setVersion( configurationProperties.getProperty( Constants.ARCHETYPE_VERSION, project.getVersion() ) );
-        model.setPackaging( "maven-archetype" );
-        model.setName( configurationProperties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, project.getArtifactId() ) );
-
-        Build build = new Build();
-        model.setBuild( build );
-
-        // In many cases where we are behind a firewall making Archetypes for work mates we want
-        // to simply be able to deploy the archetypes once we have created them. In order to do
-        // this we want to utilize information from the project we are creating the archetype from.
-        // This will be a fully working project that has been testing and inherits from a POM
-        // that contains deployment information, along with any extensions required for deployment.
-        // We don't want to create archetypes that cannot be deployed after we create them. People
-        // might want to edit the archetype POM but they should not have too.
-
-        if ( project.getParent() != null )
-        {
-            Artifact pa = project.getParentArtifact();
-
-            try
-            {
-                MavenProject p = projectBuilder.buildFromRepository( pa, project.getRemoteArtifactRepositories(), localRepository );
-
-                if ( p.getDistributionManagement() != null )
-                {
-                    model.setDistributionManagement( p.getDistributionManagement() );
-                }
-
-                if ( p.getBuildExtensions() != null )
-                {
-                    for ( Iterator i = p.getBuildExtensions().iterator(); i.hasNext(); )
-                    {
-                        Extension be = (Extension) i.next();
-
-                        model.getBuild().addExtension( be );
-                    }
-                }
-            }
-            catch ( ProjectBuildingException e )
-            {
-                result.setCause( new TemplateCreationException(
-                    "Error reading parent POM of project: " + pa.getGroupId() + ":" + pa.getArtifactId() + ":" + pa.getVersion() ) );
-
-                return;
-            }
-        }
-
-        Extension extension = new Extension();
-        extension.setGroupId( "org.apache.maven.archetype" );
-        extension.setArtifactId( "archetype-packaging" );
-        extension.setVersion( getArchetypeVersion() );
-        model.getBuild().addExtension( extension );
-
-        Plugin plugin = new Plugin();
-        plugin.setGroupId( "org.apache.maven.plugins" );
-        plugin.setArtifactId( "maven-archetype-plugin" );
-        plugin.setVersion( getArchetypeVersion() );
-        plugin.setExtensions( true );
-        model.getBuild().addPlugin( plugin );
-
-        getLogger().debug( "Creating archetype's pom" );
-
-        File archetypePomFile = FileUtils.resolveFile( basedir, getArchetypePom() );
-
         try
         {
-            archetypePomFile.getParentFile().mkdirs();
-
-            pomManager.writePom( model, archetypePomFile, archetypePomFile );
-        }
-        catch ( IOException e )
-        {
-            result.setCause( e );
-        }
+            File archetypePomFile =
+                createArchetypeProjectPom( project, localRepository, configurationProperties, generatedSourcesDirectory );
 
-        File archetypeResourcesDirectory = FileUtils.resolveFile( generatedSourcesDirectory, getTemplateOutputDirectory() );
-        archetypeResourcesDirectory.mkdirs();
+            File archetypeResourcesDirectory = new File( generatedSourcesDirectory, getTemplateOutputDirectory() );
 
-        File archetypeFilesDirectory = FileUtils.resolveFile( archetypeResourcesDirectory, Constants.ARCHETYPE_RESOURCES );
-        archetypeFilesDirectory.mkdirs();
-        getLogger().debug( "Archetype's files output directory " + archetypeFilesDirectory );
+            File archetypeFilesDirectory = new File( archetypeResourcesDirectory, Constants.ARCHETYPE_RESOURCES );
+            getLogger().debug( "Archetype's files output directory " + archetypeFilesDirectory );
 
-        File archetypeDescriptorFile = FileUtils.resolveFile( archetypeResourcesDirectory, Constants.ARCHETYPE_DESCRIPTOR );
-        archetypeDescriptorFile.getParentFile().mkdirs();
+            File archetypeDescriptorFile = new File( archetypeResourcesDirectory, Constants.ARCHETYPE_DESCRIPTOR );
+            archetypeDescriptorFile.getParentFile().mkdirs();
 
-        ArchetypeDescriptor archetypeDescriptor = new ArchetypeDescriptor();
-        archetypeDescriptor.setName( project.getArtifactId() );
+            getLogger().debug( "Starting archetype's descriptor " + project.getArtifactId() );
+            ArchetypeDescriptor archetypeDescriptor = new ArchetypeDescriptor();
 
-        getLogger().debug( "Starting archetype's descriptor " + project.getArtifactId() );
-        archetypeDescriptor.setPartial( partialArchetype );
+            archetypeDescriptor.setName( project.getArtifactId() );
+            archetypeDescriptor.setPartial( partialArchetype );
 
-        addRequiredProperties( archetypeDescriptor, properties );
+            addRequiredProperties( archetypeDescriptor, properties );
 
-        // TODO ensure reverseProperties contains NO dotted properties
-        Properties reverseProperties = getRequiredProperties( archetypeDescriptor, properties );
-        //reverseProperties.remove( Constants.GROUP_ID );
+            // TODO ensure reverseProperties contains NO dotted properties
+            Properties reverseProperties = getRequiredProperties( archetypeDescriptor, properties );
+            // reverseProperties.remove( Constants.GROUP_ID );
 
-        // TODO ensure pomReversedProperties contains NO dotted properties
-        Properties pomReversedProperties = getRequiredProperties( archetypeDescriptor, properties );
-        //pomReversedProperties.remove( Constants.PACKAGE );
+            // TODO ensure pomReversedProperties contains NO dotted properties
+            Properties pomReversedProperties = getRequiredProperties( archetypeDescriptor, properties );
+            // pomReversedProperties.remove( Constants.PACKAGE );
 
-        String packageName = configurationProperties.getProperty( Constants.PACKAGE );
+            String packageName = configurationProperties.getProperty( Constants.PACKAGE );
 
-        try
-        {
             Model pom = pomManager.readPom( FileUtils.resolveFile( basedir, Constants.ARCHETYPE_POM ) );
 
             List fileNames = resolveFileNames( pom, basedir );
-            getLogger().debug( "Scanned for files " + fileNames.size() );
-
-            Iterator names = fileNames.iterator();
-
-            while ( names.hasNext() )
+            if ( getLogger().isDebugEnabled() )
             {
-                getLogger().debug( "- " + names.next().toString() );
+                getLogger().debug( "Scanned for files " + fileNames.size() );
+
+                for ( Iterator names = fileNames.iterator(); names.hasNext(); )
+                {
+                    getLogger().debug( "- " + names.next().toString() );
+                }
             }
 
             List filesets = resolveFileSets( packageName, fileNames, languages, filtereds, defaultEncoding );
@@ -279,8 +175,7 @@ public class FilesetArchetypeCreator
 
             setParentArtifactId( reverseProperties, configurationProperties.getProperty( Constants.ARTIFACT_ID ) );
 
-            Iterator modules = pom.getModules().iterator();
-            while ( modules.hasNext() )
+            for ( Iterator modules = pom.getModules().iterator(); modules.hasNext(); )
             {
                 String moduleId = (String) modules.next();
                 String rootArtifactId = configurationProperties.getProperty( Constants.ARTIFACT_ID );
@@ -296,7 +191,7 @@ public class FilesetArchetypeCreator
                 ModuleDescriptor moduleDescriptor =
                     createModule( reverseProperties, rootArtifactId, moduleId, packageName,
                                   FileUtils.resolveFile( basedir, moduleId ),
-                                  FileUtils.resolveFile( archetypeFilesDirectory, moduleIdDirectory ), languages,
+                                  new File( archetypeFilesDirectory, moduleIdDirectory ), languages,
                                   filtereds, defaultEncoding, preserveCData, keepParent );
 
                 archetypeDescriptor.addModule( moduleDescriptor );
@@ -312,7 +207,7 @@ public class FilesetArchetypeCreator
             createPoms( pom, configurationProperties.getProperty( Constants.ARTIFACT_ID ),
                         configurationProperties.getProperty( Constants.ARTIFACT_ID ), archetypeFilesDirectory, basedir,
                         pomReversedProperties, preserveCData, keepParent );
-            getLogger().debug( "Created Archetype " + archetypeDescriptor.getName() + " pom" );
+            getLogger().debug( "Created Archetype " + archetypeDescriptor.getName() + " template pom(s)" );
 
             Writer out = null;
             try
@@ -343,6 +238,119 @@ public class FilesetArchetypeCreator
         }
     }
 
+    private void extractPropertiesFromProject( MavenProject project, Properties properties,
+                                               Properties configurationProperties, String packageName )
+    {
+        if ( !properties.containsKey( Constants.GROUP_ID ) )
+        {
+            properties.setProperty( Constants.GROUP_ID, project.getGroupId() );
+        }
+        configurationProperties.setProperty( Constants.GROUP_ID, properties.getProperty( Constants.GROUP_ID ) );
+
+        if ( !properties.containsKey( Constants.ARTIFACT_ID ) )
+        {
+            properties.setProperty( Constants.ARTIFACT_ID, project.getArtifactId() );
+        }
+        configurationProperties.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
+
+        if ( !properties.containsKey( Constants.VERSION ) )
+        {
+            properties.setProperty( Constants.VERSION, project.getVersion() );
+        }
+        configurationProperties.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
+
+        if ( packageName != null )
+        {
+            properties.setProperty( Constants.PACKAGE, packageName );
+        }
+        else if ( !properties.containsKey( Constants.PACKAGE ) )
+        {
+            properties.setProperty( Constants.PACKAGE, project.getGroupId() );
+        }
+        configurationProperties.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
+    }
+
+    /**
+     * Create the archetype project pom.xml file, that will be used to build the archetype.
+     */
+    private File createArchetypeProjectPom( MavenProject project, ArtifactRepository localRepository,
+                                            Properties configurationProperties, File projectDir )
+        throws TemplateCreationException, IOException
+    {
+        Model model = new Model();
+        model.setModelVersion( "4.0.0" );
+        // these values should be retrieved from the request with sensible defaults
+        model.setGroupId( configurationProperties.getProperty( Constants.ARCHETYPE_GROUP_ID, project.getGroupId() ) );
+        model.setArtifactId( configurationProperties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, project.getArtifactId() ) );
+        model.setVersion( configurationProperties.getProperty( Constants.ARCHETYPE_VERSION, project.getVersion() ) );
+        model.setPackaging( "maven-archetype" );
+        model.setName( configurationProperties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, project.getArtifactId() ) );
+
+        Build build = new Build();
+        model.setBuild( build );
+
+        // In many cases where we are behind a firewall making Archetypes for work mates we want
+        // to simply be able to deploy the archetypes once we have created them. In order to do
+        // this we want to utilize information from the project we are creating the archetype from.
+        // This will be a fully working project that has been testing and inherits from a POM
+        // that contains deployment information, along with any extensions required for deployment.
+        // We don't want to create archetypes that cannot be deployed after we create them. People
+        // might want to edit the archetype POM but they should not have too.
+
+        if ( project.getParent() != null )
+        {
+            Artifact pa = project.getParentArtifact();
+
+            try
+            {
+                MavenProject p = projectBuilder.buildFromRepository( pa, project.getRemoteArtifactRepositories(), localRepository );
+
+                if ( p.getDistributionManagement() != null )
+                {
+                    model.setDistributionManagement( p.getDistributionManagement() );
+                }
+
+                if ( p.getBuildExtensions() != null )
+                {
+                    for ( Iterator i = p.getBuildExtensions().iterator(); i.hasNext(); )
+                    {
+                        Extension be = (Extension) i.next();
+
+                        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();
+        extension.setGroupId( "org.apache.maven.archetype" );
+        extension.setArtifactId( "archetype-packaging" );
+        extension.setVersion( getArchetypeVersion() );
+        model.getBuild().addExtension( extension );
+
+        Plugin plugin = new Plugin();
+        plugin.setGroupId( "org.apache.maven.plugins" );
+        plugin.setArtifactId( "maven-archetype-plugin" );
+        plugin.setVersion( getArchetypeVersion() );
+        plugin.setExtensions( true );
+        model.getBuild().addPlugin( plugin );
+
+        getLogger().debug( "Creating archetype's pom" );
+
+        File archetypePomFile = new File( projectDir, Constants.ARCHETYPE_POM );
+
+        archetypePomFile.getParentFile().mkdirs();
+
+        pomManager.writePom( model, archetypePomFile, archetypePomFile );
+
+        return archetypePomFile;
+    }
+
     private void addRequiredProperties( ArchetypeDescriptor archetypeDescriptor, Properties properties )
     {
         Properties requiredProperties = new Properties();
@@ -425,7 +433,7 @@ public class FilesetArchetypeCreator
 
             createModulePoms( pomReversedProperties, rootArtifactId, moduleId,
                               FileUtils.resolveFile( basedir, moduleId ),
-                              FileUtils.resolveFile( archetypeFilesDirectory, moduleIdDirectory ),
+                              new File( archetypeFilesDirectory, moduleIdDirectory ),
                               preserveCData, keepParent );
         }
 
@@ -436,11 +444,6 @@ public class FilesetArchetypeCreator
                             FileUtils.resolveFile( basedir, Constants.ARCHETYPE_POM ), preserveCData, keepParent );
     }
 
-    private String getArchetypePom()
-    {
-        return getGeneratedSourcesDirectory() + File.separator + Constants.ARCHETYPE_POM;
-    }
-
     private String getPackageInPathFormat( String aPackage )
     {
         return StringUtils.replace( aPackage, ".", "/" );
@@ -726,12 +729,6 @@ public class FilesetArchetypeCreator
         }
     }
 
-    private void copyPom( File basedir, File replicaFilesDirectory )
-        throws IOException
-    {
-        FileUtils.copyFileToDirectory( new File( basedir, Constants.ARCHETYPE_POM ), replicaFilesDirectory );
-    }
-
     private void createArchetypeFiles( Properties reverseProperties, List fileSets, String packageName, File basedir,
                                        File archetypeFilesDirectory, String defaultEncoding )
         throws IOException
@@ -1298,19 +1295,19 @@ public class FilesetArchetypeCreator
     {
         getLogger().debug( "Resolving files for " + pom.getId() + " in " + basedir );
 
-        String excludes = "pom.xml*,archetype.properties*,target/**,";
+        StringBuffer buff = new StringBuffer( "pom.xml*,archetype.properties*,target/**," );
         for ( Iterator modules = pom.getModules().iterator(); modules.hasNext(); )
         {
-            excludes += "," + (String) modules.next() + "/**";
+            buff.append( ',' ).append( (String) modules.next() ).append( "/**" );
         }
 
         for ( Iterator defaultExcludes = Arrays.asList( ListScanner.DEFAULTEXCLUDES ).iterator();
             defaultExcludes.hasNext(); )
         {
-            excludes += "," + (String) defaultExcludes.next() + "/**";
+            buff.append( ',' ).append( (String) defaultExcludes.next() ).append( "/**" );
         }
 
-        excludes = PathUtils.convertPathForOS( excludes );
+        String excludes = PathUtils.convertPathForOS( buff.toString() );
 
         List fileNames = FileUtils.getFileNames( basedir, "**,.*,**/.*", excludes, false );