-
Notifications
You must be signed in to change notification settings - Fork 173
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
alehane
wants to merge
10
commits into
apache:master
Choose a base branch
from
alehane:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1dd1c2d
MDEP-650 - Add ability to resolve version ranges when using the "unpack"
alehane 155cd72
MDEP-650 - Add ability to resolve version ranges when using the "unpack"
alehane 27e4685
MDEP-650 - Add ability to resolve version ranges when using the "unpack"
alehane c84ff87
MDEP-650 - Add ability to resolve version ranges when using the "unpack"
alehane 295f50f
MDEP-650 - Add ability to resolve version ranges when using the "unpack"
alehane e321ea7
MDEP-650 - Add ability to resolve version ranges when using the "unpa…
alehane 93c2b4c
MDEP-650 - Add ability to resolve version ranges when using the "unpa…
alehane 20a8628
MDEP-650 - dependency:unpack doesn't seem to handle version ranges - …
alehane a35c269
[MDEP-650] - dependency:unpack doesn't seem to handle version ranges …
alehane bab35d7
Change snapshot version to allow for internal use.
alehane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<ArtifactItem> 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<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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope