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/20 09:28:13 UTC

[maven-ant-plugin] 15/42: [MANT-46] mvn ant:ant throws a StringIndexOutOfBoundsException

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

hboutemy pushed a commit to annotated tag maven-ant-plugin-2.1.1
in repository https://gitbox.apache.org/repos/asf/maven-ant-plugin.git

commit 1118cde3d9fe5e0bf18df5c9230035081bab5cd0
Author: Benjamin Bentmann <be...@apache.org>
AuthorDate: Tue Jul 29 10:49:16 2008 +0000

    [MANT-46] mvn ant:ant throws a StringIndexOutOfBoundsException
    
    git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-ant-plugin@680669 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  7 +--
 src/it/relative-path-it/goals.txt                  |  3 +
 src/it/relative-path-it/pom.xml                    | 60 +++++++++++++++++++
 src/it/relative-path-it/verify.bsh                 | 61 ++++++++++++++++++++
 .../apache/maven/plugin/ant/AntBuildWriter.java    | 39 ++++++-------
 .../maven/plugin/ant/AntBuildWriterUtil.java       | 67 +++++++++++++++++++++-
 .../maven/plugin/ant/AntBuildWriterUtilTest.java   | 24 +++++++-
 7 files changed, 232 insertions(+), 29 deletions(-)

diff --git a/pom.xml b/pom.xml
index 368bd9e..298140a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -72,11 +72,6 @@ under the License.
       <version>2.0.6</version>
     </dependency>
     <dependency>
-      <groupId>org.apache.maven.wagon</groupId>
-      <artifactId>wagon-provider-api</artifactId>
-      <version>1.0-beta-2</version>
-    </dependency>
-    <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-plugin-api</artifactId>
       <version>2.0.2</version>
@@ -84,7 +79,7 @@ under the License.
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
-      <version>1.5.4</version>
+      <version>1.5.5</version>
     </dependency>
     <dependency>
       <groupId>ant</groupId>
diff --git a/src/it/relative-path-it/goals.txt b/src/it/relative-path-it/goals.txt
new file mode 100644
index 0000000..4524f24
--- /dev/null
+++ b/src/it/relative-path-it/goals.txt
@@ -0,0 +1,3 @@
+clean
+ant:ant
+antrun:run
diff --git a/src/it/relative-path-it/pom.xml b/src/it/relative-path-it/pom.xml
new file mode 100644
index 0000000..957f0e0
--- /dev/null
+++ b/src/it/relative-path-it/pom.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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>org.apache.maven.plugins.maven-ant-plugin.it</groupId>
+  <artifactId>relative-path-it</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ant-plugin</artifactId>
+        <version>@pom.version@</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-clean-plugin</artifactId>
+        <version>2.2</version>
+        <configuration>
+          <filesets>
+            <fileset>
+              <directory>${basedir}</directory>
+              <includes>
+                <include>build.xml</include>
+                <include>maven-build.*</include>
+              </includes>
+            </fileset>
+          </filesets>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <version>1.1</version>
+        <configuration>
+          <tasks>
+            <ant dir="${basedir}" antfile="${basedir}/build.xml" target="package" />
+          </tasks>
+        </configuration>
+      </plugin>
+    </plugins>
+    <resources>
+     <resource>
+        <directory>src/main/resources</directory>
+      </resource>
+      <resource>
+        <directory>.</directory>
+        <targetPath>META-INF</targetPath>
+        <includes>
+          <include>NOTICE.txt</include>
+          <include>LICENSE.txt</include>
+        </includes>
+      </resource>
+    </resources>
+  </build>
+
+</project>
diff --git a/src/it/relative-path-it/verify.bsh b/src/it/relative-path-it/verify.bsh
new file mode 100644
index 0000000..4a32ec9
--- /dev/null
+++ b/src/it/relative-path-it/verify.bsh
@@ -0,0 +1,61 @@
+import java.io.*;
+import java.util.*;
+import java.util.regex.*;
+
+import org.codehaus.plexus.util.*;
+
+boolean checkValue( String expected, String key, Properties props )
+{
+    String actual = props.getProperty( key );
+    if ( !expected.equals( actual ) )
+    {
+        System.err.println( "Generated build properties contain bad value for key " + key + ": expected \"" + expected
+                            + "\" but was \"" + actual + "\"" );
+        return false;
+    }
+    return true;
+}
+
+try
+{
+    File propsFile = new File( basedir, "maven-build.properties" );
+    Properties props = new Properties();
+    InputStream in = null;
+    try
+    {
+        in = new FileInputStream( propsFile );
+        props.load( in );
+    }
+    finally
+    {
+        IOUtil.close( in );
+    }
+
+    if ( !checkValue( "target", "maven.build.dir", props ) )
+    {
+        return false;
+    }
+    if ( !checkValue( "src/main/java", "maven.build.srcDir.0", props ) )
+    {
+        return false;
+    }
+    if ( !checkValue( "src/test/java", "maven.build.testDir.0", props ) )
+    {
+        return false;
+    }
+    if ( !checkValue( "src/main/resources", "maven.build.resourceDir.0", props ) )
+    {
+        return false;
+    }
+    if ( !checkValue( ".", "maven.build.resourceDir.1", props ) )
+    {
+        return false;
+    }
+}
+catch( Throwable t )
+{
+    t.printStackTrace();
+    return false;
+}
+
+return true;
diff --git a/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java b/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
index 8cada3a..61f22dd 100644
--- a/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
+++ b/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
@@ -36,7 +36,6 @@ import org.apache.maven.model.Repository;
 import org.apache.maven.model.Resource;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Settings;
