-
Notifications
You must be signed in to change notification settings - Fork 200
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
Resolve dependency constraints. #351
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ import org.gradle.api.artifacts.ComponentMetadata | |
import org.gradle.api.artifacts.ComponentSelection | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.artifacts.Dependency | ||
import org.gradle.api.artifacts.DependencyConstraint | ||
import org.gradle.api.artifacts.ExternalDependency | ||
import org.gradle.api.artifacts.LenientConfiguration | ||
import org.gradle.api.artifacts.ModuleVersionIdentifier | ||
|
@@ -117,6 +118,15 @@ class Resolver { | |
createQueryDependency(dependency, revision) | ||
} | ||
|
||
// Common use case for dependency constraints is a java-platform BOM project. | ||
try { | ||
configuration.dependencyConstraints.each { dependency -> | ||
latest.add(createQueryDependency(dependency, revision)) | ||
} | ||
} catch (MissingPropertyException e) { | ||
// Skip if constraints not supported | ||
} | ||
|
||
Configuration copy = configuration.copyRecursive().setTransitive(false) | ||
// https://github.com/ben-manes/gradle-versions-plugin/issues/127 | ||
if (copy.metaClass.respondsTo(copy, "setCanBeResolved", Boolean)) { | ||
|
@@ -149,6 +159,19 @@ class Resolver { | |
} | ||
} | ||
|
||
/** Returns a variant of the provided dependency used for querying the latest version. */ | ||
@TypeChecked(SKIP) | ||
private Dependency createQueryDependency(DependencyConstraint dependency, String revision) { | ||
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. Its probably not simpler to combine this with the other |
||
String versionQuery = useSelectionRules ? '+' : "latest.${revision}" | ||
|
||
// If no version was specified then use 'none' to pass it through. | ||
String version = dependency.version == null ? 'none' : versionQuery | ||
|
||
return project.dependencies.create("${dependency.group}:${dependency.name}:${version}") { | ||
transitive = false | ||
} | ||
} | ||
|
||
/** Adds a revision filter by rejecting candidates using a component selection rule. */ | ||
@TypeChecked(SKIP) | ||
private void addRevisionFilter(Configuration configuration, String revision) { | ||
|
@@ -184,12 +207,11 @@ class Resolver { | |
|
||
/** Returns the coordinates for the current (declared) dependency versions. */ | ||
private Map<Coordinate.Key, Coordinate> getCurrentCoordinates(Configuration configuration) { | ||
Map<Coordinate.Key, Coordinate> declared = configuration.dependencies.findAll { dependency -> | ||
dependency instanceof ExternalDependency | ||
}.collectEntries { | ||
Coordinate coordinate = Coordinate.from(it) | ||
return [coordinate.key, coordinate] | ||
} | ||
Map<Coordinate.Key, Coordinate> declared = | ||
getResolvableDependencies(configuration).collectEntries { | ||
return [it.key, it] | ||
} | ||
|
||
if (declared.isEmpty()) { | ||
return Collections.emptyMap() | ||
} | ||
|
@@ -217,6 +239,15 @@ class Resolver { | |
coordinates.put(coordinate.key, declared.get(coordinate.key)) | ||
} | ||
|
||
try { | ||
for (DependencyConstraint constraint : copy.dependencyConstraints) { | ||
Coordinate coordinate = Coordinate.from(constraint) | ||
coordinates.put(coordinate.key, declared.get(coordinate.key)) | ||
} | ||
} catch (MissingPropertyException e) { | ||
// Skip if constraints not supported | ||
} | ||
|
||
// Ignore undeclared (hidden) dependencies that appear when resolving a configuration | ||
coordinates.keySet().retainAll(declared.keySet()) | ||
|
||
|
@@ -333,6 +364,24 @@ class Resolver { | |
return null | ||
} | ||
|
||
private static List<Coordinate> getResolvableDependencies(Configuration configuration) { | ||
List<Coordinate> coordinates = configuration.dependencies.findAll { dependency -> | ||
dependency instanceof ExternalDependency | ||
}.collect { dependency -> | ||
Coordinate.from(dependency) | ||
} | ||
|
||
try { | ||
configuration.dependencyConstraints.each { | ||
coordinates.add(Coordinate.from(it)) | ||
} | ||
} catch (MissingPropertyException e) { | ||
// Skip | ||
} | ||
|
||
return coordinates | ||
} | ||
|
||
private static final class ProjectUrl { | ||
boolean isResolved | ||
String url | ||
|
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
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.
In other cases we've guarded using
metaClass.respondsTo("...")
. Which do you consider preferable?