You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2025/05/11 21:14:54 UTC
(maven-archetype) branch master updated: Fix recognize project packaging by PomUtils (#264)
This is an automated email from the ASF dual-hosted git repository.
sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-archetype.git
The following commit(s) were added to refs/heads/master by this push:
new 39a926bc Fix recognize project packaging by PomUtils (#264)
39a926bc is described below
commit 39a926bc4af4660ce017fcf36feb56286687bab4
Author: Thomas David <72...@users.noreply.github.com>
AuthorDate: Sun May 11 23:14:48 2025 +0200
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 <s....@gmail.com>
---
.../maven/archetype/common/util/PomUtils.java | 21 ++++-----
.../maven/archetype/common/TestPomManager.java | 50 ++++++++++++++++------
.../projects/pom-manager/pom-sample-1.xml | 45 +++++++++++++++++++
.../projects/pom-manager/pom-sample-2.xml | 45 +++++++++++++++++++
4 files changed, 139 insertions(+), 22 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 f320c13b..0e2fd956 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
@@ -48,6 +48,7 @@ import org.xml.sax.SAXException;
* POM helper class.
*/
public final class PomUtils {
+
private PomUtils() {
throw new IllegalStateException("no instantiable constructor");
}
@@ -89,16 +90,16 @@ public final class PomUtils {
}
String packaging = null;
- NodeList packagingElement = project.getElementsByTagName("packaging");
- if (packagingElement != null && packagingElement.getLength() == 1) {
- packaging = packagingElement.item(0).getTextContent();
+ Node packagingNode = getChildNode(project, "packaging");
+ if (packagingNode != null) {
+ packaging = packagingNode.getTextContent();
}
if (!"pom".equals(packaging)) {
throw new InvalidPackaging(
"Unable to add module to the current project as it is not of packaging type 'pom'");
}
- Node modules = getModulesNode(project);
+ Node modules = getChildNode(project, "modules");
if (!hasArtifactIdInModules(artifactId, modules)) {
Element module = document.createElement("module");
@@ -137,17 +138,17 @@ public final class PomUtils {
}
}
- private static Node getModulesNode(Element project) {
- Node modules = null;
- NodeList nodes = project.getChildNodes();
+ private static Node getChildNode(Element parent, String name) {
+ Node namedNode = null;
+ NodeList nodes = parent.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;
+ if (node.getNodeType() == Node.ELEMENT_NODE && name.equals(node.getNodeName())) {
+ namedNode = node;
break;
}
}
- return modules;
+ return namedNode;
}
private static boolean hasArtifactIdInModules(String artifactId, Node modules) {
diff --git a/archetype-common/src/test/java/org/apache/maven/archetype/common/TestPomManager.java b/archetype-common/src/test/java/org/apache/maven/archetype/common/TestPomManager.java
index 6757e166..b3d2bc66 100644
--- a/archetype-common/src/test/java/org/apache/maven/archetype/common/TestPomManager.java
+++ b/archetype-common/src/test/java/org/apache/maven/archetype/common/TestPomManager.java
@@ -18,34 +18,60 @@
*/
package org.apache.maven.archetype.common;
-import java.io.File;
-import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.apache.commons.io.FileUtils;
-import org.junit.Assert;
+import org.apache.maven.archetype.exception.InvalidPackaging;
import org.junit.Test;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
public class TestPomManager {
// ref: https://www.baeldung.com/java-pretty-print-xml
// https://bugs.openjdk.java.net/browse/JDK-8262285?attachmentViewMode=list
@Test
- public void testAddModule() throws Exception {
+ public void testAddModulePomPackaging() throws Exception {
PomManager pomManager = new DefaultPomManager();
- URL pom = getClass().getResource("/projects/generate-9/pom.xml.sample");
- File pomFileSrc = new File(pom.toURI());
- File pomFile = new File(pomFileSrc.getAbsolutePath() + "-copied.xml");
- FileUtils.copyFile(pomFileSrc, pomFile);
+ Path pomPath = Paths.get(
+ getClass().getResource("/projects/pom-manager/pom-sample-1.xml").toURI());
+ Path pomDestPath = pomPath.getParent().resolve("pom-sample-1-copied.xml");
+ Files.copy(pomPath, pomDestPath, StandardCopyOption.REPLACE_EXISTING);
+
final int moduleNumber = 4;
for (int i = 0; i < moduleNumber; i++) {
- pomManager.addModule(pomFile, "test" + i);
+ pomManager.addModule(pomDestPath.toFile(), "test" + i);
}
- String fileText = FileUtils.readFileToString(pomFile, "UTF-8");
+
+ String fileText = new String(Files.readAllBytes(pomDestPath), StandardCharsets.UTF_8);
Pattern pattern = Pattern.compile("(^[ ]+[\\r\\n]+){" + moduleNumber + "}", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(fileText);
- Assert.assertFalse(matcher.find());
+ assertFalse(matcher.find());
+ }
+
+ @Test
+ public void testAddModuleNonPomPackaging() throws Exception {
+ PomManager pomManager = new DefaultPomManager();
+
+ Path pomPath = Paths.get(
+ getClass().getResource("/projects/pom-manager/pom-sample-2.xml").toURI());
+ Path pomDestPath = pomPath.getParent().resolve("pom-sample-1-copied.xml");
+ Files.copy(pomPath, pomDestPath, StandardCopyOption.REPLACE_EXISTING);
+
+ Exception expectedException = null;
+ try {
+ pomManager.addModule(pomDestPath.toFile(), "test");
+ } catch (InvalidPackaging e) {
+ expectedException = e;
+ }
+
+ assertNotNull(expectedException);
}
}
diff --git a/archetype-common/src/test/resources/projects/pom-manager/pom-sample-1.xml b/archetype-common/src/test/resources/projects/pom-manager/pom-sample-1.xml
new file mode 100644
index 00000000..92efb7b3
--- /dev/null
+++ b/archetype-common/src/test/resources/projects/pom-manager/pom-sample-1.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you 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.
+ -->
+<project
+ xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.archetype</groupId>
+ <artifactId>pom-manager-1</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <name>Maven archetype Test Pom Manager</name>
+ <packaging>pom</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.archetype</groupId>
+ <artifactId>test-maven-plugin</artifactId>
+ <configuration>
+ <packaging>jar</packaging>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/archetype-common/src/test/resources/projects/pom-manager/pom-sample-2.xml b/archetype-common/src/test/resources/projects/pom-manager/pom-sample-2.xml
new file mode 100644
index 00000000..25ad786e
--- /dev/null
+++ b/archetype-common/src/test/resources/projects/pom-manager/pom-sample-2.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you 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.
+ -->
+<project
+ xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.archetype</groupId>
+ <artifactId>pom-manager-1</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <name>Maven archetype Test Pom Manager</name>
+ <packaging>jar</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.archetype</groupId>
+ <artifactId>test-maven-plugin</artifactId>
+ <configuration>
+ <packaging>pom</packaging>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>