-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid resolving configurations that are deprecated for resolving
Certain configurations can be reported as "Deprecated for resolving", which is detectable via the`DeprecatableConfiguration.canSafelyBeResolved` internal API. Resolving these configurations during dependency graph generationg can be problematic, particularly in the case of strict Dependency Locking, since no lockfile is likely to be generated for these configurations. With this fix, reflection is used to attempt to determine if a Configuration can be safely resolved, avoiding resolution of deprecated configurations. Fixes #128
- Loading branch information
Showing
5 changed files
with
145 additions
and
28 deletions.
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
99 changes: 99 additions & 0 deletions
99
.../groovy/org/gradle/github/dependencygraph/DependencyLockingDependencyExtractorTest.groovy
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.gradle.github.dependencygraph | ||
|
||
import org.gradle.test.fixtures.PluginPublisher | ||
import org.gradle.test.fixtures.maven.MavenModule | ||
import org.gradle.util.GradleVersion | ||
import spock.lang.IgnoreIf | ||
|
||
class DependencyLockingDependencyExtractorTest extends BaseExtractorTest { | ||
private MavenModule foo | ||
private MavenModule bar | ||
private MavenModule baz | ||
private File settingsFile | ||
private File buildFile | ||
|
||
def setup() { | ||
establishEnvironmentVariables() | ||
|
||
foo = mavenRepo.module("org.test", "foo", "1.0").publish() | ||
|
||
settingsFile = file("settings.gradle") << """ | ||
rootProject.name = 'a' | ||
""" | ||
|
||
buildFile = file("build.gradle") << """ | ||
apply plugin: 'java' | ||
repositories { | ||
maven { url "${mavenRepo.uri}" } | ||
} | ||
""" | ||
} | ||
|
||
def "extracts dependencies when dependency locking is enabled"() { | ||
given: | ||
buildFile << """ | ||
dependencies { | ||
implementation "org.test:foo:+" | ||
} | ||
dependencyLocking { | ||
lockAllConfigurations() | ||
} | ||
""" | ||
|
||
// Write dependency lock file | ||
run("dependencies", "--write-locks") | ||
mavenRepo.module("org.test", "foo", "1.1").publish() | ||
|
||
when: | ||
applyDependencyGraphPlugin() | ||
run() | ||
|
||
then: | ||
def manifest = gitHubManifest() | ||
manifest.sourceFile == "settings.gradle" | ||
|
||
manifest.assertResolved([ | ||
"org.test:foo:1.0": [ | ||
package_url: purlFor(foo) | ||
] | ||
]) | ||
} | ||
|
||
@IgnoreIf({ | ||
// `LockMode.STRICT` was introduced in Gradle 6.1 | ||
GradleVersion.version(testGradleVersion) < GradleVersion.version("6.1") | ||
}) | ||
def "extracts dependencies when Strict dependency locking is enabled"() { | ||
given: | ||
buildFile << """ | ||
dependencies { | ||
implementation "org.test:foo:+" | ||
} | ||
dependencyLocking { | ||
lockAllConfigurations() | ||
lockMode = LockMode.STRICT | ||
} | ||
""" | ||
|
||
// Write dependency lock file | ||
run("dependencies", "--write-locks") | ||
mavenRepo.module("org.test", "foo", "1.1").publish() | ||
|
||
when: | ||
applyDependencyGraphPlugin() | ||
run() | ||
|
||
then: | ||
def manifest = gitHubManifest() | ||
manifest.sourceFile == "settings.gradle" | ||
|
||
manifest.assertResolved([ | ||
"org.test:foo:1.0": [ | ||
package_url: purlFor(foo) | ||
] | ||
]) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
plugin/src/main/kotlin/org/gradle/forceresolve/AbstractResolveProjectDependenciesTask.kt
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.gradle.forceresolve | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.dependencygraph.extractor.ResolvedConfigurationFilter | ||
import org.gradle.work.DisableCachingByDefault | ||
import java.lang.reflect.Method | ||
|
||
@DisableCachingByDefault(because = "Not worth caching") | ||
abstract class AbstractResolveProjectDependenciesTask : DefaultTask() { | ||
private val canSafelyBeResolvedMethod: Method? = getCanSafelyBeResolvedMethod() | ||
|
||
@Internal | ||
var configurationFilter: ResolvedConfigurationFilter? = null | ||
|
||
@Internal | ||
protected fun getReportableConfigurations(): List<Configuration> { | ||
return project.configurations.filter { | ||
canSafelyBeResolved(it) && configurationFilter!!.include(project.path, it.name) | ||
} | ||
} | ||
|
||
/** | ||
* If `DeprecatableConfiguration.canSafelyBeResolve()` is available, use it. | ||
* Else fall back to `Configuration.canBeResolved`. | ||
*/ | ||
private fun canSafelyBeResolved(configuration: Configuration): Boolean { | ||
if (canSafelyBeResolvedMethod != null) { | ||
return canSafelyBeResolvedMethod.invoke(configuration) as Boolean | ||
} | ||
return configuration.isCanBeResolved | ||
} | ||
|
||
private fun getCanSafelyBeResolvedMethod(): Method? { | ||
return try { | ||
val dc = Class.forName("org.gradle.internal.deprecation.DeprecatableConfiguration") | ||
dc.getMethod("canSafelyBeResolved") | ||
} catch (e: ReflectiveOperationException) { | ||
null | ||
} | ||
} | ||
} |
14 changes: 1 addition & 13 deletions
14
plugin/src/main/kotlin/org/gradle/forceresolve/LegacyResolveProjectDependenciesTask.kt
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
15 changes: 1 addition & 14 deletions
15
plugin/src/main/kotlin/org/gradle/forceresolve/ResolveProjectDependenciesTask.kt
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