-import org.apache.maven.wagon.PathUtils;
 import org.apache.tools.ant.Main;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
@@ -138,17 +137,17 @@ public class AntBuildWriter
         // Build properties
         // ----------------------------------------------------------------------
 
-        addProperty( properties, "maven.build.finalName", PathUtils.toRelative( project.getBasedir(), project
+        addProperty( properties, "maven.build.finalName", AntBuildWriterUtil.toRelative( project.getBasedir(), project
             .getBuild().getFinalName() ) );
 
         // target
-        addProperty( properties, "maven.build.dir", PathUtils.toRelative( project.getBasedir(), project.getBuild()
+        addProperty( properties, "maven.build.dir", AntBuildWriterUtil.toRelative( project.getBasedir(), project.getBuild()
             .getDirectory() ) );
         addProperty( properties, "project.build.directory", "${maven.build.dir}" );
 
         // ${maven.build.dir}/classes
         addProperty( properties, "maven.build.outputDir", "${maven.build.dir}/"
-            + PathUtils.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
+            + AntBuildWriterUtil.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
                                     project.getBuild().getOutputDirectory() ) );
         addProperty( properties, "project.build.outputDirectory", "${maven.build.outputDir}" );
 
@@ -158,7 +157,7 @@ public class AntBuildWriter
             String[] compileSourceRoots = (String[]) project.getCompileSourceRoots().toArray( new String[0] );
             for ( int i = 0; i < compileSourceRoots.length; i++ )
             {
-                addProperty( properties, "maven.build.srcDir." + i, PathUtils.toRelative( project.getBasedir(),
+                addProperty( properties, "maven.build.srcDir." + i, AntBuildWriterUtil.toRelative( project.getBasedir(),
                                                                                           compileSourceRoots[i] ) );
             }
         }
@@ -168,14 +167,14 @@ public class AntBuildWriter
             Resource[] array = (Resource[]) project.getBuild().getResources().toArray( new Resource[0] );
             for ( int i = 0; i < array.length; i++ )
             {
-                addProperty( properties, "maven.build.resourceDir." + i, PathUtils.toRelative( project.getBasedir(),
+                addProperty( properties, "maven.build.resourceDir." + i, AntBuildWriterUtil.toRelative( project.getBasedir(),
                                                                                                array[i].getDirectory() ) );
             }
         }
 
         // ${maven.build.dir}/test-classes
         addProperty( properties, "maven.build.testOutputDir", "${maven.build.dir}/"
-            + PathUtils.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
+            + AntBuildWriterUtil.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
                                     project.getBuild().getTestOutputDirectory() ) );
         // src/test/java
         if ( !project.getTestCompileSourceRoots().isEmpty() )
@@ -183,7 +182,7 @@ public class AntBuildWriter
             String[] compileSourceRoots = (String[]) project.getTestCompileSourceRoots().toArray( new String[0] );
             for ( int i = 0; i < compileSourceRoots.length; i++ )
             {
-                addProperty( properties, "maven.build.testDir." + i, PathUtils.toRelative( project.getBasedir(),
+                addProperty( properties, "maven.build.testDir." + i, AntBuildWriterUtil.toRelative( project.getBasedir(),
                                                                                            compileSourceRoots[i] ) );
             }
         }
@@ -193,7 +192,7 @@ public class AntBuildWriter
             Resource[] array = (Resource[]) project.getBuild().getTestResources().toArray( new Resource[0] );
             for ( int i = 0; i < array.length; i++ )
             {
-                addProperty( properties, "maven.build.testResourceDir." + i, PathUtils
+                addProperty( properties, "maven.build.testResourceDir." + i, AntBuildWriterUtil
                     .toRelative( project.getBasedir(), array[i].getDirectory() ) );
             }
         }
@@ -435,13 +434,13 @@ public class AntBuildWriter
 
         writer.startElement( "property" );
         writer.addAttribute( "name", "maven.build.dir" );
-        writer.addAttribute( "value", PathUtils.toRelative( project.getBasedir(), project.getBuild().getDirectory() ) );
+        writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), project.getBuild().getDirectory() ) );
         writer.endElement(); // property
 
         writer.startElement( "property" );
         writer.addAttribute( "name", "maven.build.outputDir" );
         writer.addAttribute( "value", "${maven.build.dir}/"
-            + PathUtils.toRelative( new File( project.getBuild().getDirectory() ), project.getBuild()
+            + AntBuildWriterUtil.toRelative( new File( project.getBuild().getDirectory() ), project.getBuild()
                 .getOutputDirectory() ) );
         writer.endElement(); // property
 
@@ -452,7 +451,7 @@ public class AntBuildWriter
             {
                 writer.startElement( "property" );
                 writer.addAttribute( "name", "maven.build.srcDir." + i );
-                writer.addAttribute( "value", PathUtils.toRelative( project.getBasedir(), compileSourceRoots[i] ) );
+                writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), compileSourceRoots[i] ) );
                 writer.endElement(); // property
             }
         }
