This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch agent/inline-file-name-mapping in repository https://gitbox.apache.org/repos/asf/maven-ear-plugin.git
commit 7a800009de3b5d0ad1bff7dcfeed8e38b3493316 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Mon Oct 26 14:19:47 2026 +0100 Inline the file name mapping and drop maven-mapping The plugin needs only one method of maven-mapping, and the war plugin's Maven 4 line already inlined it. --- pom.xml | 5 - .../maven/plugins/ear/AbstractEarModule.java | 3 +- .../java/org/apache/maven/plugins/ear/EarMojo.java | 5 +- .../apache/maven/plugins/ear/FileNameMapping.java | 84 ++++++++++++ .../maven/plugins/ear/FileNameMappingTest.java | 144 +++++++++++++++++++++ 5 files changed, 231 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 13f17df..39dbbc9 100644 --- a/pom.xml +++ b/pom.xml @@ -157,11 +157,6 @@ <artifactId>plexus-interpolation</artifactId> <version>1.30.0</version> </dependency> - <dependency> - <groupId>org.apache.maven.shared</groupId> - <artifactId>maven-mapping</artifactId> - <version>3.0.0</version> - </dependency> <dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-shared-utils</artifactId> diff --git a/src/main/java/org/apache/maven/plugins/ear/AbstractEarModule.java b/src/main/java/org/apache/maven/plugins/ear/AbstractEarModule.java index 4a5ca82..f83da78 100644 --- a/src/main/java/org/apache/maven/plugins/ear/AbstractEarModule.java +++ b/src/main/java/org/apache/maven/plugins/ear/AbstractEarModule.java @@ -23,7 +23,6 @@ import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.ear.util.ArtifactRepository; -import org.apache.maven.shared.mapping.MappingUtils; import org.codehaus.plexus.interpolation.InterpolationException; import org.codehaus.plexus.util.xml.XMLWriter; @@ -264,7 +263,7 @@ public abstract class AbstractEarModule implements EarModule { if (bundleFileName == null) { try { String outputFileNameMapping = earExecutionContext.getOutputFileNameMapping(); - bundleFileName = MappingUtils.evaluateFileNameMapping(outputFileNameMapping, artifact); + bundleFileName = FileNameMapping.evaluate(outputFileNameMapping, artifact); } catch (InterpolationException e) { // We currently ignore this here, cause assumption is that // has already been happened before.. 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 b949180..7d4d3ed 100644 --- a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java +++ b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java @@ -63,7 +63,6 @@ import org.apache.maven.shared.filtering.MavenFileFilter; import org.apache.maven.shared.filtering.MavenFilteringException; import org.apache.maven.shared.filtering.MavenResourcesExecution; import org.apache.maven.shared.filtering.MavenResourcesFiltering; -import org.apache.maven.shared.mapping.MappingUtils; import org.apache.maven.shared.utils.io.FileUtils; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.UnArchiver; @@ -766,7 +765,7 @@ public class EarMojo extends AbstractEarMojo { final Artifact otherModuleArtifact = otherModule.getArtifact(); if (otherModuleArtifact.isSnapshot()) { try { - artifact = workLibDir.resolve(MappingUtils.evaluateFileNameMapping( + artifact = workLibDir.resolve(FileNameMapping.evaluate( ARTIFACT_DEFAULT_FILE_NAME_MAPPING, otherModuleArtifact)); getLog().debug("Artifact with default mapping file name: " + artifact.toAbsolutePath()); } catch (InterpolationException e) { @@ -929,7 +928,7 @@ public class EarMojo extends AbstractEarMojo { if (artifact.isSnapshot()) { try { moduleClassPathIndex = classPathElements.indexOf( - MappingUtils.evaluateFileNameMapping(ARTIFACT_DEFAULT_FILE_NAME_MAPPING, artifact)); + FileNameMapping.evaluate(ARTIFACT_DEFAULT_FILE_NAME_MAPPING, artifact)); if (moduleClassPathIndex != -1) { return moduleClassPathIndex; } diff --git a/src/main/java/org/apache/maven/plugins/ear/FileNameMapping.java b/src/main/java/org/apache/maven/plugins/ear/FileNameMapping.java new file mode 100644 index 0000000..62f53b2 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/ear/FileNameMapping.java @@ -0,0 +1,84 @@ +/* + * 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.util.Properties; + +import org.apache.maven.artifact.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 or of its artifact handler can be used as a token, plus + * {@code dashClassifier} and {@code dashClassifier?}, which expand to {@code -<classifier>} or to nothing. + */ +final class FileNameMapping { + + /** + * Default file name mapping. + */ + static final String DEFAULT_FILE_NAME_MAPPING = "@{artifactId}@-@{baseVersion}@.@{extension}@"; + + /** + * Default file name mapping including classifier. + */ + static final String DEFAULT_FILE_NAME_MAPPING_CLASSIFIER = + "@{artifactId}@-@{baseVersion}@@{dashClassifier?}@.@{extension}@"; + + 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)); + interpolator.addValueSource(new ObjectBasedValueSource(artifact.getArtifactHandler())); + + // 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(); + + if (classifier != null && !classifier.isEmpty()) { + classifierMask.setProperty("dashClassifier?", "-" + classifier); + classifierMask.setProperty("dashClassifier", "-" + classifier); + } 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/ear/FileNameMappingTest.java b/src/test/java/org/apache/maven/plugins/ear/FileNameMappingTest.java new file mode 100644 index 0000000..e69dcad --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/ear/FileNameMappingTest.java @@ -0,0 +1,144 @@ +/* + * 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 org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests the mapping of file names. + */ +class FileNameMappingTest { + + @Test + void completeMapping() throws Exception { + Artifact jar = new DefaultArtifact( + "org.apache.sample", + "maven-test-lib", + "1.0", + "compile", + "jar", + null, + new DefaultArtifactHandler("jar")); + assertEquals( + "maven-test-lib-1.0.jar", FileNameMapping.evaluate("@{artifactId}@-@{version}@.@{extension}@", jar)); + } + + @Test + void noVersionMapping() throws Exception { + Artifact jar = new DefaultArtifact( + "org.apache.sample", "maven-test-lib", "1.0", null, "jar", null, new DefaultArtifactHandler("jar")); + assertEquals("maven-test-lib.jar", FileNameMapping.evaluate("@{artifactId}@.@{extension}@", jar)); + } + + @Test + void mappingWithGroupId() throws Exception { + Artifact jar = new DefaultArtifact( + "org.apache.sample", "maven-test-lib", "1.0", null, "jar", null, new DefaultArtifactHandler("jar")); + assertEquals( + "org.apache.sample-maven-test-lib-1.0.jar", + FileNameMapping.evaluate("@{groupId}@-@{artifactId}@-@{version}@.@{extension}@", jar)); + } + + @Test + void mappingWithClassifier() throws Exception { + Artifact jar = new DefaultArtifact( + "org.apache.sample", + "maven-test-lib", + "1.0", + null, + "jar", + "classifier", + new DefaultArtifactHandler("jar")); + assertEquals( + "maven-test-lib-1.0-classifier.jar", + FileNameMapping.evaluate(FileNameMapping.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, jar)); + } + + @Test + void mappingWithNullClassifier() throws Exception { + Artifact jar = new DefaultArtifact( + "org.apache.sample", "maven-test-lib", "1.0", null, "jar", null, new DefaultArtifactHandler("jar")); + assertEquals( + "maven-test-lib-1.0.jar", + FileNameMapping.evaluate(FileNameMapping.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, 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 = new DefaultArtifact( + "org.apache.sample", "maven-test-lib", "1.0", null, "jar", null, new DefaultArtifactHandler("jar")); + assertEquals( + "maven-test-lib-1.0.jar", + FileNameMapping.evaluate(FileNameMapping.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, jar)); + } + + @Test + void mappingWithEmptyClassifierShouldNotHaveTrailingDash() throws Exception { + Artifact jar = new DefaultArtifact( + "org.apache.sample", "maven-test-lib", "1.0", null, "jar", "", new DefaultArtifactHandler("jar")); + assertEquals( + "maven-test-lib-1.0.jar", + FileNameMapping.evaluate(FileNameMapping.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, 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 = new DefaultArtifact( + "org.apache.sample", "maven-test-lib", "1.0", null, "jar", null, new DefaultArtifactHandler("jar")); + assertEquals("maven-test-lib-1.0.jar", FileNameMapping.evaluate(mappingWithOptionalClassifier1, jar)); + assertEquals("maven-test-lib-1.0.jar", FileNameMapping.evaluate(mappingWithOptionalClassifier2, jar)); + + jar = new DefaultArtifact( + "org.apache.sample", + "maven-test-lib", + "1.0", + null, + "jar", + "classifier", + new DefaultArtifactHandler("jar")); + 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 = new DefaultArtifact( + "org.apache.sample", "maven-test-lib", "1.0", null, "jar", "", new DefaultArtifactHandler("jar")); + assertEquals( + "maven-test-lib-1.0.jar", + FileNameMapping.evaluate("@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@", jar)); + } +}
