You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2017/12/08 18:14:43 UTC

[maven-mapping] 02/42: [MSHARED-291] Copy code for file name mapping from Maven WAR Plugin

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

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

commit 64c0f715e74d4ebb82a39874d7040e453e6b31b6
Author: Dennis Lundberg <de...@apache.org>
AuthorDate: Fri Aug 9 10:39:31 2013 +0000

    [MSHARED-291] Copy code for file name mapping from Maven WAR Plugin
    
    Copy MappingUtils (with unit test) and DashClassifierValueSource from Maven WAR Plugin.
    Modify to fit the new package structure.
    Remove unneeded throwing of MojoExecutionException in the unit test.
    
    git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1512241 13f79535-47bb-0310-9956-ffa450edef68
---
 .../shared/mapping/DashClassifierValueSource.java  |  65 ++++++++
 .../apache/maven/shared/mapping/MappingUtils.java  |  76 ++++++++++
 .../maven/shared/mapping/MappingUtilsTest.java     | 167 +++++++++++++++++++++
 3 files changed, 308 insertions(+)

diff --git a/src/main/java/org/apache/maven/shared/mapping/DashClassifierValueSource.java b/src/main/java/org/apache/maven/shared/mapping/DashClassifierValueSource.java
new file mode 100644
index 0000000..8422512
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/mapping/DashClassifierValueSource.java
@@ -0,0 +1,65 @@
+package org.apache.maven.shared.mapping;
+
+/*
+ * 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.
+ */
+
+import java.util.Properties;
+
+import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
+
+/**
+ * This is a ValueSource, that can be used in an Interpolator. It supports
+ * special expressions, like <code>dashClassifier</code> and
+ * <code>dashClassifier?</code>.
+ *
+ * @version $Id$
+ * @since 2.5
+ */
+public class DashClassifierValueSource
+    extends PropertiesBasedValueSource
+{
+    /**
+     * Create the ValueSource.
+     *
+     * @param classifier The classifier that should be used during interpolation
+     */
+    public DashClassifierValueSource( String classifier )
+    {
+        super( createDashClassifierProperties( classifier ) );
+    }
+
+    private static Properties createDashClassifierProperties( String classifier )
+    {
+        Properties classifierMask = new Properties();
+
+        if ( classifier != null )
+        {
+            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 classifierMask;
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/mapping/MappingUtils.java b/src/main/java/org/apache/maven/shared/mapping/MappingUtils.java
new file mode 100644
index 0000000..991077b
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/mapping/MappingUtils.java
@@ -0,0 +1,76 @@
+package org.apache.maven.shared.mapping;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.artifact.Artifact;
+import org.codehaus.plexus.interpolation.InterpolationException;
+import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
+import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
+
+/**
+ * Utilities used to evaluate expression.
+ * <p/>
+ * TODO: this comes from the assembly plugin; refactor when it's shared.
+ * <p/>
+ * The expression might use any field of the {@link Artifact} interface. Some
+ * examples might be:
+ * <ul>
+ * <li>@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@</li>
+ * <li>@{artifactId}@-@{version}@.@{extension}@</li>
+ * <li>@{artifactId}@.@{extension}@</li>
+ * </ul>
+ *
+ * @author Stephane Nicoll
+ * @version $Id$
+ */
+public class MappingUtils
+{
+    public static final String DEFAULT_FILE_NAME_MAPPING = "@{artifactId}@-@{baseVersion}@.@{extension}@";
+    public static final String DEFAULT_FILE_NAME_MAPPING_CLASSIFIER =
+        "@{artifactId}@-@{baseVersion}@-@{classifier}@.@{extension}@";
+
+    /**
+     * 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 expression the evaluated expression
+     */
+    public static String evaluateFileNameMapping( String expression, Artifact artifact )
+        throws InterpolationException
+    {
+        String value = expression;
+
+        // FIXME: This is BAD! Accessors SHOULD NOT change the behavior of the object.
+        // [dennisl; 2013-07-30] This was fixed in Maven 2.0.8
+        artifact.isSnapshot();
+
+        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( new DashClassifierValueSource( artifact.getClassifier() ) );
+
+        value = interpolator.interpolate( value, "__artifact" );
+
+        return value;
+    }
+}
diff --git a/src/test/java/org/apache/maven/shared/mapping/MappingUtilsTest.java b/src/test/java/org/apache/maven/shared/mapping/MappingUtilsTest.java
new file mode 100644
index 0000000..e6d5e5c
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/mapping/MappingUtilsTest.java
@@ -0,0 +1,167 @@
+package org.apache.maven.shared.mapping;
+
+/*
+ * 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.
+ */
+
+import junit.framework.TestCase;
+
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.plugin.testing.stubs.ArtifactStub;
+import org.codehaus.plexus.interpolation.InterpolationException;
+
+/**
+ * Tests the mapping of file names.
+ *
+ * @author Stephane Nicoll
+ */
+public class MappingUtilsTest
+    extends TestCase
+{
+
+    public void testCompleteMapping()
+        throws InterpolationException
+    {
+        TestArtifactStub jar = new TestArtifactStub();
+        jar.setGroupId( "org.apache.sample" );
+        jar.setArtifactId( "maven-test-lib" );
+        jar.setVersion( "1.0" );
+        assertEquals( "maven-test-lib-1.0.jar",
+                      MappingUtils.evaluateFileNameMapping( "@{artifactId}@-@{version}@.@{extension}@", jar ) );
+
+    }
+
+    public void testNoVersionMapping()
+        throws InterpolationException
+    {
+        TestArtifactStub jar = new TestArtifactStub();
+        jar.setGroupId( "org.apache.sample" );
+        jar.setArtifactId( "maven-test-lib" );
+        jar.setVersion( "1.0" );
+        assertEquals( "maven-test-lib.jar", MappingUtils.evaluateFileNameMapping( "@{artifactId}@.@{extension}@", jar ) );
+
+    }
+
+    public void testMappingWithGroupId()
+        throws InterpolationException
+    {
+        TestArtifactStub jar = new TestArtifactStub();
+        jar.setGroupId( "org.apache.sample" );
+        jar.setArtifactId( "maven-test-lib" );
+        jar.setVersion( "1.0" );
+        assertEquals( "org.apache.sample-maven-test-lib-1.0.jar",
+                      MappingUtils.evaluateFileNameMapping( "@{groupId}@-@{artifactId}@-@{version}@.@{extension}@", jar ) );
+
+    }
+
+    public void testMappingWithClassifier()
+        throws InterpolationException
+    {
+        TestArtifactStub jar = new TestArtifactStub();
+        jar.setGroupId( "org.apache.sample" );
+        jar.setArtifactId( "maven-test-lib" );
+        jar.setVersion( "1.0" );
+        jar.setClassifier( "classifier" );
+        assertEquals( "maven-test-lib-1.0-classifier.jar",
+                      MappingUtils.evaluateFileNameMapping( MappingUtils.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, jar ) );
+    }
+
+    public void testMappingWithNullClassifier()
+        throws InterpolationException
+    {
+        TestArtifactStub jar = new TestArtifactStub();
+        jar.setGroupId( "org.apache.sample" );
+        jar.setArtifactId( "maven-test-lib" );
+        jar.setVersion( "1.0" );
+        jar.setClassifier( null );
+        assertEquals( "maven-test-lib-1.0-.jar",
+                      MappingUtils.evaluateFileNameMapping( MappingUtils.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, jar ) );
+    }
+
+    /**
+     * Test for MWAR-212.
+     */
+    public void testMappingWithOptionalClassifier()
+        throws InterpolationException
+    {
+        final String MAPPING_WITH_OPTIONAL_CLASSIFIER_1 = "@{artifactId}@-@{version}@@{dashClassifier}@.@{extension}@";
+        final String MAPPING_WITH_OPTIONAL_CLASSIFIER_2 = "@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@";
+
+        TestArtifactStub jar = new TestArtifactStub();
+        jar.setGroupId( "org.apache.sample" );
+        jar.setArtifactId( "maven-test-lib" );
+        jar.setVersion( "1.0" );
+        assertEquals( "maven-test-lib-1.0.jar",
+                      MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_1, jar ) );
+        assertEquals( "maven-test-lib-1.0.jar",
+                      MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_2, jar ) );
+
+        jar = new TestArtifactStub();
+        jar.setGroupId( "org.apache.sample" );
+        jar.setArtifactId( "maven-test-lib" );
+        jar.setVersion( "1.0" );
+        jar.setClassifier( "classifier" );
+        assertEquals( "maven-test-lib-1.0-classifier.jar",
+                      MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_1, jar ) );
+        assertEquals( "maven-test-lib-1.0-classifier.jar",
+                      MappingUtils.evaluateFileNameMapping( MAPPING_WITH_OPTIONAL_CLASSIFIER_2, jar ) );
+    }
+
+    // A very dumb stub used to test the mappings
+    class TestArtifactStub
+        extends ArtifactStub
+    {
+        /**
+         * Override in this class because there is no setter in ArtifactStub.
+         */
+        protected String classifier;
+
+        public TestArtifactStub()
+        {
+            super();
+            setType( "jar" );
+        }
+
+        /**
+         * Override in this class because ArtifactStub always returns null.
+         */
+        @Override
+        public ArtifactHandler getArtifactHandler()
+        {
+            return new DefaultArtifactHandler( getType() );
+        }
+
+        @Override
+        public String getBaseVersion()
+        {
+            return getVersion();
+        }
+
+        @Override
+        public String getClassifier()
+        {
+            return classifier;
+        }
+
+        public void setClassifier( String classifier )
+        {
+            this.classifier = classifier;
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@maven.apache.org" <co...@maven.apache.org>.