Skip to content

Commit 39a926b

Browse files
Fix recognize project packaging by PomUtils (#264)
* ISSUE_263 Unable to add module as it is not of packaging 'pom' /fixed Check for packaging type pom relies now on child node of project * Extend unit test --------- Co-authored-by: Slawomir Jaranowski <[email protected]>
1 parent 0c1e55e commit 39a926b

File tree

4 files changed

+139
-22
lines changed

4 files changed

+139
-22
lines changed

archetype-common/src/main/java/org/apache/maven/archetype/common/util/PomUtils.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* POM helper class.
4949
*/
5050
public final class PomUtils {
51+
5152
private PomUtils() {
5253
throw new IllegalStateException("no instantiable constructor");
5354
}
@@ -89,16 +90,16 @@ public static boolean addNewModule(String artifactId, Reader fileReader, Writer
8990
}
9091

9192
String packaging = null;
92-
NodeList packagingElement = project.getElementsByTagName("packaging");
93-
if (packagingElement != null && packagingElement.getLength() == 1) {
94-
packaging = packagingElement.item(0).getTextContent();
93+
Node packagingNode = getChildNode(project, "packaging");
94+
if (packagingNode != null) {
95+
packaging = packagingNode.getTextContent();
9596
}
9697
if (!"pom".equals(packaging)) {
9798
throw new InvalidPackaging(
9899
"Unable to add module to the current project as it is not of packaging type 'pom'");
99100
}
100101

101-
Node modules = getModulesNode(project);
102+
Node modules = getChildNode(project, "modules");
102103

103104
if (!hasArtifactIdInModules(artifactId, modules)) {
104105
Element module = document.createElement("module");
@@ -137,17 +138,17 @@ public static boolean addNewModule(String artifactId, Reader fileReader, Writer
137138
}
138139
}
139140

140-
private static Node getModulesNode(Element project) {
141-
Node modules = null;
142-
NodeList nodes = project.getChildNodes();
141+
private static Node getChildNode(Element parent, String name) {
142+
Node namedNode = null;
143+
NodeList nodes = parent.getChildNodes();
143144
for (int len = nodes.getLength(), i = 0; i < len; i++) {
144145
Node node = nodes.item(i);
145-
if (node.getNodeType() == Node.ELEMENT_NODE && "modules".equals(node.getNodeName())) {
146-
modules = node;
146+
if (node.getNodeType() == Node.ELEMENT_NODE && name.equals(node.getNodeName())) {
147+
namedNode = node;
147148
break;
148149
}
149150
}
150-
return modules;
151+
return namedNode;
151152
}
152153

153154
private static boolean hasArtifactIdInModules(String artifactId, Node modules) {

archetype-common/src/test/java/org/apache/maven/archetype/common/TestPomManager.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,60 @@
1818
*/
1919
package org.apache.maven.archetype.common;
2020

21-
import java.io.File;
22-
import java.net.URL;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.nio.file.StandardCopyOption;
2326
import java.util.regex.Matcher;
2427
import java.util.regex.Pattern;
2528

26-
import org.apache.commons.io.FileUtils;
27-
import org.junit.Assert;
29+
import org.apache.maven.archetype.exception.InvalidPackaging;
2830
import org.junit.Test;
2931

32+
import static org.junit.Assert.assertFalse;
33+
import static org.junit.Assert.assertNotNull;
34+
3035
public class TestPomManager {
3136

3237
// ref: https://www.baeldung.com/java-pretty-print-xml
3338
// https://bugs.openjdk.java.net/browse/JDK-8262285?attachmentViewMode=list
3439
@Test
35-
public void testAddModule() throws Exception {
40+
public void testAddModulePomPackaging() throws Exception {
3641
PomManager pomManager = new DefaultPomManager();
3742

38-
URL pom = getClass().getResource("/projects/generate-9/pom.xml.sample");
39-
File pomFileSrc = new File(pom.toURI());
40-
File pomFile = new File(pomFileSrc.getAbsolutePath() + "-copied.xml");
41-
FileUtils.copyFile(pomFileSrc, pomFile);
43+
Path pomPath = Paths.get(
44+
getClass().getResource("/projects/pom-manager/pom-sample-1.xml").toURI());
45+
Path pomDestPath = pomPath.getParent().resolve("pom-sample-1-copied.xml");
46+
Files.copy(pomPath, pomDestPath, StandardCopyOption.REPLACE_EXISTING);
47+
4248
final int moduleNumber = 4;
4349
for (int i = 0; i < moduleNumber; i++) {
44-
pomManager.addModule(pomFile, "test" + i);
50+
pomManager.addModule(pomDestPath.toFile(), "test" + i);
4551
}
46-
String fileText = FileUtils.readFileToString(pomFile, "UTF-8");
52+
53+
String fileText = new String(Files.readAllBytes(pomDestPath), StandardCharsets.UTF_8);
4754
Pattern pattern = Pattern.compile("(^[ ]+[\\r\\n]+){" + moduleNumber + "}", Pattern.MULTILINE);
4855
Matcher matcher = pattern.matcher(fileText);
49-
Assert.assertFalse(matcher.find());
56+
assertFalse(matcher.find());
57+
}
58+
59+
@Test
60+
public void testAddModuleNonPomPackaging() throws Exception {
61+
PomManager pomManager = new DefaultPomManager();
62+
63+
Path pomPath = Paths.get(
64+
getClass().getResource("/projects/pom-manager/pom-sample-2.xml").toURI());
65+
Path pomDestPath = pomPath.getParent().resolve("pom-sample-1-copied.xml");
66+
Files.copy(pomPath, pomDestPath, StandardCopyOption.REPLACE_EXISTING);
67+
68+
Exception expectedException = null;
69+
try {
70+
pomManager.addModule(pomDestPath.toFile(), "test");
71+
} catch (InvalidPackaging e) {
72+
expectedException = e;
73+
}
74+
75+
assertNotNull(expectedException);
5076
}
5177
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
<project
21+
xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<groupId>org.apache.maven.archetype</groupId>
28+
<artifactId>pom-manager-1</artifactId>
29+
<version>1.0-SNAPSHOT</version>
30+
31+
<name>Maven archetype Test Pom Manager</name>
32+
<packaging>pom</packaging>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.archetype</groupId>
38+
<artifactId>test-maven-plugin</artifactId>
39+
<configuration>
40+
<packaging>jar</packaging>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
<project
21+
xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<groupId>org.apache.maven.archetype</groupId>
28+
<artifactId>pom-manager-1</artifactId>
29+
<version>1.0-SNAPSHOT</version>
30+
31+
<name>Maven archetype Test Pom Manager</name>
32+
<packaging>jar</packaging>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.archetype</groupId>
38+
<artifactId>test-maven-plugin</artifactId>
39+
<configuration>
40+
<packaging>pom</packaging>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
</project>

0 commit comments

Comments
 (0)