maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bentm...@apache.org
Subject svn commit: r669269 - in /maven/plugins/trunk/maven-ant-plugin/src: main/java/org/apache/maven/plugin/ant/AntBuildWriter.java test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
Date Wed, 18 Jun 2008 19:43:44 GMT
Author: bentmann
Date: Wed Jun 18 12:43:43 2008
New Revision: 669269

URL: http://svn.apache.org/viewvc?rev=669269&view=rev
Log:
o Handled URL decoding to prepare for the day when the Maven core will use RFC-compliant URLs

Added:
    maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
  (with props)
Modified:
    maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java

Modified: maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java?rev=669269&r1=669268&r2=669269&view=diff
==============================================================================
--- maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
(original)
+++ maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
Wed Jun 18 12:43:43 2008
@@ -23,6 +23,7 @@
 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.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 @@
                     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 @@
         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
     // ----------------------------------------------------------------------

Added: maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java?rev=669269&view=auto
==============================================================================
--- maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
(added)
+++ maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
Wed Jun 18 12:43:43 2008
@@ -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 ) );
+    }
+
+}

Propchange: maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntBuildWriterTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message