You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2019/07/26 14:46:48 UTC

[maven-archetype] branch ARCHETYPE-571 created (now 443064d)

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a change to branch ARCHETYPE-571
in repository https://gitbox.apache.org/repos/asf/maven-archetype.git.


      at 443064d  [ARCHETYPE-571] Unable to generate project which has pom with multiple profiles with modules section

This branch includes the following new commits:

     new 443064d  [ARCHETYPE-571] Unable to generate project which has pom with multiple profiles with modules section

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-archetype] 01/01: [ARCHETYPE-571] Unable to generate project which has pom with multiple profiles with modules section

Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch ARCHETYPE-571
in repository https://gitbox.apache.org/repos/asf/maven-archetype.git

commit 443064d6426f5cb1a1cd1dbe30bc41e0147ba289
Author: tibordigana <ti...@apache.org>
AuthorDate: Fri Jul 26 16:46:22 2019 +0200

    [ARCHETYPE-571] Unable to generate project which has pom with multiple profiles with modules section
---
 .../maven/archetype/common/util/PomUtils.java      | 75 +++++++++++++---------
 .../apache/maven/archetype/old/ArchetypeTest.java  | 52 +++++++++++++++
 2 files changed, 98 insertions(+), 29 deletions(-)

diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/common/util/PomUtils.java b/archetype-common/src/main/java/org/apache/maven/archetype/common/util/PomUtils.java
index 971adaa..a27f125 100644
--- a/archetype-common/src/main/java/org/apache/maven/archetype/common/util/PomUtils.java
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/common/util/PomUtils.java
@@ -102,37 +102,17 @@ public final class PomUtils
                     "Unable to add module to the current project as it is not of packaging type 'pom'" );
         }
 
-        NodeList modulesList = project.getElementsByTagName( "modules" );
-        final Element modules;
-        if ( modulesList.getLength() == 0 )
-        {
-            modules = document.createElement( "modules" );
-            project.appendChild( modules );
-        }
-        else if ( modulesList.getLength() == 1 )
-        {
-            modules = (Element) modulesList.item( 0 );
-        }
-        else
-        {
-            throw new ArchetypeTemplateProcessingException( "Illegal to use multiple <modules/> sections." );
-        }
-        boolean found = false;
-        NodeList moduleList = modules.getElementsByTagName( "module" );
-        for ( int len = moduleList.getLength(), i = 0; i < len; i++ )
-        {
-            Node module = moduleList.item( i );
-            String moduleValue = module.getTextContent();
-            if ( moduleValue != null && moduleValue.equals( artifactId ) )
-            {
-                found = true;
-                break;
-            }
-        }
-        if ( !found )
+        Node modules = getModulesNode( project );
+
+        if ( !hasArtifactIdInModules( artifactId, modules ) )
         {
             Element module = document.createElement( "module" );
             module.setTextContent( artifactId );
+            if ( modules == null )
+            {
+                modules = document.createElement( "modules" );
+                project.appendChild( modules );
+            }
             modules.appendChild( module );
 
             TransformerFactory tf = TransformerFactory.newInstance();
@@ -146,7 +126,44 @@ public final class PomUtils
             tr.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
             tr.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
             tr.transform( new DOMSource( document ), new StreamResult( fileWriter ) );
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    private static Node getModulesNode( Element project )
+    {
+        Node modules = null;
+        NodeList nodes = project.getChildNodes();
+        for ( int len = nodes.getLength(), i = 0; i < len; i++ )
+        {
+            Node node = nodes.item( i );
+            if ( node.getNodeType() == Node.ELEMENT_NODE && "modules".equals( node.getNodeName() ) )
+            {
+                modules = node;
+                break;
+            }
+        }
+        return modules;
+    }
+
+    private static boolean hasArtifactIdInModules( String artifactId, Node modules )
+    {
+        if ( modules != null )
+        {
+            Node module = modules.getFirstChild();
+            while ( module != null )
+            {
+                if ( module.getNodeType() == Node.ELEMENT_NODE && artifactId.equals( module.getTextContent() ) )
+                {
+                    return true;
+                }
+                module = module.getNextSibling();
+            }
         }
-        return !found;
+        return false;
     }
 }
diff --git a/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java b/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
index 92f2fd5..e4490d1 100644
--- a/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
+++ b/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
@@ -324,6 +324,58 @@ public class ArchetypeTest
 
         // empty means unchanged
         assertEquals( "", out.toString().trim() );
+
+
+        pom = "<project><modelVersion>4.0.0</modelVersion>\n"
+                + "  <packaging>pom</packaging>\n"
+                + "  <modules>\n"
+                + "    <module>myArtifactId1</module>\n"
+                + "    <module>myArtifactId2</module>\n"
+                + "    <module>myArtifactId3</module>\n"
+                + "  </modules>\n"
+                + "  <profiles>\n"
+                + "    <profile>\n"
+                + "      <id>profile1</id>\n"
+                + "      <modules>\n"
+                + "        <module>module1</module>\n"
+                + "      </modules>\n"
+                + "    </profile>\n"
+                + "    <profile>\n"
+                + "      <id>profile2</id>\n"
+                + "      <modules>\n"
+                + "        <module>module2</module>\n"
+                + "      </modules>\n"
+                + "    </profile>\n"
+                + "  </profiles>\n"
+                + "</project>";
+
+        out = new StringWriter();
+        assertTrue( DefaultOldArchetype.addModuleToParentPom( "module1", new StringReader( pom ), out ) );
+
+        assertThat( out.toString(), isIdenticalTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+                + "<project><modelVersion>4.0.0</modelVersion>\n"
+                + "  <packaging>pom</packaging>\n"
+                + "  <modules>\n"
+                + "    <module>myArtifactId1</module>\n"
+                + "    <module>myArtifactId2</module>\n"
+                + "    <module>myArtifactId3</module>\n"
+                + "    <module>module1</module>\n"
+                + "  </modules>\n"
+                + "  <profiles>\n"
+                + "    <profile>\n"
+                + "      <id>profile1</id>\n"
+                + "      <modules>\n"
+                + "        <module>module1</module>\n"
+                + "      </modules>\n"
+                + "    </profile>\n"
+                + "    <profile>\n"
+                + "      <id>profile2</id>\n"
+                + "      <modules>\n"
+                + "        <module>module2</module>\n"
+                + "      </modules>\n"
+                + "    </profile>\n"
+                + "  </profiles>\n"
+                + "</project>" ).normalizeWhitespace() );
     }
 
     public void testAddModuleToParentPOMNoPackaging()