Return-Path: Delivered-To: apmail-maven-commits-archive@www.apache.org Received: (qmail 53353 invoked from network); 18 Jun 2008 19:44:06 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Jun 2008 19:44:06 -0000 Received: (qmail 28925 invoked by uid 500); 18 Jun 2008 19:44:07 -0000 Delivered-To: apmail-maven-commits-archive@maven.apache.org Received: (qmail 28870 invoked by uid 500); 18 Jun 2008 19:44:07 -0000 Mailing-List: contact commits-help@maven.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@maven.apache.org Delivered-To: mailing list commits@maven.apache.org Received: (qmail 28861 invoked by uid 99); 18 Jun 2008 19:44:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jun 2008 12:44:07 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jun 2008 19:43:26 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id DA37C23889BA; Wed, 18 Jun 2008 12:43:44 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@maven.apache.org From: bentmann@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080618194344.DA37C23889BA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 null. + * @param projectDir The absolute path to the base directory of the project, must not be null + * @return The path to the repository (relative to the project base directory) or null 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 AntBuildWriter. + * + * @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