Author: pgier Date: Tue May 5 16:34:00 2009 New Revision: 771907 URL: http://svn.apache.org/viewvc?rev=771907&view=rev Log: [MANTTASKS-142] Improve default remote repository id.
Added: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/java/org/apache/maven/artifact/ant/PomTestCase.java (with props) Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactWithRepositoryTask.java maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/reference.apt Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactWithRepositoryTask.java URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactWithRepositoryTask.java?rev=771907&r1=771906&r2=771907&view=diff ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactWithRepositoryTask.java (original) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactWithRepositoryTask.java Tue May 5 16:34:00 2009 @@ -19,11 +19,15 @@ * under the License. */ +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.maven.model.Repository; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; /** @@ -123,8 +127,51 @@ return remoteRepositories; } - public void addRemoteRepository( RemoteRepository remoteRepository ) + /** + * This is called automatically by ant when the task is initialized. + * Need to use "addConfigured..." instead of "add..." because the + * repository Id and URL need to be set before the method is called. + * + * @param remoteRepository + */ + public void addConfiguredRemoteRepository( RemoteRepository remoteRepository ) { + // Validate the url and id parameters before adding the repository + if ( remoteRepository.getUrl() == null ) + { + throw new BuildException( "Each remote repository must specify a url." ); + } + if ( remoteRepository.getId() == null || remoteRepository.getId().equals( remoteRepository.getUrl() ) ) + { + log( "Each remote repository should specify a unique id.", Project.MSG_WARN ); + remoteRepository.setId( generateDefaultRepositoryId( remoteRepository ) ); + } remoteRepositories.add( remoteRepository ); } + + public final String MD5_ALGO_NAME = "MD5"; + + /** + * Generates an MD5 digest based on the url of the repository. + * This is safer to use for the id than the url. + * MANTTASKS-142 + * + * @param repository + * @return + */ + public String generateDefaultRepositoryId( RemoteRepository repository ) + { + try + { + MessageDigest md = MessageDigest.getInstance( MD5_ALGO_NAME ); + md.update( repository.getUrl().getBytes() ); + BigInteger digest = new BigInteger( md.digest() ); + return digest.toString( 16 ); + } + catch ( NoSuchAlgorithmException e ) + { + log( "Unable to generate unique repository Id: " + e, Project.MSG_WARN ); + return "default"; + } + } } \ No newline at end of file Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/reference.apt URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/reference.apt?rev=771907&r1=771906&r2=771907&view=diff ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/reference.apt (original) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/reference.apt Tue May 5 16:34:00 2009 @@ -186,10 +186,12 @@ *----------------------+--------------------------------------------------------+--------------+ | <<Attribute>> | <<Description>> | <<Required>> | *----------------------+--------------------------------------------------------+--------------+ -| <<<layout>>> | The layout of the remote repository. The valid options are <<<legacy>>> (Maven 1), or <<<default>>> (Maven 2). Defaults to <<<default>>>. | No | +| <<<id>>> | A unique ID of the repository. | Yes | *----------------------+--------------------------------------------------------+--------------+ | <<<url>>> | The URL of the repository. | Yes | *----------------------+--------------------------------------------------------+--------------+ +| <<<layout>>> | The layout of the remote repository. The valid options are <<<legacy>>> (Maven 1), or <<<default>>> (Maven 2). Defaults to <<<default>>>. | No | +*----------------------+--------------------------------------------------------+--------------+ The <<<remoteRepository>>> can have the following nested elements: <<<releases>>>, <<<snapshots>>>, <<<authentication>>> and <<<proxy>>>. Added: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/java/org/apache/maven/artifact/ant/PomTestCase.java URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/java/org/apache/maven/artifact/ant/PomTestCase.java?rev=771907&view=auto ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/java/org/apache/maven/artifact/ant/PomTestCase.java (added) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/java/org/apache/maven/artifact/ant/PomTestCase.java Tue May 5 16:34:00 2009 @@ -0,0 +1,21 @@ +package org.apache.maven.artifact.ant; + +import junit.framework.TestCase; + +public class PomTestCase + extends TestCase +{ + + public void testDefaultRepositoryId() + { + RemoteRepository repo = new RemoteRepository(); + repo.setUrl( "file:///home/test/stuff" ); + + Pom task = new Pom(); + String defaultId = task.generateDefaultRepositoryId( repo ); + if ( defaultId.equals( repo.getUrl() ) ) + { + this.fail( "MD5 digest not calculated" ); + } + } +} Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/java/org/apache/maven/artifact/ant/PomTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/java/org/apache/maven/artifact/ant/PomTestCase.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision