Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDEP-650] - dependency:unpack doesn't seem to handle version ranges #12

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ under the License.
</parent>

<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2-SNAPSHOT</version>
<version>3.1.1.alehane-SNAPSHOT</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope

<packaging>maven-plugin</packaging>

<name>Apache Maven Dependency Plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -106,6 +114,9 @@ public abstract class AbstractFromConfigurationMojo
@Component
private ArtifactResolver artifactResolver;

@Component
private DependencyResolver dependencyResolver;

@Component
private RepositoryManager repositoryManager;

Expand All @@ -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()
Expand Down Expand Up @@ -156,6 +167,8 @@ protected List<ArtifactItem> getProcessedArtifactItems( ProcessArtifactItemsRequ
{
this.getLog().info( "Configured Artifact: " + artifactItem.toString() );

resolveArtifactRanges( artifactItem );

if ( artifactItem.getOutputDirectory() == null )
{
artifactItem.setOutputDirectory( this.outputDirectory );
Expand Down Expand Up @@ -189,6 +202,105 @@ protected List<ArtifactItem> 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the artifact item to update, if required

per oracle javadoc conventions

*
* @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<ArtifactResult> 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
{
Expand Down
Loading