diff --git a/pom.xml b/pom.xml
index ca53726c1..53150191e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -30,7 +30,7 @@ under the License.
maven-dependency-plugin
- 3.1.2-SNAPSHOT
+ 3.1.1.alehane-SNAPSHOT
maven-plugin
Apache Maven Dependency Plugin
diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
index 377923c12..f839b17d1 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
@@ -26,6 +26,10 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -40,6 +44,10 @@
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
+import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.codehaus.plexus.util.StringUtils;
@@ -106,6 +114,9 @@ public abstract class AbstractFromConfigurationMojo
@Component
private ArtifactResolver artifactResolver;
+ @Component
+ private DependencyResolver dependencyResolver;
+
@Component
private RepositoryManager repositoryManager;
@@ -116,7 +127,7 @@ public abstract class AbstractFromConfigurationMojo
/**
* artifactItems is filled by either field injection or by setArtifact().
- *
+ *
* @throws MojoFailureException in case of an error.
*/
protected void verifyRequirements()
@@ -156,6 +167,8 @@ protected List getProcessedArtifactItems( ProcessArtifactItemsRequ
{
this.getLog().info( "Configured Artifact: " + artifactItem.toString() );
+ resolveArtifactRanges( artifactItem );
+
if ( artifactItem.getOutputDirectory() == null )
{
artifactItem.setOutputDirectory( this.outputDirectory );
@@ -189,6 +202,105 @@ protected List getProcessedArtifactItems( ProcessArtifactItemsRequ
return artifactItems;
}
+ /**
+ * If the artifact item has a version range, rather than a version, this
+ * method attempts to resolve this range by inspecting the list of resolved dependencies
+ * in the project for a match, before using the maven dependency resolver to resolve
+ * the range.
+ *
+ * If the dependency can be found and the version fits the artifact item's range
+ * then the artifact item is updated with the found version.
+ *
+ * If the dependency is not found or the range does not match, then the version
+ * is not changed.
+ *
+ * @param artifactItem The artifact item to update, if required.
+ *
+ * @throws MojoExecutionException if
+ */
+ private void resolveArtifactRanges( final ArtifactItem artifactItem )
+ {
+ VersionRange range;
+ try
+ {
+ range = VersionRange.createFromVersionSpec( artifactItem.getVersion() );
+ }
+ catch ( InvalidVersionSpecificationException ivse )
+ {
+ this.getLog().warn( "Found invalid version range on artifact: " + artifactItem );
+ range = null;
+ }
+
+ if ( range != null && range.hasRestrictions() )
+ {
+ // First, try and find the artifact in the projects list of already, resolved
+ // dependencies:
+ ComparableVersion foundVersion = null;
+
+ if ( getProject().getDependencyArtifacts() != null )
+ {
+ for ( Artifact a : getProject().getDependencyArtifacts() )
+ {
+ if ( artifactItem.getArtifactId().equals( a.getArtifactId() )
+ && artifactItem.getGroupId().equals( a.getGroupId() )
+ && range.containsVersion( new DefaultArtifactVersion( a.getVersion() ) ) )
+ {
+
+ ComparableVersion v = new ComparableVersion( a.getVersion() );
+ if ( foundVersion == null || v.compareTo( foundVersion ) > 0 )
+ {
+ foundVersion = v;
+ }
+ }
+ }
+ }
+
+ if ( foundVersion == null )
+ {
+ // If we've not found the artifact in the resolved list of project dependencies,
+ // then attempt to use the dependency resolver to resolve the version range:
+
+ ProjectBuildingRequest request = newResolveArtifactProjectBuildingRequest();
+
+ DefaultDependableCoordinate searchDep = new DefaultDependableCoordinate();
+ searchDep.setGroupId( artifactItem.getGroupId() );
+ searchDep.setArtifactId( artifactItem.getArtifactId() );
+ searchDep.setVersion( artifactItem.getVersion() );
+ searchDep.setType( artifactItem.getType() );
+ searchDep.setClassifier( artifactItem.getClassifier() );
+
+ Iterable result;
+ try
+ {
+ result = dependencyResolver.resolveDependencies( request, searchDep, null );
+ }
+ catch ( DependencyResolverException are )
+ {
+ result = null;
+ this.getLog().warn( are );
+ }
+
+ if ( result != null )
+ {
+ for ( ArtifactResult artifact : result )
+ {
+ ComparableVersion v = new ComparableVersion( artifact.getArtifact().getVersion() );
+ if ( foundVersion == null || v.compareTo( foundVersion ) > 0 )
+ {
+ foundVersion = v;
+ }
+ }
+ }
+ }
+
+ if ( foundVersion != null )
+ {
+ this.getLog().info( "Resolved version from: " + range.toString() + ", to: " + foundVersion );
+ artifactItem.setVersion( foundVersion.toString() );
+ }
+ }
+ }
+
private boolean checkIfProcessingNeeded( ArtifactItem item )
throws MojoExecutionException, ArtifactFilterException
{
diff --git a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java
index 5a2748499..cca7f9411 100644
--- a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java
+++ b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackMojo.java
@@ -1,6 +1,6 @@
package org.apache.maven.plugins.dependency.fromConfiguration;
-/*
+/*
* 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
@@ -16,7 +16,7 @@
* "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.
+ * under the License.
*/
import java.io.File;
@@ -24,26 +24,39 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
+import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyResolverStub;
import org.apache.maven.plugins.dependency.utils.markers.UnpackFileMarkerHandler;
import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
+import org.codehaus.plexus.util.ReflectionUtils;
import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
import org.sonatype.aether.util.DefaultRepositorySystemSession;
+
public class TestUnpackMojo
extends AbstractDependencyMojoTestCase
{
UnpackMojo mojo;
+ DefaultArtifactHandlerManager artifactHandlerManager;
public TestUnpackMojo()
{
@@ -80,8 +93,25 @@ protected void setUp()
DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) session.getRepositorySession();
repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( stubFactory.getWorkingDir() ) );
+
+ artifactHandlerManager = new DefaultArtifactHandlerManager();
+
+ Map handlerMap = new HashMap<>();
+ handlerMap.put("", new DefaultArtifactHandler(""));
+ handlerMap.put("jar", new DefaultArtifactHandler("jar"));
+ artifactHandlerManager.addHandlers(handlerMap);
+ }
+
+ @Override
+ protected void tearDown()
+ {
+ super.tearDown();
+
+ artifactHandlerManager = null;
+ mojo = null;
}
+
public ArtifactItem getSingleArtifactItem( boolean removeVersion )
throws MojoExecutionException
{
@@ -92,12 +122,7 @@ public ArtifactItem getSingleArtifactItem( boolean removeVersion )
public void testGetArtifactItems()
throws Exception
{
-
- ArtifactItem item = new ArtifactItem();
-
- item.setArtifactId( "artifact" );
- item.setGroupId( "groupId" );
- item.setVersion( "1.0" );
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", "1.0", null, null );
ArrayList list = new ArrayList( 1 );
list.add( createArtifact( item ) );
@@ -212,12 +237,7 @@ public boolean exists()
public void testMissingVersionNotFound()
throws Exception
{
- ArtifactItem item = new ArtifactItem();
-
- item.setArtifactId( "artifactId" );
- item.setClassifier( "" );
- item.setGroupId( "groupId" );
- item.setType( "type" );
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", null, "", "type");
List list = new ArrayList();
list.add( item );
@@ -260,12 +280,7 @@ public List getDependencyList( ArtifactItem item )
public void testMissingVersionFromDependencies()
throws Exception
{
- ArtifactItem item = new ArtifactItem();
-
- item.setArtifactId( "artifactId" );
- item.setClassifier( "" );
- item.setGroupId( "groupId" );
- item.setType( "jar" );
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", null, "", "jar");
List list = new ArrayList();
list.add( item );
@@ -282,12 +297,8 @@ public void testMissingVersionFromDependencies()
public void testMissingVersionFromDependenciesWithClassifier()
throws Exception
{
- ArtifactItem item = new ArtifactItem();
- item.setArtifactId( "artifactId" );
- item.setClassifier( "classifier" );
- item.setGroupId( "groupId" );
- item.setType( "war" );
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", null, "classifier", "war");
List list = new ArrayList();
list.add( item );
@@ -327,22 +338,12 @@ public List getDependencyMgtList( ArtifactItem item )
public void testMissingVersionFromDependencyMgt()
throws Exception
{
- ArtifactItem item = new ArtifactItem();
-
- item.setArtifactId( "artifactId" );
- item.setClassifier( "" );
- item.setGroupId( "groupId" );
- item.setType( "jar" );
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", null, "", "jar");
MavenProject project = mojo.getProject();
project.setDependencies( createArtifacts( getDependencyList( item ) ) );
- item = new ArtifactItem();
-
- item.setArtifactId( "artifactId-2" );
- item.setClassifier( "" );
- item.setGroupId( "groupId" );
- item.setType( "jar" );
+ item = createArtifactItem( "groupId", "artifactId-2", null, "", "jar");
List list = new ArrayList();
list.add( item );
@@ -359,22 +360,12 @@ public void testMissingVersionFromDependencyMgt()
public void testMissingVersionFromDependencyMgtWithClassifier()
throws Exception
{
- ArtifactItem item = new ArtifactItem();
-
- item.setArtifactId( "artifactId" );
- item.setClassifier( "classifier" );
- item.setGroupId( "groupId" );
- item.setType( "jar" );
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", null, "classifier", "jar");
MavenProject project = mojo.getProject();
project.setDependencies( createArtifacts( getDependencyList( item ) ) );
- item = new ArtifactItem();
-
- item.setArtifactId( "artifactId-2" );
- item.setClassifier( "classifier" );
- item.setGroupId( "groupId" );
- item.setType( "jar" );
+ item = createArtifactItem( "groupId", "artifactId-2", null, "classifier", "jar");
stubFactory.createArtifact( "groupId", "artifactId-2", VersionRange.createFromVersion( "3.0-SNAPSHOT" ), null,
"jar", "classifier", false );
@@ -409,13 +400,7 @@ public void testArtifactResolutionException()
public void dotestArtifactExceptions( boolean are, boolean anfe )
throws Exception
{
- ArtifactItem item = new ArtifactItem();
-
- item.setArtifactId( "artifactId" );
- item.setClassifier( "" );
- item.setGroupId( "groupId" );
- item.setType( "type" );
- item.setVersion( "1.0" );
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", "1.0", "", "type" );
List list = new ArrayList();
list.add( item );
@@ -573,6 +558,98 @@ public void testUnpackOverWriteIfNewer()
+ ": should be different", marker.lastModified() != unpackedFile.lastModified() );
}
+ public void testVersionRangeFromResolvedProjectDependencies()
+ throws Exception
+ {
+ stubFactory.setCreateFiles( true );
+
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", "[0,)", "", "jar");
+
+ List list = new ArrayList();
+ list.add( item );
+ mojo.setArtifactItems( list );
+
+ stubFactory.createArtifact( "groupId", "artifactId", VersionRange.createFromVersion( "2.0-SNAPSHOT" ), null,
+ "jar", "", false );
+ stubFactory.createArtifact( "groupId", "artifactId", VersionRange.createFromVersion( "2.1" ), null, "jar",
+ "", false );
+
+ MavenProject project = mojo.getProject();
+ project.setDependencies( createArtifacts( getDependencyList( item ) ) );
+ project.setDependencyArtifacts( createArtifactSet( getDependencyList( item ) ) );
+
+ mojo.execute();
+ assertMarkerFile( true, item );
+ assertEquals( "2.1", item.getVersion() );
+ }
+
+ public void testVersionRangeFromResolvedProjectDependenciesWithMultipleDependencies()
+ throws Exception
+ {
+ stubFactory.setCreateFiles( true );
+
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", "[0,)", "", "jar");
+
+ List list = new ArrayList();
+ list.add( item );
+ mojo.setArtifactItems( list );
+
+ stubFactory.createArtifact( "groupId", "artifactId", VersionRange.createFromVersion( "2.0-SNAPSHOT" ), null,
+ "jar", "", false );
+ stubFactory.createArtifact( "groupId", "artifactId", VersionRange.createFromVersion( "2.1" ), null, "jar",
+ "", false );
+
+ MavenProject project = mojo.getProject();
+ project.setDependencies( createArtifacts( getDependencyList( item ) ) );
+
+ Set dependencySet = createArtifactSet( getDependencyList( item ) );
+ dependencySet.addAll( createArtifactSet( getDependencyList( createArtifactItem( "groupId", "differentArtifactId", "1.0", "", "jar") ) ) );
+ dependencySet.addAll( createArtifactSet( getDependencyList( createArtifactItem( "differentGroupId", "artifactId", "1.0", "", "jar") ) ) );
+
+ project.setDependencyArtifacts( dependencySet );
+
+ mojo.execute();
+ assertMarkerFile( true, item );
+ assertEquals( "2.1", item.getVersion() );
+ }
+
+ public void testVersionRangeNoResolvedProjectDependencies()
+ throws Exception
+ {
+ stubFactory.setCreateFiles( true );
+
+ DependencyResolverStub stubResolver = new DependencyResolverStub();
+ ReflectionUtils.setVariableValueInObject(mojo, "dependencyResolver", stubResolver);
+
+ DefaultDependableCoordinate coord = new DefaultDependableCoordinate();
+ coord.setArtifactId( "artifactId" );
+ coord.setGroupId( "groupId" );
+ coord.setVersion( "[0,)" );
+ coord.setType( "jar" );
+
+ DefaultArtifact depArtifact = new DefaultArtifact(
+ "groupId", "artifactId", "2.1", null, "jar", "", artifactHandlerManager.getArtifactHandler( "jar" )
+ );
+
+ stubResolver.addDependableCoordinateLookup(coord, depArtifact);
+
+ ArtifactItem item = createArtifactItem( "groupId", "artifactId", "[0,)", "", "jar" );
+
+ List list = new ArrayList();
+ list.add( item );
+ mojo.setArtifactItems( list );
+
+ stubFactory.createArtifact( "groupId", "artifactId", VersionRange.createFromVersion( "2.0-SNAPSHOT" ), null,
+ "jar", "", false );
+ stubFactory.createArtifact( "groupId", "artifactId", VersionRange.createFromVersion( "2.1" ), null, "jar",
+ "", false );
+
+ mojo.execute();
+ assertMarkerFile( true, item );
+ assertEquals( "2.1", item.getVersion() );
+ }
+
+
private void displayFile( String description, File file )
{
System.out.println( description + ' ' + DateFormatUtils.ISO_DATETIME_FORMAT.format( file.lastModified() ) + ' '
@@ -614,6 +691,28 @@ public File getUnpackedFile( ArtifactItem item )
}
+ private ArtifactItem createArtifactItem(
+ final String groupId,
+ final String artifactId,
+ final String version,
+ final String classifier,
+ final String type )
+ {
+
+ ArtifactItem item = new ArtifactItem();
+
+ item.setArtifactId( artifactId );
+ item.setClassifier( classifier );
+ item.setGroupId( groupId );
+
+ if ( type != null ) {
+ item.setType( type );
+ }
+ item.setVersion( version );
+
+ return item;
+ }
+
// respects the createUnpackableFile flag of the ArtifactStubFactory
private List createArtifacts( List items )
throws IOException
@@ -628,6 +727,30 @@ private List createArtifacts( List items )
return items;
}
+ // respects the createUnpackableFile flag of the ArtifactStubFactory
+ private Set createArtifactSet( List items )
+ throws IOException
+ {
+
+ Set set = new HashSet<>();
+
+ for ( Dependency item : items )
+ {
+ set.add(
+ new DefaultArtifact(
+ item.getGroupId(),
+ item.getArtifactId(),
+ item.getVersion(),
+ item.getScope(),
+ item.getType(),
+ item.getClassifier(),
+ artifactHandlerManager.getArtifactHandler(item.getType())
+ )
+ );
+ }
+ return set;
+ }
+
private Artifact createArtifact( Artifact art )
throws IOException
{
diff --git a/src/test/java/org/apache/maven/plugins/dependency/testUtils/stubs/DependencyResolverStub.java b/src/test/java/org/apache/maven/plugins/dependency/testUtils/stubs/DependencyResolverStub.java
new file mode 100644
index 000000000..e2c76ece7
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugins/dependency/testUtils/stubs/DependencyResolverStub.java
@@ -0,0 +1,98 @@
+package org.apache.maven.plugins.dependency.testUtils.stubs;
+
+/*
+ * 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.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.model.Dependency;
+import org.apache.maven.model.Model;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
+import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
+import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
+
+/**
+ * A very simple stub to use for whiteboxing the a Maven DependencyResolver
+ * class.
+ */
+public class DependencyResolverStub implements DependencyResolver {
+
+ private Map coordCache = new HashMap<>();
+
+ public void addDependableCoordinateLookup(
+ final DependableCoordinate coord,
+ final Artifact artifact )
+ {
+ coordCache.put( coord.toString(), artifact );
+ }
+
+ @Override
+ public Iterable resolveDependencies(
+ final ProjectBuildingRequest buildingRequest,
+ final DependableCoordinate coordinate,
+ final TransformableFilter filter ) throws DependencyResolverException
+ {
+
+
+ if ( coordCache.get( coordinate.toString() ) != null ) {
+ ArtifactResult result = new ArtifactResult()
+ {
+
+ @Override
+ public Artifact getArtifact()
+ {
+ // TODO Auto-generated method stub
+ return coordCache.get( coordinate.toString() );
+ }
+ };
+
+ return Arrays.asList( result );
+ } else {
+ throw new DependencyResolverException( "Cannot resolve coordinates: " + coordinate, new IOException() );
+ }
+ }
+
+ @Override
+ public Iterable resolveDependencies(
+ ProjectBuildingRequest buildingRequest,
+ Model model,
+ TransformableFilter filter ) throws DependencyResolverException {
+
+ throw new UnsupportedOperationException( "Method not implemented" );
+ }
+
+ @Override
+ public Iterable resolveDependencies(
+ ProjectBuildingRequest buildingRequest,
+ Collection dependencies,
+ Collection managedDependencies,
+ TransformableFilter filter ) throws DependencyResolverException {
+
+ throw new UnsupportedOperationException( "Method not implemented" );
+ }
+
+}