This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch 4.0.x
in repository https://gitbox.apache.org/repos/asf/maven-war-plugin.git
The following commit(s) were added to refs/heads/4.0.x by this push:
new d906bb3 Accept the documented @{token}@ syntax in
outputFileNameMapping
d906bb3 is described below
commit d906bb3d9814eb28b18b6f8ef2c1f2aaa20319ef
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Oct 26 15:05:57 2026 +0100
Accept the documented @{token}@ syntax in outputFileNameMapping
The Maven 4 port evaluated mappings with StringSearchInterpolator("@{",
"}"), which leaves an @ after every token of a documented mapping such as
@{artifactId}@.@{extension}@. Use the same FileNameMapping as the Maven 3 line.
---
.../war/packaging/AbstractWarPackagingTask.java | 51 +-------
.../war/packaging/ArtifactsPackagingTask.java | 34 ++---
.../plugins/war/packaging/FileNameMapping.java | 85 ++++++++++++
.../maven/plugins/war/WarExplodedMojoTest.java | 4 +-
.../plugins/war/packaging/FileNameMappingTest.java | 142 +++++++++++++++++++++
5 files changed, 254 insertions(+), 62 deletions(-)
diff --git
a/src/main/java/org/apache/maven/plugins/war/packaging/AbstractWarPackagingTask.java
b/src/main/java/org/apache/maven/plugins/war/packaging/AbstractWarPackagingTask.java
index 1d084aa..1780373 100644
---
a/src/main/java/org/apache/maven/plugins/war/packaging/AbstractWarPackagingTask.java
+++
b/src/main/java/org/apache/maven/plugins/war/packaging/AbstractWarPackagingTask.java
@@ -35,8 +35,6 @@ import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
import org.codehaus.plexus.interpolation.InterpolationException;
-import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
-import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.SelectorUtils;
@@ -418,10 +416,11 @@ public abstract class AbstractWarPackagingTask implements
WarPackagingTask {
return pathSet;
}
- private static final String DEFAULT_FILE_NAME_MAPPING =
"@{artifactId}-@{version}@{dashClassifier?}.@{extension}";
-
- private static final String DEFAULT_FILE_NAME_MAPPING_CLASSIFIER =
- "@{artifactId}-@{version}-@{classifier}.@{extension}";
+ /**
+ * The default file name mapping; {@code @{dashClassifier?}@} expands to
nothing when the artifact has no classifier.
+ */
+ private static final String DEFAULT_FILE_NAME_MAPPING =
+ "@{artifactId}@-@{baseVersion}@@{dashClassifier?}@.@{extension}@";
/**
* Returns the final name of the specified artifact.
@@ -435,44 +434,8 @@ public abstract class AbstractWarPackagingTask implements
WarPackagingTask {
*/
protected String getArtifactFinalName(WarPackagingContext context,
Artifact artifact)
throws InterpolationException {
- String mapping;
- if (context.getOutputFileNameMapping() != null) {
- mapping = context.getOutputFileNameMapping();
- } else {
- String classifier = artifact.getClassifier();
- if (classifier != null && !(classifier.trim().isEmpty())) {
- mapping = DEFAULT_FILE_NAME_MAPPING_CLASSIFIER;
- } else {
- mapping = DEFAULT_FILE_NAME_MAPPING;
- }
- }
- return evaluateFileNameMapping(mapping, artifact);
- }
-
- /**
- * Evaluates a file name mapping expression against a Maven 4 API artifact.
- * This replaces the use of MappingUtils.evaluateFileNameMapping which
requires the old Artifact type.
- */
- private static String evaluateFileNameMapping(String expression, Artifact
artifact) throws InterpolationException {
- StringSearchInterpolator interpolator = new
StringSearchInterpolator("@{", "}");
- interpolator.addValueSource(new ObjectBasedValueSource(artifact) {
- @Override
- public Object getValue(String expr) {
- if ("dashClassifier".equals(expr) ||
"dashClassifier?".equals(expr)) {
- String classifier = artifact.getClassifier();
- if (classifier != null && !classifier.isEmpty()) {
- return "-" + classifier;
- }
- return "";
- } else if ("version".equals(expr)) {
- return artifact.getVersion().toString();
- } else if ("baseVersion".equals(expr)) {
- return artifact.getBaseVersion().toString();
- }
- return super.getValue(expr);
- }
- });
- return interpolator.interpolate(expression);
+ String mapping = context.getOutputFileNameMapping();
+ return FileNameMapping.evaluate(mapping != null ? mapping :
DEFAULT_FILE_NAME_MAPPING, artifact);
}
/**
diff --git
a/src/main/java/org/apache/maven/plugins/war/packaging/ArtifactsPackagingTask.java
b/src/main/java/org/apache/maven/plugins/war/packaging/ArtifactsPackagingTask.java
index 5d7c880..879a5db 100644
---
a/src/main/java/org/apache/maven/plugins/war/packaging/ArtifactsPackagingTask.java
+++
b/src/main/java/org/apache/maven/plugins/war/packaging/ArtifactsPackagingTask.java
@@ -20,7 +20,9 @@ package org.apache.maven.plugins.war.packaging;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import org.apache.maven.api.DependencyScope;
import org.apache.maven.api.DownloadedArtifact;
@@ -83,10 +85,15 @@ public class ArtifactsPackagingTask extends
AbstractWarPackagingTask {
}
}
- final List<String> duplicates = findDuplicates(context, artifacts);
-
+ final List<String> finalNames = new ArrayList<>(artifacts.size());
for (DownloadedArtifact artifact : artifacts) {
- String targetFileName = getArtifactFinalName(context,
artifact);
+ finalNames.add(getArtifactFinalName(context, artifact));
+ }
+ final Set<String> duplicates = findDuplicates(finalNames);
+
+ for (int i = 0; i < artifacts.size(); i++) {
+ DownloadedArtifact artifact = artifacts.get(i);
+ String targetFileName = finalNames.get(i);
context.getLog().debug("Processing: " + targetFileName);
@@ -168,22 +175,17 @@ public class ArtifactsPackagingTask extends
AbstractWarPackagingTask {
}
/**
- * Searches a set of artifacts for duplicate filenames and returns a list
of duplicates.
+ * Returns the file names that occur more than once.
*
- * @param context the packaging context
- * @param artifacts list of artifacts
- * @return list of duplicated artifacts as bundling file names
+ * @param finalNames the final file names of the artifacts
+ * @return the duplicated file names
*/
- private List<String> findDuplicates(WarPackagingContext context,
List<DownloadedArtifact> artifacts)
- throws InterpolationException {
- List<String> duplicates = new ArrayList<>();
- List<String> identifiers = new ArrayList<>();
- for (DownloadedArtifact artifact : artifacts) {
- String candidate = getArtifactFinalName(context, artifact);
- if (identifiers.contains(candidate)) {
+ private static Set<String> findDuplicates(List<String> finalNames) {
+ Set<String> duplicates = new HashSet<>();
+ Set<String> identifiers = new HashSet<>();
+ for (String candidate : finalNames) {
+ if (!identifiers.add(candidate)) {
duplicates.add(candidate);
- } else {
- identifiers.add(candidate);
}
}
return duplicates;
diff --git
a/src/main/java/org/apache/maven/plugins/war/packaging/FileNameMapping.java
b/src/main/java/org/apache/maven/plugins/war/packaging/FileNameMapping.java
new file mode 100644
index 0000000..5d99b16
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/war/packaging/FileNameMapping.java
@@ -0,0 +1,85 @@
+/*
+ * 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.war.packaging;
+
+import java.util.Properties;
+
+import org.apache.maven.api.Artifact;
+import org.codehaus.plexus.interpolation.InterpolationException;
+import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
+import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
+import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
+
+/**
+ * Evaluates file name mapping expressions such as {@code
@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@}
+ * against an {@link Artifact}. Any getter of the artifact can be used as a
token, plus
+ * {@code dashClassifier} and {@code dashClassifier?}, which expand to {@code
-<classifier>} or, for a blank classifier, to nothing.
+ * The syntax and the classifier handling match the Maven 3 line of this
plugin.
+ */
+final class FileNameMapping {
+
+ private FileNameMapping() {
+ // prevent instantiation.
+ }
+
+ /**
+ * Evaluates the specified expression for the given artifact.
+ *
+ * @param expression the expression to evaluate
+ * @param artifact the artifact to use as value object for tokens
+ * @return the evaluated expression
+ * @throws InterpolationException in case of an error
+ */
+ static String evaluate(String expression, Artifact artifact) throws
InterpolationException {
+ RegexBasedInterpolator interpolator = new
RegexBasedInterpolator("\\@\\{(", ")?([^}]+)\\}@");
+ interpolator.addValueSource(new ObjectBasedValueSource(artifact) {
+ @Override
+ public Object getValue(String expression) {
+ // Version objects render through toString()
+ if ("version".equals(expression)) {
+ return artifact.getVersion().toString();
+ } else if ("baseVersion".equals(expression)) {
+ return artifact.getBaseVersion().toString();
+ }
+ return super.getValue(expression);
+ }
+ });
+
+ // Support for special expressions, like @{dashClassifier?}@, see
MWAR-212
+
interpolator.addValueSource(dashClassifierValueSource(artifact.getClassifier()));
+
+ return interpolator.interpolate(expression, "__artifact");
+ }
+
+ private static PropertiesBasedValueSource dashClassifierValueSource(String
classifier) {
+ Properties classifierMask = new Properties();
+ String trimmed = classifier == null ? "" : classifier.trim();
+
+ if (!trimmed.isEmpty()) {
+ classifierMask.setProperty("dashClassifier?", "-" + trimmed);
+ classifierMask.setProperty("dashClassifier", "-" + trimmed);
+ } else {
+ classifierMask.setProperty("dashClassifier?", "");
+ classifierMask.setProperty("dashClassifier", "");
+ // Make sure that the classifier property is usable, if it is null
+ classifierMask.setProperty("classifier", "");
+ }
+ return new PropertiesBasedValueSource(classifierMask);
+ }
+}
diff --git
a/src/test/java/org/apache/maven/plugins/war/WarExplodedMojoTest.java
b/src/test/java/org/apache/maven/plugins/war/WarExplodedMojoTest.java
index 5b66c05..54ee9c6 100644
--- a/src/test/java/org/apache/maven/plugins/war/WarExplodedMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/war/WarExplodedMojoTest.java
@@ -918,7 +918,7 @@ public class WarExplodedMojoTest {
@MojoParameter(
name = "webappDirectory",
value =
"target/test-classes/unit/warexplodedmojo/ExplodedWarWithFileNameMapping")
- @MojoParameter(name = "outputFileNameMapping", value =
"@{artifactId}.@{extension}")
+ @MojoParameter(name = "outputFileNameMapping", value =
"@{artifactId}@.@{extension}@")
@Test
public void testExplodedWarWithOutputFileNameMapping(WarExplodedMojo mojo)
throws Exception {
// configure mojo
@@ -958,7 +958,7 @@ public class WarExplodedMojoTest {
@MojoParameter(
name = "webappDirectory",
value =
"target/test-classes/unit/warexplodedmojo/ExplodedWarWithFileNameMappingAndDuplicateDependencies")
- @MojoParameter(name = "outputFileNameMapping", value =
"@{artifactId}.@{extension}")
+ @MojoParameter(name = "outputFileNameMapping", value =
"@{artifactId}@.@{extension}@")
@Test
public void
testExplodedWarWithOutputFileNameMappingAndDuplicateDependencies(WarExplodedMojo
mojo)
throws Exception {
diff --git
a/src/test/java/org/apache/maven/plugins/war/packaging/FileNameMappingTest.java
b/src/test/java/org/apache/maven/plugins/war/packaging/FileNameMappingTest.java
new file mode 100644
index 0000000..1f0fab2
--- /dev/null
+++
b/src/test/java/org/apache/maven/plugins/war/packaging/FileNameMappingTest.java
@@ -0,0 +1,142 @@
+/*
+ * 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.war.packaging;
+
+import org.apache.maven.api.Artifact;
+import org.apache.maven.api.plugin.testing.stubs.ArtifactStub;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests the mapping of file names.
+ */
+class FileNameMappingTest {
+
+ private static final String DEFAULT_MAPPING =
"@{artifactId}@-@{baseVersion}@@{dashClassifier?}@.@{extension}@";
+
+ @Test
+ void completeMapping() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
null);
+ assertEquals(
+ "maven-test-lib-1.0.jar",
FileNameMapping.evaluate("@{artifactId}@-@{version}@.@{extension}@", jar));
+ }
+
+ @Test
+ void noVersionMapping() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
null);
+ assertEquals("maven-test-lib.jar",
FileNameMapping.evaluate("@{artifactId}@.@{extension}@", jar));
+ }
+
+ @Test
+ void mappingWithGroupId() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
null);
+ assertEquals(
+ "org.apache.sample-maven-test-lib-1.0.jar",
+
FileNameMapping.evaluate("@{groupId}@-@{artifactId}@-@{version}@.@{extension}@",
jar));
+ }
+
+ @Test
+ void mappingWithClassifier() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
"classifier");
+ assertEquals("maven-test-lib-1.0-classifier.jar",
FileNameMapping.evaluate(DEFAULT_MAPPING, jar));
+ }
+
+ @Test
+ void mappingWithNullClassifier() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
null);
+ assertEquals("maven-test-lib-1.0.jar",
FileNameMapping.evaluate(DEFAULT_MAPPING, jar));
+ }
+
+ /**
+ * Test for #65. When classifier is null, the default classifier mapping
should
+ * produce a clean filename without a trailing dash.
+ */
+ @Test
+ void mappingWithNullClassifierShouldNotHaveTrailingDash() throws Exception
{
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
null);
+ assertEquals("maven-test-lib-1.0.jar",
FileNameMapping.evaluate(DEFAULT_MAPPING, jar));
+ }
+
+ @Test
+ void mappingWithEmptyClassifierShouldNotHaveTrailingDash() throws
Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
"");
+ assertEquals("maven-test-lib-1.0.jar",
FileNameMapping.evaluate(DEFAULT_MAPPING, jar));
+ }
+
+ /**
+ * Test for MWAR-212.
+ */
+ @Test
+ void mappingWithOptionalClassifier() throws Exception {
+ final String mappingWithOptionalClassifier1 =
"@{artifactId}@-@{version}@@{dashClassifier}@.@{extension}@";
+ final String mappingWithOptionalClassifier2 =
"@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@";
+
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
null);
+ assertEquals("maven-test-lib-1.0.jar",
FileNameMapping.evaluate(mappingWithOptionalClassifier1, jar));
+ assertEquals("maven-test-lib-1.0.jar",
FileNameMapping.evaluate(mappingWithOptionalClassifier2, jar));
+
+ jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
"classifier");
+ assertEquals(
+ "maven-test-lib-1.0-classifier.jar",
FileNameMapping.evaluate(mappingWithOptionalClassifier1, jar));
+ assertEquals(
+ "maven-test-lib-1.0-classifier.jar",
FileNameMapping.evaluate(mappingWithOptionalClassifier2, jar));
+ }
+
+ @Test
+ void emptyClassifierWithOptionalDashClassifier() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
"");
+ assertEquals(
+ "maven-test-lib-1.0.jar",
+
FileNameMapping.evaluate("@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@",
jar));
+ }
+
+ @Test
+ void blankClassifierWithOptionalDashClassifier() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
" ");
+ assertEquals("maven-test-lib-1.0.jar",
FileNameMapping.evaluate(DEFAULT_MAPPING, jar));
+ }
+
+ @Test
+ void classifierIsTrimmedInDashClassifier() throws Exception {
+ Artifact jar = artifact("org.apache.sample", "maven-test-lib", "1.0",
" tests ");
+ assertEquals("maven-test-lib-1.0-tests.jar",
FileNameMapping.evaluate(DEFAULT_MAPPING, jar));
+ }
+
+ @Test
+ void snapshotUsesBaseVersionByDefault() throws Exception {
+ ArtifactStub jar = artifact("org.apache.sample", "maven-test-lib",
"1.0-20261026.101010-1", null);
+ jar.setBaseVersion("1.0-SNAPSHOT");
+ assertEquals("maven-test-lib-1.0-SNAPSHOT.jar",
FileNameMapping.evaluate(DEFAULT_MAPPING, jar));
+ assertEquals(
+ "maven-test-lib-1.0-20261026.101010-1.jar",
+
FileNameMapping.evaluate("@{artifactId}@-@{version}@.@{extension}@", jar));
+ }
+
+ private static ArtifactStub artifact(String groupId, String artifactId,
String version, String classifier) {
+ ArtifactStub artifact = new ArtifactStub();
+ artifact.setGroupId(groupId);
+ artifact.setArtifactId(artifactId);
+ artifact.setVersion(version);
+ artifact.setBaseVersion(version);
+ artifact.setClassifier(classifier);
+ artifact.setExtension("jar");
+ return artifact;
+ }
+}