This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch fix-whitespace-classifier-tdd in repository https://gitbox.apache.org/repos/asf/maven-ear-plugin.git
commit baf107c3d1b03d9c08203ddf8e06c5e3489ffeaf Author: Elliotte Rusty Harold <[email protected]> AuthorDate: Thu Jul 16 14:55:00 2026 +0000 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 --- .../java/org/apache/maven/plugins/ear/EarMojo.java | 7 ++- .../org/apache/maven/plugins/ear/EarMojoTest.java | 63 ++++++++++++++++++++++ .../org/apache/maven/plugins/ear/it/EarMojoIT.java | 13 +++++ src/test/resources/projects/project-103/pom.xml | 50 +++++++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) 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..23c9ac0 100644 --- a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java +++ b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java @@ -560,9 +560,12 @@ public class EarMojo extends AbstractEarMojo { * @return the EAR file to generate */ private static File getEarFile(String basedir, String finalName, String classifier) { - if (classifier == null) { + if (classifier != null) { + classifier = classifier.trim(); + } + if (classifier == null || classifier.isEmpty()) { 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..7d335f2 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/ear/EarMojoTest.java @@ -0,0 +1,63 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class EarMojoTest { + + private static File invokeGetEarFile(String basedir, 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, basedir, finalName, classifier); + } + + @Test + void testGetEarFileWithNullClassifier() throws Exception { + File result = invokeGetEarFile("/tmp", "myapp", null); + assertEquals(new File("/tmp", "myapp.ear"), result); + } + + @Test + void testGetEarFileWithClassifier() throws Exception { + File result = invokeGetEarFile("/tmp", "myapp", "sources"); + assertEquals(new File("/tmp", "myapp-sources.ear"), result); + } + + @Test + void testGetEarFileWithDashPrefixedClassifier() throws Exception { + File result = invokeGetEarFile("/tmp", "myapp", "-sources"); + assertEquals(new File("/tmp", "myapp-sources.ear"), result); + } + + @Test + void testGetEarFileWithWhitespaceOnlyClassifier() throws Exception { + File result = invokeGetEarFile("/tmp", "myapp", " "); + assertEquals(new File("/tmp", "myapp.ear"), result); + assertFalse( + result.getAbsolutePath().contains(" "), + "EAR filename should not contain spaces from whitespace-only classifier"); + } +} 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>   </classifier> + <version>1.3</version> + </configuration> + </plugin> + </plugins> + </build> +</project>