@@ -464,7 +463,7 @@ public class AntBuildWriter
             {
                 writer.startElement( "property" );
                 writer.addAttribute( "name", "maven.build.resourceDir." + i );
-                writer.addAttribute( "value", PathUtils.toRelative( project.getBasedir(), array[i].getDirectory() ) );
+                writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), array[i].getDirectory() ) );
                 writer.endElement(); // property
             }
         }
@@ -472,7 +471,7 @@ public class AntBuildWriter
         writer.startElement( "property" );
         writer.addAttribute( "name", "maven.build.testOutputDir" );
         writer.addAttribute( "value", "${maven.build.dir}/"
-            + PathUtils.toRelative( new File( project.getBuild().getDirectory() ), project.getBuild()
+            + AntBuildWriterUtil.toRelative( new File( project.getBuild().getDirectory() ), project.getBuild()
                 .getTestOutputDirectory() ) );
         writer.endElement(); // property
 
@@ -483,7 +482,7 @@ public class AntBuildWriter
             {
                 writer.startElement( "property" );
                 writer.addAttribute( "name", "maven.build.testDir." + i );
-                writer.addAttribute( "value", PathUtils.toRelative( project.getBasedir(), compileSourceRoots[i] ) );
+                writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), compileSourceRoots[i] ) );
                 writer.endElement(); // property
             }
         }
