You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by pg...@apache.org on 2010/05/21 23:04:33 UTC

svn commit: r947173 - in /maven/plugins/trunk/maven-antrun-plugin/src: main/java/org/apache/maven/ant/tasks/support/ main/java/org/apache/maven/plugin/antrun/ site/apt/tasks/

Author: pgier
Date: Fri May 21 21:04:32 2010
New Revision: 947173

URL: http://svn.apache.org/viewvc?rev=947173&view=rev
Log:
[MANTRUN-138] Add version mapper to antrun plugin.

Added:
    maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java   (with props)
    maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/versionMapper.apt.vm   (with props)
Modified:
    maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java
    maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/tasks.apt.vm

Added: maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java?rev=947173&view=auto
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java (added)
+++ maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java Fri May 21 21:04:32 2010
@@ -0,0 +1,94 @@
+package org.apache.maven.ant.tasks.support;
+
+/*
+ * 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.io.File;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.tools.ant.util.FileNameMapper;
+import org.codehaus.plexus.util.StringUtils;
+
+/**
+ * Ant filename mapper to remove version info from filename when copying dependencies.
+ *
+ * @author <a href="mailto:hboutemy@apache.org">Herve Boutemy</a>
+ * @version $Id$
+ */
+public class VersionMapper
+    implements FileNameMapper, Comparator
+{
+    private List versions;
+
+    private String to;
+
+    public String[] mapFileName( String sourceFileName )
+    {
+        String originalFileName = new File( sourceFileName ).getName();
+        for ( Iterator iter = versions.iterator(); iter.hasNext(); )
+        {
+            String version = (String) iter.next();
+            int index = originalFileName.indexOf( version );
+            if ( index >= 0 )
+            {
+                // remove version in artifactId-version(-classifier).type
+                String baseFilename = originalFileName.substring( 0, index - 1 );
+                String extension = originalFileName.substring( index + version.length() );
+                String path = sourceFileName.substring( 0, sourceFileName.length() - originalFileName.length() );
+                if ( "flatten".equals( to ) )
+                {
+                    path = "";
+                }
+                return new String[] { path + baseFilename + extension };
+            }
+        }
+        return new String[] { sourceFileName };
+    }
+
+    /**
+     * Set the versions identifiers that this mapper can remove from filenames. The separator value used is path
+     * separator, as used by dependencies task when setting <code>versionsId</code> property value.
+     */
+    public void setFrom( String from )
+    {
+        String[] split = StringUtils.split( from, File.pathSeparator );
+        // sort, from lengthiest to smallest
+        Arrays.sort( split, this );
+        versions = Arrays.asList( split );
+    }
+
+    /**
+     * By default, only filename is changed, but if this attribute is set to <code>flatten</code>, directory is removed.
+     */
+    public void setTo( String to )
+    {
+        this.to = to;
+    }
+
+    public int compare( Object o1, Object o2 )
+    {
+        String s1 = (String) o1;
+        String s2 = (String) o2;
+        int lengthDiff = s2.length() - s1.length();
+        return ( lengthDiff != 0 ) ? lengthDiff : s1.compareTo( s2 );
+    }
+}

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java?rev=947173&r1=947172&r2=947173&view=diff
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java (original)
+++ maven/plugins/trunk/maven-antrun-plugin/src/main/java/org/apache/maven/plugin/antrun/AbstractAntMojo.java Fri May 21 21:04:32 2010
@@ -108,6 +108,14 @@ public abstract class AbstractAntMojo
     private String taskNamespace;
     
     /**
+     * The name of a property containing the list of all dependency versions.
+     * This is used for the removing the versions from the filenames.
+     * 
+     * @parameter default-value="maven.project.dependencies.versions"
+     */
+    private String versionsPropertyName;
+    
+    /**
      * @deprecated use {@link AbstractAntMojo#executeTasks(Target,MavenProject,List)}.
      */
     /*protected void executeTasks( Target antTasks, MavenProject mavenProject )
@@ -272,7 +280,7 @@ public abstract class AbstractAntMojo
         antProject.setProperty( ( propertyPrefix + "project.build.testSourceDirectory" ), mavenProject.getBuild().getTestSourceDirectory() );
         
         // Add properties for depenedency artifacts
-        Set depArtifacts = mavenProject.getDependencyArtifacts();
+        Set depArtifacts = mavenProject.getArtifacts();
         for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
         {
             Artifact artifact = (Artifact) it.next();
@@ -282,7 +290,19 @@ public abstract class AbstractAntMojo
             antProject.setProperty( propertyPrefix + propName, artifact.getFile().getPath() );
         }
         
+        // Add a property containing the list of versions for the mapper
+        StringBuffer versionsBuffer = new StringBuffer();
+        for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+
+            versionsBuffer.append( artifact.getVersion() + File.pathSeparator );
+        }
+        antProject.setProperty( versionsPropertyName, versionsBuffer.toString() );
+        
+        
         // Add properties in deprecated format to depenedency artifacts
+        // This should be removed in future versions of the antrun plugin.
         for ( Iterator it = depArtifacts.iterator(); it.hasNext(); )
         {
             Artifact artifact = (Artifact) it.next();

Modified: maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/tasks.apt.vm
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/tasks.apt.vm?rev=947173&r1=947172&r2=947173&view=diff
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/tasks.apt.vm (original)
+++ maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/tasks.apt.vm Fri May 21 21:04:32 2010
@@ -39,4 +39,6 @@ Ant Tasks
 *---------------+--------------------------------------------------------+
 | {{{./dependencyFilesets.html}dependencyfilesets}}   | Creates a fileset for each of the Maven project dependencies and adds them to the Ant build   | 
 *---------------+--------------------------------------------------------+
+| {{{./versionMapper.html}versionMapper}}     | Mapper to remove version from artifact filenames   | 
+*---------------+--------------------------------------------------------+
    
\ No newline at end of file

Added: maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/versionMapper.apt.vm
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/versionMapper.apt.vm?rev=947173&view=auto
==============================================================================
--- maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/versionMapper.apt.vm (added)
+++ maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/versionMapper.apt.vm Fri May 21 21:04:32 2010
@@ -0,0 +1,69 @@
+ ------
+ VersionMapper
+ ------
+ Paul Gier
+ ------
+ May 2010
+ ------
+
+ ~~ 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.
+
+ ~~ NOTE: For help with the syntax of this file, see:
+ ~~ http://maven.apache.org/doxia/references/apt-format.html
+ 
+VersionMapper
+
+  This is an Ant mapper class which can be used to remove the versions from the dependency
+  artifact filenames..
+
+* Example
+
+  This example shows how to use the version mapper.
+  
+------
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>copy-dependencies</id>
+            <phase>package</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+            <configuration>
+              <tasks>
+                <mapper id="remove-versions"
+                        classname="org.apache.maven.ant.tasks.support.VersionMapper"
+                        from="${maven.project.dependencies.versions}" 
+                        to="flatten" />
+                <copy todir="lib" flatten="true">
+                  <path>
+                    <pathelement path="${maven.dependency.classpath}"/>
+                  </path>
+                  <mapper refid="remove-versions" />
+                </copy>
+              </tasks>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
+------
+

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/versionMapper.apt.vm
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-antrun-plugin/src/site/apt/tasks/versionMapper.apt.vm
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision