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

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-ear-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new ec82f7e  fix: trim whitespace-only classifier to prevent spaces in EAR 
filename (#531)
ec82f7e is described below

commit ec82f7e51b0e391e2533a7fac95bc033928b07a0
Author: Elliotte Rusty Harold <[email protected]>
AuthorDate: Thu Jul 16 14:06:46 2026 -0500

    fix: trim whitespace-only classifier to prevent spaces in EAR filename 
(#531)
    
    * fix: trim whitespace-only classifier to prevent spaces in EAR filename 
(#521)
    
    The getEarFile method did not handle whitespace-only classifiers. A
    classifier like "   " would pass through all branches unchanged,
    producing filenames with embedded spaces (e.g. "myapp   .ear").
    
    - Trim the classifier string before processing
    - Treat empty/whitespace-only classifiers the same as null
    - Add unit tests for getEarFile with null, normal, dash-prefixed,
      and whitespace-only classifiers
    - Add integration test (project-103) with a whitespace-only classifier
    
    * fix: normalize classifier in execute() instead of getEarFile()
    
    Move classifier normalization (trim + treat empty as null) to execute()
    so that both getEarFile() and the attachArtifact() decision use the
    same normalized value. Simplify getEarFile() since it no longer needs
    to trim.
    
    Fix unit test to use java.io.tmpdir instead of hard-coded /tmp for
    portability across platforms. Remove the whitespace-only unit test
    since trimming is now in execute(); the integration test (project-103)
    covers the end-to-end behavior.
    
    Addresses PR #531 review feedback:
    - normalize classifier once in execute()
    - use portable temp directory in unit tests
    
    * use @TempDir instead of java.io.tmpdir system property
    
    * use non-static @TempDir field
---
 .../java/org/apache/maven/plugins/ear/EarMojo.java | 10 +++-
 .../org/apache/maven/plugins/ear/EarMojoTest.java  | 57 ++++++++++++++++++++++
 .../org/apache/maven/plugins/ear/it/EarMojoIT.java | 13 +++++
 src/test/resources/projects/project-103/pom.xml    | 50 +++++++++++++++++++
 4 files changed, 129 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java 
b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
index 576a009..b949180 100644
--- a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
@@ -305,6 +305,14 @@ public class EarMojo extends AbstractEarMojo {
         // Initializes ear modules
         super.execute();
 
+        // Normalize classifier: trim whitespace and treat empty as null
+        if (classifier != null) {
+            classifier = classifier.trim();
+            if (classifier.isEmpty()) {
+                classifier = null;
+            }
+        }
+
         File earFile = getEarFile(outputDirectory, finalName, classifier);
         MavenArchiver archiver = new EarMavenArchiver(getModules());
         File ddFile = new File(getWorkDirectory(), APPLICATION_XML_URI);
@@ -562,7 +570,7 @@ public class EarMojo extends AbstractEarMojo {
     private static File getEarFile(String basedir, String finalName, String 
classifier) {
         if (classifier == null) {
             classifier = "";
-        } else if (classifier.trim().length() > 0 && 
!classifier.startsWith("-")) {
+        } else if (!classifier.startsWith("-")) {
             classifier = "-" + classifier;
         }
 
diff --git a/src/test/java/org/apache/maven/plugins/ear/EarMojoTest.java 
b/src/test/java/org/apache/maven/plugins/ear/EarMojoTest.java
new file mode 100644
index 0000000..b0c37cb
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugins/ear/EarMojoTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.ear;
+
+import java.io.File;
+import java.lang.reflect.Method;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class EarMojoTest {
+
+    @TempDir
+    File tempDir;
+
+    private File invokeGetEarFile(String finalName, String classifier) throws 
Exception {
+        Method method = EarMojo.class.getDeclaredMethod("getEarFile", 
String.class, String.class, String.class);
+        method.setAccessible(true);
+        return (File) method.invoke(null, tempDir.getAbsolutePath(), 
finalName, classifier);
+    }
+
+    @Test
+    void testGetEarFileWithNullClassifier() throws Exception {
+        File result = invokeGetEarFile("myapp", null);
+        assertEquals(new File(tempDir, "myapp.ear"), result);
+    }
+
+    @Test
+    void testGetEarFileWithClassifier() throws Exception {
+        File result = invokeGetEarFile("myapp", "sources");
+        assertEquals(new File(tempDir, "myapp-sources.ear"), result);
+    }
+
+    @Test
+    void testGetEarFileWithDashPrefixedClassifier() throws Exception {
+        File result = invokeGetEarFile("myapp", "-sources");
+        assertEquals(new File(tempDir, "myapp-sources.ear"), result);
+    }
+}
diff --git a/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java 
b/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java
index 159a6f6..d36eb1a 100644
--- a/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java
+++ b/src/test/java/org/apache/maven/plugins/ear/it/EarMojoIT.java
@@ -1434,4 +1434,17 @@ class EarMojoIT extends AbstractEarPluginIT {
     void testProject102() throws Exception {
         doTestProject("project-102", new String[] 
{"eartest-ejb-sample-one-1.0.jar"});
     }
+
+    /**
+     * Builds an EAR with a whitespace-only classifier.
+     * Verifies the whitespace is trimmed and does not appear in the filename.
+     */
+    @Test
+    void testProject103() throws Exception {
+        final File baseDir = executeMojo("project-103");
+        final File badFile = new File(baseDir, 
"target/maven-ear-plugin-test-project-103-99.0   .ear");
+        final File expectedFile = new File(baseDir, 
"target/maven-ear-plugin-test-project-103-99.0.ear");
+        assertFalse(badFile.exists(), "EAR filename should not contain spaces 
from whitespace classifier");
+        assertTrue(expectedFile.exists(), "EAR archive not found at expected 
path without spaces");
+    }
 }
diff --git a/src/test/resources/projects/project-103/pom.xml 
b/src/test/resources/projects/project-103/pom.xml
new file mode 100644
index 0000000..0b55388
--- /dev/null
+++ b/src/test/resources/projects/project-103/pom.xml
@@ -0,0 +1,50 @@
+<?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/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>ear</groupId>
+  <artifactId>maven-ear-plugin-test-project-103</artifactId>
+  <version>99.0</version>
+  <name>Maven</name>
+  <packaging>ear</packaging>
+  <dependencies>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>ejb-sample-one</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ear-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <classifier>&#x20;&#x20;&#x20;</classifier>
+          <version>1.3</version>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Reply via email to