@@ -495,7 +494,7 @@ public class AntBuildWriter
             {
                 writer.startElement( "property" );
                 writer.addAttribute( "name", "maven.build.testResourceDir." + i );
-                writer.addAttribute( "value", PathUtils.toRelative( project.getBasedir(), array[i].getDirectory() ) );
+                writer.addAttribute( "value", AntBuildWriterUtil.toRelative( project.getBasedir(), array[i].getDirectory() ) );
                 writer.endElement(); // property
             }
         }
@@ -514,7 +513,7 @@ public class AntBuildWriter
         writer.startElement( "property" );
         writer.addAttribute( "name", "maven.reporting.outputDirectory" );
         writer.addAttribute( "value", "${maven.build.dir}/"
-            + PathUtils.toRelative( new File( project.getBuild().getDirectory() ), reportingOutputDir ) );
+            + AntBuildWriterUtil.toRelative( new File( project.getBuild().getDirectory() ), reportingOutputDir ) );
         writer.endElement(); // property
 
         // ----------------------------------------------------------------------
@@ -585,7 +584,7 @@ public class AntBuildWriter
             {
                 Artifact artifact = (Artifact) i.next();
                 writer.startElement( "include" );
-                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
+                writer.addAttribute( "name", AntBuildWriterUtil.toRelative( localRepository, artifact.getFile().getPath() ) );
                 writer.endElement(); // include
             }
         }
@@ -608,7 +607,7 @@ public class AntBuildWriter
             {
                 Artifact artifact = (Artifact) i.next();
                 writer.startElement( "include" );
-                writer.addAttribute( "name", PathUtils.toRelative( localRepository, artifact.getFile().getPath() ) );
+                writer.addAttribute( "name", AntBuildWriterUtil.toRelative( localRepository, artifact.getFile().getPath() ) );
                 writer.endElement(); // include
             }
         }
@@ -1253,7 +1252,7 @@ public class AntBuildWriter
             Artifact artifact = (Artifact) i.next();
 
             // TODO: should the artifacthandler be used instead?
-            String path = PathUtils.toRelative( localRepository, artifact.getFile().getPath() );
+            String path = AntBuildWriterUtil.toRelative( localRepository, artifact.getFile().getPath() );
 
             if ( !new File( path ).exists() )
             {
diff --git a/src/main/java/org/apache/maven/plugin/ant/AntBuildWriterUtil.java b/src/main/java/org/apache/maven/plugin/ant/AntBuildWriterUtil.java
index 0da93f8..333f306 100644
--- a/src/main/java/org/apache/maven/plugin/ant/AntBuildWriterUtil.java
+++ b/src/main/java/org/apache/maven/plugin/ant/AntBuildWriterUtil.java
@@ -37,8 +37,8 @@ import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.model.ReportPlugin;
 import org.apache.maven.project.MavenProject;
-import org.apache.maven.wagon.PathUtils;
 import org.apache.xpath.XPathAPI;
+import org.codehaus.plexus.util.PathTool;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.XMLWriter;
 import org.codehaus.plexus.util.xml.XmlWriterUtil;
@@ -157,7 +157,7 @@ public class AntBuildWriterUtil
     {
         writer.startElement( "ant" );
         writer.addAttribute( "antfile", "build.xml" );
-        writer.addAttribute( "dir", PathUtils.toRelative( project.getBasedir(), moduleSubPath ) );
+        writer.addAttribute( "dir", toRelative( project.getBasedir(), moduleSubPath ) );
         writer.addAttribute( "target", tasks );
         writer.endElement(); // ant
     }
@@ -1269,4 +1269,67 @@ public class AntBuildWriterUtil
         return singularForm;
     }
 
