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:08 UTC

[maven-ant-plugin] 10/42: o Handled URL decoding to prepare for the day when the Maven core will use RFC-compliant URLs

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 7d9e0ac5549137a3d064b0b1883345a0bf8f8b49
Author: Benjamin Bentmann <be...@apache.org>
AuthorDate: Wed Jun 18 19:43:43 2008 +0000

    o Handled URL decoding to prepare for the day when the Maven core will use RFC-compliant URLs
    
    git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-ant-plugin@669269 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/maven/plugin/ant/AntBuildWriter.java    | 66 +++++++++++++++++++---
 .../maven/plugin/ant/AntBuildWriterTest.java       | 64 +++++++++++++++++++++
 2 files changed, 121 insertions(+), 9 deletions(-)

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 33d5283..8cada3a 100644
--- a/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
+++ b/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -37,6 +38,7 @@ 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;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
@@ -1269,20 +1271,16 @@ public class AntBuildWriter
                     Repository repository = (Repository) j.next();
                     String url = repository.getUrl();
 
-                    if ( url.regionMatches( true, 0, "file:", 0, 5 ) && url.indexOf( basedir ) > 0 )
+                    String localDir = getProjectRepoDirectory( url, basedir );
+                    if ( localDir != null )
                     {
-                        url = url.substring( url.indexOf( basedir ) + basedir.length() );
-                        if ( url.startsWith( "/" ) )
+                        if ( localDir.length() > 0 && !localDir.endsWith( "/" ) )
                         {
-                            url = url.substring( 1 );
-                        }
-                        if ( !url.endsWith( "/" ) && url.length() > 0 )
-                        {
-                            url += '/';
+                            localDir += '/';
                         }
 
                         writer.startElement( "copy" );
-                        writer.addAttribute( "file", url + path );
+                        writer.addAttribute( "file", localDir + path );
                         AntBuildWriterUtil.addWrapAttribute( writer, "copy", "tofile", "${maven.repo.local}/" + path, 3 );
                         AntBuildWriterUtil.addWrapAttribute( writer, "copy", "failonerror", "false", 3 );
                         writer.endElement(); // copy
@@ -1305,6 +1303,56 @@ public class AntBuildWriter
         XmlWriterUtil.writeLineBreak( writer );
     }
 
+    /**
+     * Gets the relative path to a repository that is rooted in the project. The returned path (if any) will always use
+     * the forward slash ('/') as the directory separator. For example, the path "target/it-repo" will be returned for a
+     * repository constructed from the URL "file://${basedir}/target/it-repo".
+     * 
+     * @param repoUrl The URL to the repository, must not be <code>null</code>.
+     * @param projectDir The absolute path to the base directory of the project, must not be <code>null</code>
+     * @return The path to the repository (relative to the project base directory) or <code>null</code> if the
+     *         repository is not rooted in the project.
+     */
+    static String getProjectRepoDirectory( String repoUrl, String projectDir )
+    {
+        try
+        {
+            /*
+             * NOTE: The usual way of constructing repo URLs rooted in the project is "file://${basedir}" or
+             * "file:/${basedir}". None of these forms delivers a valid URL on both Unix and Windows (even ignoring URL
+             * encoding), one platform will end up with the first directory of the path being interpreted as the host
+             * name...
+             */
+            if ( repoUrl.regionMatches( true, 0, "file://", 0, 7 ) )
+            {
+                String temp = repoUrl.substring( 7 );
+                if ( !temp.startsWith( "/" ) && !temp.regionMatches( true, 0, "localhost/", 0, 10 ) )
+                {
+                    repoUrl = "file:///" + temp;
+                }
+            }
+            String path = FileUtils.toFile( new URL( repoUrl ) ).getPath();
+            if ( path.startsWith( projectDir ) )
+            {
+                path = path.substring( projectDir.length() ).replace( '\\', '/' );
+                if ( path.startsWith( "/" ) )
+                {
+                    path = path.substring( 1 );
+                }
+                if ( path.endsWith( "/" ) )
+                {
+                    path = path.substring( 0, path.length() - 1 );
+                }
+                return path;
+            }
+        }
+        catch ( Exception e )
+        {
+            // not a "file:" URL or simply malformed
+        }
+        return null;
+    }
+
     // ----------------------------------------------------------------------
     // Convenience methods
     // ----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java b/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
new file mode 100644
index 0000000..7ad62f7
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
@@ -0,0 +1,64 @@
+package org.apache.maven.plugin.ant;
+
+/*
+ * 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 junit.framework.TestCase;
+
+/**
+ * Tests <code>AntBuildWriter</code>.
+ * 
+ * @author Benjamin Bentmann
+ * @version $Id$
+ */
+public class AntBuildWriterTest
+    extends TestCase
+{
+
+    public void testGetProjectRepoDirectory()
+    {
+        String basedir = new File( System.getProperty( "java.io.tmpdir" ) ).getPath();
+
+        // non-project rooted repo URLs
+        assertEquals( null, AntBuildWriter.getProjectRepoDirectory( "http://maven.apache.org/", basedir ) );
+        assertEquals( null, AntBuildWriter.getProjectRepoDirectory( "file:///just-some-test-directory", basedir ) );
+
+        // RFC-compliant URLs
+        assertEquals( "", AntBuildWriter.getProjectRepoDirectory( new File( basedir ).toURI().toString(), basedir ) );
+        assertEquals( "dir", AntBuildWriter.getProjectRepoDirectory( new File( basedir, "dir" ).toURI().toString(),
+                                                                     basedir ) );
+        assertEquals( "dir/subdir",
+                      AntBuildWriter.getProjectRepoDirectory( new File( basedir, "dir/subdir" ).toURI().toString(),
+                                                              basedir ) );
+
+        // not so strict URLs
+        assertEquals( "", AntBuildWriter.getProjectRepoDirectory( "file://" + basedir, basedir ) );
+        assertEquals( "dir", AntBuildWriter.getProjectRepoDirectory( "file://" + basedir + "/dir", basedir ) );
+        assertEquals( "dir/subdir", AntBuildWriter.getProjectRepoDirectory( "file://" + basedir + "/dir/subdir",
+                                                                            basedir ) );
+
+        // URLs with encoded characters
+        assertEquals( "some dir",
+                      AntBuildWriter.getProjectRepoDirectory( new File( basedir, "some dir" ).toURI().toString(),
+                                                              basedir ) );
+    }
+
+}

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