You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ca...@apache.org on 2011/10/26 21:00:50 UTC
svn commit: r1189385 - in /maven/plugins/trunk/maven-dependency-plugin/src:
main/java/org/apache/maven/plugin/dependency/GetMojo.java
test/java/org/apache/maven/plugin/dependency/TestGetMojo.java
Author: carlos
Date: Wed Oct 26 19:00:50 2011
New Revision: 1189385
URL: http://svn.apache.org/viewvc?rev=1189385&view=rev
Log:
[MDEP-333] Add a test for the remoteRepositories parameter and fix issue found with parsing
Modified:
maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/GetMojo.java
maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestGetMojo.java
Modified: maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/GetMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/GetMojo.java?rev=1189385&r1=1189384&r2=1189385&view=diff
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/GetMojo.java (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/main/java/org/apache/maven/plugin/dependency/GetMojo.java Wed Oct 26 19:00:50 2011
@@ -26,6 +26,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.Artifact;
@@ -52,6 +54,7 @@ import org.codehaus.plexus.util.StringUt
public class GetMojo
extends AbstractMojo
{
+ private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
/**
* @component
@@ -231,28 +234,7 @@ public class GetMojo
List<String> repos = Arrays.asList( StringUtils.split( remoteRepositories, "," ) );
for ( String repo : repos )
{
- String[] split = StringUtils.split( repo, "::" );
- if ( split.length > 1 && split.length != 3 )
- {
- throw new MojoExecutionException(
- "remoteRepositories parameter must be a list of URLs or Strings like id::layout::url" );
- }
-
- String id = repositoryId;
- ArtifactRepositoryLayout layout = getLayout( "default" );
- String url = repo;
- if ( split.length > 1 )
- {
- id = split[0];
- if ( !StringUtils.isEmpty( split[1] ) )
- {
- layout = getLayout( split[1] );
- }
- url = split[2];
- }
- ArtifactRepository remoteRepo =
- artifactRepositoryFactory.createArtifactRepository( id, url, layout, always, always );
- repoList.add( remoteRepo );
+ repoList.add( parseRepository( repo, always ) );
}
}
@@ -302,14 +284,42 @@ public class GetMojo
}
}
+ ArtifactRepository parseRepository( String repo, ArtifactRepositoryPolicy policy )
+ throws MojoFailureException
+ {
+ // if it's a simple url
+ String id = repositoryId;
+ ArtifactRepositoryLayout layout = getLayout( "default" );
+ String url = repo;
+
+ // if it's an extended repo URL of the form id::layout::url
+ if ( repo.indexOf( "::" ) >= 0 )
+ {
+ Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( repo );
+ if ( !matcher.matches() )
+ {
+ throw new MojoFailureException( repo, "Invalid syntax for repository: " + repo,
+ "Invalid syntax for repository. Use \"id::layout::url\" or \"URL\"." );
+ }
+
+ id = matcher.group( 1 ).trim();
+ if ( !StringUtils.isEmpty( matcher.group( 2 ) ) )
+ {
+ layout = getLayout( matcher.group( 2 ).trim() );
+ }
+ url = matcher.group( 3 ).trim();
+ }
+ return artifactRepositoryFactory.createArtifactRepository( id, url, layout, policy, policy );
+ }
+
private ArtifactRepositoryLayout getLayout( String id )
- throws MojoExecutionException
+ throws MojoFailureException
{
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( id );
if ( layout == null )
{
- throw new MojoExecutionException( "Invalid repository layout: " + id );
+ throw new MojoFailureException( id, "Invalid repository layout", "Invalid repository layout: " + id );
}
return layout;
Modified: maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestGetMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestGetMojo.java?rev=1189385&r1=1189384&r2=1189385&view=diff
==============================================================================
--- maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestGetMojo.java (original)
+++ maven/plugins/trunk/maven-dependency-plugin/src/test/java/org/apache/maven/plugin/dependency/TestGetMojo.java Wed Oct 26 19:00:50 2011
@@ -21,38 +21,43 @@ package org.apache.maven.plugin.dependen
import java.io.File;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout;
+import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
public class TestGetMojo
extends AbstractDependencyMojoTestCase
{
+ GetMojo mojo;
protected void setUp()
throws Exception
{
// required for mojo lookups to work
super.setUp( "markers", false );
+
+ File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
+ assert testPom.exists();
+ mojo = (GetMojo) lookupMojo( "get", testPom );
+
+ assertNotNull( mojo );
+ setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( testDir.getAbsolutePath() ) );
}
/**
- * tests the proper discovery and configuration of the mojo
+ * Test transitive parameter
*
* @throws Exception
*/
- public void testgetTestEnvironment()
+ public void testTransitive()
throws Exception
{
- File testPom = new File( getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml" );
- assert testPom.exists();
- GetMojo mojo = (GetMojo) lookupMojo( "get", testPom );
-
- assertNotNull( mojo );
-
- setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( testDir.getAbsolutePath() ) );
-
// Set properties, transitive = default value = true
setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
- setVariableValueToObject( mojo, "repositoryUrl", "http://repo1.maven.org/maven2" );
+ setVariableValueToObject( mojo, "repositoryUrl", "http://repo1.maven.apache.org/maven2" );
setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
setVariableValueToObject( mojo, "artifactId", "maven-model" );
setVariableValueToObject( mojo, "version", "2.0.9" );
@@ -63,4 +68,72 @@ public class TestGetMojo
setVariableValueToObject( mojo, "transitive", Boolean.FALSE );
mojo.execute();
}
+
+ /**
+ * Test remote repositories parameter
+ *
+ * @throws Exception
+ */
+ public void testRemoteRepositories()
+ throws Exception
+ {
+ setVariableValueToObject( mojo, "remoteRepositories", "central::default::http://repo1.maven.apache.org/maven2,"
+ + "central::::http://repo1.maven.apache.org/maven2," + "http://repo1.maven.apache.org/maven2" );
+ setVariableValueToObject( mojo, "groupId", "org.apache.maven" );
+ setVariableValueToObject( mojo, "artifactId", "maven-model" );
+ setVariableValueToObject( mojo, "version", "2.0.9" );
+
+ mojo.execute();
+ }
+
+ /**
+ * Test parsing of the remote repositories parameter
+ *
+ * @throws Exception
+ */
+ public void testParseRepository()
+ throws Exception
+ {
+ ArtifactRepository repo;
+ ArtifactRepositoryPolicy policy = null;
+ repo = mojo.parseRepository( "central::default::http://repo1.maven.apache.org/maven2", policy );
+ assertEquals( "central", repo.getId() );
+ assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
+ assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
+
+ repo = mojo.parseRepository( "central::legacy::http://repo1.maven.apache.org/maven2", policy );
+ assertEquals( "central", repo.getId() );
+ assertEquals( LegacyRepositoryLayout.class, repo.getLayout().getClass() );
+ assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
+
+ repo = mojo.parseRepository( "central::::http://repo1.maven.apache.org/maven2", policy );
+ assertEquals( "central", repo.getId() );
+ assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
+ assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
+
+ repo = mojo.parseRepository( "http://repo1.maven.apache.org/maven2", policy );
+ assertEquals( "temp", repo.getId() );
+ assertEquals( DefaultRepositoryLayout.class, repo.getLayout().getClass() );
+ assertEquals( "http://repo1.maven.apache.org/maven2", repo.getUrl() );
+
+ try
+ {
+ repo = mojo.parseRepository( "::::http://repo1.maven.apache.org/maven2", policy );
+ fail( "Exception expected" );
+ }
+ catch ( MojoFailureException e )
+ {
+ // expected
+ }
+
+ try
+ {
+ repo = mojo.parseRepository( "central::http://repo1.maven.apache.org/maven2", policy );
+ fail( "Exception expected" );
+ }
+ catch ( MojoFailureException e )
+ {
+ // expected
+ }
+ }
}