+    /**
+     * Relativizes the specified path against the given base directory (if possible). If the specified path is a
+     * subdirectory of the base directory, the base directory prefix will be chopped off. If the specified path is equal
+     * to the base directory, the path "." is returned. Otherwise, the path is returned as is. Examples:
+     * <table border="1">
+     * <tr>
+     * <td>basedir</td>
+     * <td>path</td>
+     * <td>result</td>
+     * </tr>
+     * <tr>
+     * <td>/home</td>
+     * <td>/home/dir</td>
+     * <td>dir</td>
+     * </tr>
+     * <tr>
+     * <td>/home</td>
+     * <td>/home/dir/</td>
+     * <td>dir/</td>
+     * </tr>
+     * <tr>
+     * <td>/home</td>
+     * <td>/home</td>
+     * <td>.</td>
+     * </tr>
+     * <tr>
+     * <td>/home</td>
+     * <td>/home/</td>
+     * <td>./</td>
+     * </tr>
+     * <tr>
+     * <td>/home</td>
+     * <td>dir</td>
+     * <td>dir</td>
+     * </tr>
+     * </table>
+     * The returned path will always use the forward slash ('/') as the file separator regardless of the current
+     * platform. Also, the result path will have a trailing slash if the input path has a trailing file separator.
+     * 
+     * @param basedir The base directory to relativize the path against, must not be <code>null</code>.
+     * @param path The path to relativize, must not be <code>null</code>.
+     * @return The relativized path, never <code>null</code>.
+     */
+    static String toRelative( File basedir, String path )
+    {
+        String result = null;
+        if ( new File( path ).isAbsolute() )
+        {
+            String pathNormalized = path.replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
+            result = PathTool.getRelativeFilePath( basedir.getAbsolutePath(), pathNormalized );
+        }
+        if ( result == null )
+        {
+            result = path;
+        }
+        result = result.replace( '\\', '/' );
+        if ( result.length() <= 0 || "/".equals( result ) )
+        {
+            result = '.' + result;
+        }
+        return result;
+    }
+
 }
diff --git a/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterUtilTest.java b/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterUtilTest.java
index bfd8e12..30b9063 100644
--- a/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterUtilTest.java
+++ b/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterUtilTest.java
@@ -136,7 +136,7 @@ public class AntBuildWriterUtilTest
     }
 
     /**
-     * Test method for {@link AntBuildWriterUtil#getSingularForm(String)}.
+     * Test method for <code>AntBuildWriterUtil.getSingularForm(String)}</code>.
      *
      * @throws Exception
      */
@@ -150,4 +150,26 @@ public class AntBuildWriterUtilTest
         assertEquals( "", AntBuildWriterUtil.getSingularForm( null ) );
     }
 
+    /**
+     * Test method for <code>AntBuildWriterUtil.toRelative(File, String)</code>.
+     * 
+     * @throws Exception
+     */
+    public static void testToRelative()
+        throws Exception
+    {
+        assertEquals( "relative", AntBuildWriterUtil.toRelative( new File( "/home" ), "relative" ) );
+        assertEquals( "dir", AntBuildWriterUtil.toRelative( new File( "home" ),
+                                                            new File( "home/dir" ).getAbsolutePath() ) );
+        assertEquals( "dir", AntBuildWriterUtil.toRelative( new File( "/home" ),
+                                                            new File( "/home/dir" ).getAbsolutePath() ) );
+        assertEquals( "dir/", AntBuildWriterUtil.toRelative( new File( "/home" ),
+                                                             new File( "/home/dir" ).getAbsolutePath() + "/" ) );
+        assertEquals( "dir/sub", AntBuildWriterUtil.toRelative( new File( "/home" ),
+                                                                new File( "/home/dir/sub" ).getAbsolutePath() ) );
+        assertEquals( ".", AntBuildWriterUtil.toRelative( new File( "/home" ), new File( "/home" ).getAbsolutePath() ) );
+        assertEquals( "./", AntBuildWriterUtil.toRelative( new File( "/home" ), new File( "/home" ).getAbsolutePath()
+            + "/" ) );
+    }
+
 }

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