From cc7c461e087303bcb6f0f65d57cfb3436c63de95 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Mon, 26 Aug 2019 17:04:07 +0200 Subject: [PATCH 01/16] Add more versions for guice --- .../com/google/inject/guice/2.2/guice-2.2.pom | 240 +++++++++++++++++ .../com/google/inject/guice/3.1/guice-3.1.pom | 241 ++++++++++++++++++ .../google/inject/guice/maven-metadata.xml | 3 + 3 files changed, 484 insertions(+) create mode 100644 src/test/resources/maven/com/google/inject/guice/2.2/guice-2.2.pom create mode 100644 src/test/resources/maven/com/google/inject/guice/3.1/guice-3.1.pom diff --git a/src/test/resources/maven/com/google/inject/guice/2.2/guice-2.2.pom b/src/test/resources/maven/com/google/inject/guice/2.2/guice-2.2.pom new file mode 100644 index 00000000..55e550d5 --- /dev/null +++ b/src/test/resources/maven/com/google/inject/guice/2.2/guice-2.2.pom @@ -0,0 +1,240 @@ + + + + 4.0.0 + + + com.google.inject + guice-parent + 3.0 + + + guice + + Google Guice - Core Library + + + 2.2.1-v20090111 + + + + + javax.inject + javax.inject + 1 + + + aopalliance + aopalliance + 1.0 + + + + org.sonatype.sisu.inject + cglib + ${cglib.version} + + + javax.inject + javax.inject-tck + 1 + test + + + org.springframework + spring-beans + 3.0.5.RELEASE + test + + + biz.aQute + bnd + 0.0.384 + test + + + org.apache.felix + org.apache.felix.framework + 3.0.5 + test + + + + + + + + maven-remote-resources-plugin + + + + org.codehaus.mojo + animal-sniffer-maven-plugin + + + maven-surefire-plugin + + + + **/OSGiContainerTest* + **/ScopesTest* + **/TypeConversionTest* + + + + + + org.apache.felix + maven-bundle-plugin + + + + maven-jar-plugin + + + LICENSE + NOTICE + + + + + + + + + + guice.with.no_aop + + + guice.with.no_aop + !false + + + + + + org.sonatype.plugins + munge-maven-plugin + 1.0 + + + prepare-package + + munge-fork + + + NO_AOP + + **/InterceptorBinding.java, + **/InterceptorBindingProcessor.java, + **/InterceptorStackCallback.java, + **/LineNumbers.java, + **/MethodAspect.java, + **/ProxyFactory.java, + **/BytecodeGenTest.java, + **/IntegrationTest.java, + **/MethodInterceptionTest.java, + **/ProxyFactoryTest.java + + + + + + + + maven-jar-plugin + + + no_aop + package + + jar + + + ${project.build.directory}/munged/classes + no_aop + + ${project.build.directory}/munged/classes/META-INF/MANIFEST.MF + + + + + + + + + + + guice.with.jarjar + + + guice.with.jarjar + !false + + + + + + org.sonatype.sisu.inject + cglib + ${cglib.version} + true + + + + + + org.sonatype.plugins + jarjar-maven-plugin + + + *:asm + *:cglib + + + + + + maven-jar-plugin + + + no_deps + package + + jar + + + ${project.build.directory}/original-classes + no_deps + + + + + + + + + + diff --git a/src/test/resources/maven/com/google/inject/guice/3.1/guice-3.1.pom b/src/test/resources/maven/com/google/inject/guice/3.1/guice-3.1.pom new file mode 100644 index 00000000..66674ec4 --- /dev/null +++ b/src/test/resources/maven/com/google/inject/guice/3.1/guice-3.1.pom @@ -0,0 +1,241 @@ + + + + 4.0.0 + + + com.google.inject + guice-parent + 3.0 + + + guice + 3.1 + + Google Guice - Core Library + + + 2.2.1-v20090111 + + + + + javax.inject + javax.inject + 1 + + + aopalliance + aopalliance + 1.0 + + + + org.sonatype.sisu.inject + cglib + ${cglib.version} + + + javax.inject + javax.inject-tck + 1 + test + + + org.springframework + spring-beans + 3.0.5.RELEASE + test + + + biz.aQute + bnd + 0.0.384 + test + + + org.apache.felix + org.apache.felix.framework + 3.0.5 + test + + + + + + + + maven-remote-resources-plugin + + + + org.codehaus.mojo + animal-sniffer-maven-plugin + + + maven-surefire-plugin + + + + **/OSGiContainerTest* + **/ScopesTest* + **/TypeConversionTest* + + + + + + org.apache.felix + maven-bundle-plugin + + + + maven-jar-plugin + + + LICENSE + NOTICE + + + + + + + + + + guice.with.no_aop + + + guice.with.no_aop + !false + + + + + + org.sonatype.plugins + munge-maven-plugin + 1.0 + + + prepare-package + + munge-fork + + + NO_AOP + + **/InterceptorBinding.java, + **/InterceptorBindingProcessor.java, + **/InterceptorStackCallback.java, + **/LineNumbers.java, + **/MethodAspect.java, + **/ProxyFactory.java, + **/BytecodeGenTest.java, + **/IntegrationTest.java, + **/MethodInterceptionTest.java, + **/ProxyFactoryTest.java + + + + + + + + maven-jar-plugin + + + no_aop + package + + jar + + + ${project.build.directory}/munged/classes + no_aop + + ${project.build.directory}/munged/classes/META-INF/MANIFEST.MF + + + + + + + + + + + guice.with.jarjar + + + guice.with.jarjar + !false + + + + + + org.sonatype.sisu.inject + cglib + ${cglib.version} + true + + + + + + org.sonatype.plugins + jarjar-maven-plugin + + + *:asm + *:cglib + + + + + + maven-jar-plugin + + + no_deps + package + + jar + + + ${project.build.directory}/original-classes + no_deps + + + + + + + + + + diff --git a/src/test/resources/maven/com/google/inject/guice/maven-metadata.xml b/src/test/resources/maven/com/google/inject/guice/maven-metadata.xml index 70c1249c..b90c67ab 100644 --- a/src/test/resources/maven/com/google/inject/guice/maven-metadata.xml +++ b/src/test/resources/maven/com/google/inject/guice/maven-metadata.xml @@ -8,7 +8,10 @@ 1.0 2.0 + 2.1 + 2.2 3.0 + 3.1 20100320162336 From feafafb8d7346c642759d66460f9b89782e666e9 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Mon, 26 Aug 2019 17:04:51 +0200 Subject: [PATCH 02/16] Update tests --- .../versions/DifferentGradleVersionsSpec.groovy | 11 ++++++++++- .../benmanes/gradle/versions/JavaLibrarySpec.groovy | 2 +- .../gradle/versions/KotlinDslUsageSpec.groovy | 7 +++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy index b1336998..dfad9abc 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy @@ -55,6 +55,16 @@ final class DifferentGradleVersionsSpec extends Specification { dependencies { compile 'com.google.inject:guice:2.0' } + + dependencyUpdates.resolutionStrategy { + componentSelection { + all { + if (candidate.version == "3.1" && current == "2.0") { + reject("Guice 3.1 not allowed") + } + } + } + } """.stripIndent() when: @@ -71,7 +81,6 @@ final class DifferentGradleVersionsSpec extends Specification { where: gradleVersion << [ - '2.0', '3.3', '3.4', '3.5', diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/JavaLibrarySpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/JavaLibrarySpec.groovy index 64d19e8e..fb199680 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/JavaLibrarySpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/JavaLibrarySpec.groovy @@ -43,7 +43,7 @@ final class JavaLibrarySpec extends Specification { .build() then: - result.output.contains('com.google.inject:guice [2.0 -> 3.0]') + result.output.contains('com.google.inject:guice [2.0 -> 3.1]') result.task(':dependencyUpdates').outcome == SUCCESS } } diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy index 94d4fb07..a5dac584 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy @@ -46,8 +46,8 @@ final class KotlinDslUsageSpec extends Specification { resolutionStrategy { componentSelection { all { - if (candidate.version == "3.0") { - reject("Guava 3.0 not allowed") + if (candidate.version == "3.1" && current != "") { + reject("Guice 3.1 not allowed") } } } @@ -66,8 +66,7 @@ final class KotlinDslUsageSpec extends Specification { .build() then: - result.output.contains('''Failed to determine the latest version for the following dependencies (use --info for details): - | - com.google.inject:guice'''.stripMargin()) + result.output.contains('''com.google.inject:guice [2.0 -> 3.0]''') srdErrWriter.toString().empty where: From 9c512861263ccd808c3e6178d0035b9bd6b9ca9e Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Mon, 26 Aug 2019 17:05:49 +0200 Subject: [PATCH 03/16] Decorate ComponentSelection with current version --- .../gradle/versions/updates/Coordinate.groovy | 5 + .../versions/updates/DependencyUpdates.groovy | 6 +- .../updates/DependencyUpdatesTask.groovy | 10 +- .../gradle/versions/updates/Resolver.groovy | 21 ++-- .../ComponentSelectionRulesWithCurrent.groovy | 61 +++++++++++ .../ComponentSelectionWithCurrent.groovy | 28 +++++ .../ResolutionStrategyWithCurrent.groovy | 103 ++++++++++++++++++ 7 files changed, 218 insertions(+), 16 deletions(-) create mode 100644 src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy create mode 100644 src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy create mode 100644 src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/Coordinate.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/Coordinate.groovy index 82a10f21..55487d51 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/Coordinate.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/Coordinate.groovy @@ -20,6 +20,7 @@ import groovy.transform.TypeChecked import org.gradle.api.artifacts.Dependency import org.gradle.api.artifacts.ModuleVersionIdentifier import org.gradle.api.artifacts.ModuleVersionSelector +import org.gradle.api.artifacts.component.ModuleComponentIdentifier /** * The dependency's coordinate. @@ -64,6 +65,10 @@ class Coordinate implements Comparable { return new Coordinate(identifier.group, identifier.name, identifier.version) } + static Coordinate from(ModuleComponentIdentifier identifier) { + return new Coordinate(identifier.group, identifier.module, identifier.version) + } + @EqualsAndHashCode static class Key implements Comparable { final String groupId diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdates.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdates.groovy index 6b37d426..a4d9a2d4 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdates.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdates.groovy @@ -16,14 +16,14 @@ package com.github.benmanes.gradle.versions.updates import com.github.benmanes.gradle.versions.updates.gradle.GradleUpdateChecker +import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ResolutionStrategyWithCurrent import groovy.transform.TupleConstructor import groovy.transform.TypeChecked +import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.UnresolvedDependency -import static groovy.transform.TypeCheckingMode.SKIP - /** * An evaluator for reporting of which dependencies have later versions. *

@@ -38,7 +38,7 @@ import static groovy.transform.TypeCheckingMode.SKIP @TupleConstructor class DependencyUpdates { Project project - Closure resolutionStrategy + Action resolutionStrategy String revision Object outputFormatter String outputDir diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy index 2d4fdc86..219dadc5 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy @@ -15,10 +15,10 @@ */ package com.github.benmanes.gradle.versions.updates +import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ResolutionStrategyWithCurrent import groovy.transform.TypeChecked import org.gradle.api.Action import org.gradle.api.DefaultTask -import org.gradle.api.artifacts.ResolutionStrategy import org.gradle.api.tasks.Input import org.gradle.api.tasks.Optional import org.gradle.api.tasks.TaskAction @@ -53,7 +53,7 @@ class DependencyUpdatesTask extends DefaultTask { boolean checkForGradleUpdate = true Object outputFormatter = 'plain'; - Closure resolutionStrategy = null; + Action resolutionStrategy = null; DependencyUpdatesTask() { description = 'Displays the dependency updates for the project.' @@ -76,10 +76,8 @@ class DependencyUpdatesTask extends DefaultTask { * Sets the {@link #resolutionStrategy} to the provided strategy. * @param resolutionStrategy the resolution strategy */ - void resolutionStrategy(final Action resolutionStrategy) { - // The delegate of the Closure body is ResolutionStrategy: - // https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/Configuration.html#resolutionStrategy-groovy.lang.Closure- - this.resolutionStrategy = { resolutionStrategy.execute(delegate as ResolutionStrategy) } + void resolutionStrategy(final Action resolutionStrategy) { + this.resolutionStrategy = resolutionStrategy } /** Returns the resolution revision level. */ diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy index eda6a9a1..b4345165 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy @@ -15,9 +15,11 @@ */ package com.github.benmanes.gradle.versions.updates +import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ResolutionStrategyWithCurrent import groovy.transform.TypeChecked import java.util.concurrent.ConcurrentMap import java.util.concurrent.ConcurrentHashMap +import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.artifacts.ComponentMetadata import org.gradle.api.artifacts.ComponentSelection @@ -51,12 +53,12 @@ import static org.gradle.api.specs.Specs.SATISFIES_ALL @TypeChecked class Resolver { final Project project - final Closure resolutionStrategy + final Action resolutionStrategy final boolean useSelectionRules final boolean collectProjectUrls final ConcurrentMap projectUrls - Resolver(Project project, Closure resolutionStrategy) { + Resolver(Project project, Action resolutionStrategy) { this.projectUrls = new ConcurrentHashMap<>() this.resolutionStrategy = resolutionStrategy this.project = project @@ -72,7 +74,7 @@ class Resolver { /** Returns the version status of the configuration's dependencies at the given revision. */ public Set resolve(Configuration configuration, String revision) { Map coordinates = getCurrentCoordinates(configuration) - Configuration latestConfiguration = createLatestConfiguration(configuration, revision) + Configuration latestConfiguration = createLatestConfiguration(configuration, revision, coordinates) LenientConfiguration lenient = latestConfiguration.resolvedConfiguration.lenientConfiguration Set resolved = lenient.getFirstLevelModuleDependencies(SATISFIES_ALL) @@ -106,7 +108,7 @@ class Resolver { } /** Returns a copy of the configuration where dependencies will be resolved up to the revision. */ - private Configuration createLatestConfiguration(Configuration configuration, String revision) { + private Configuration createLatestConfiguration(Configuration configuration, String revision, Map currentCoordinates) { List latest = configuration.dependencies.findAll { dependency -> dependency instanceof ExternalDependency }.collect { dependency -> @@ -124,7 +126,7 @@ class Resolver { if (useSelectionRules) { addRevisionFilter(copy, revision) - addCustomResolutionStrategy(copy) + addCustomResolutionStrategy(copy, currentCoordinates) } return copy } @@ -165,9 +167,14 @@ class Resolver { } /** Adds a custom resolution strategy only applicable for the dependency updates task. */ - private void addCustomResolutionStrategy(Configuration configuration) { + private void addCustomResolutionStrategy(Configuration configuration, Map currentCoordinates) { if (resolutionStrategy != null) { - configuration.resolutionStrategy(resolutionStrategy) + configuration.resolutionStrategy(new Action() { + @java.lang.Override + void execute(ResolutionStrategy inner) { + resolutionStrategy.execute(new ResolutionStrategyWithCurrent(inner as ResolutionStrategy, currentCoordinates)) + } + }) } } diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy new file mode 100644 index 00000000..28300d81 --- /dev/null +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy @@ -0,0 +1,61 @@ +package com.github.benmanes.gradle.versions.updates.resolutionstrategy + +import com.github.benmanes.gradle.versions.updates.Coordinate +import org.gradle.api.Action +import org.gradle.api.artifacts.ComponentSelection +import org.gradle.api.artifacts.ComponentSelectionRules + +class ComponentSelectionRulesWithCurrent { + + private ComponentSelectionRules delegate + private Map currentCoordinates + + ComponentSelectionRulesWithCurrent(ComponentSelectionRules delegate, Map currentCoordinates) { + this.delegate = delegate + this.currentCoordinates = currentCoordinates + } + + ComponentSelectionRulesWithCurrent all(Action selectionAction) { + delegate.all(new Action() { + void execute(ComponentSelection inner) { + Coordinate candidateCoordinate = Coordinate.from(inner.candidate) + Coordinate current = currentCoordinates.get(candidateCoordinate.key) + selectionAction.execute(new ComponentSelectionWithCurrent(inner, current.version)) + } + }) + return this + } + + ComponentSelectionRulesWithCurrent all(Closure closure) { + delegate.all(new Action() { + void execute(ComponentSelection inner) { + Coordinate candidateCoordinate = Coordinate.from(inner.candidate) + Coordinate current = currentCoordinates.get(candidateCoordinate.key) + ComponentSelectionWithCurrent wrapped = new ComponentSelectionWithCurrent(inner, current.version) + closure.delegate = wrapped + closure(wrapped) + } + }) + return this + } + + ComponentSelectionRulesWithCurrent all(Object ruleSource) { + delegate.all(ruleSource) + return this + } + + ComponentSelectionRulesWithCurrent withModule(Object id, Action selectionAction) { + delegate.withModule(id, selectionAction) + return this + } + + ComponentSelectionRulesWithCurrent withModule(Object id, Closure closure) { + delegate.withModule(id, closure) + return this + } + + ComponentSelectionRulesWithCurrent withModule(Object id, Object ruleSource) { + delegate.withModule(id, ruleSource) + return this + } +} diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy new file mode 100644 index 00000000..a82790bf --- /dev/null +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy @@ -0,0 +1,28 @@ +package com.github.benmanes.gradle.versions.updates.resolutionstrategy + +import org.gradle.api.artifacts.ComponentSelection +import org.gradle.api.artifacts.component.ModuleComponentIdentifier + +class ComponentSelectionWithCurrent { + + private ComponentSelection delegate + + private String current + + ComponentSelectionWithCurrent(ComponentSelection delegate, String current) { + this.delegate = delegate + this.current = current + } + + ModuleComponentIdentifier getCandidate() { + return delegate.getCandidate() + } + + String getCurrent() { + return current + } + + void reject(String reason) { + delegate.reject(reason) + } +} diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy new file mode 100644 index 00000000..80c7a42f --- /dev/null +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy @@ -0,0 +1,103 @@ +package com.github.benmanes.gradle.versions.updates.resolutionstrategy + +import com.github.benmanes.gradle.versions.updates.Coordinate +import org.gradle.api.Action +import org.gradle.api.artifacts.DependencyResolveDetails +import org.gradle.api.artifacts.DependencySubstitutions +import org.gradle.api.artifacts.ModuleVersionSelector +import org.gradle.api.artifacts.ResolutionStrategy +import org.gradle.api.artifacts.ResolutionStrategy.SortOrder + +import java.util.concurrent.TimeUnit; + +class ResolutionStrategyWithCurrent { + + private ResolutionStrategy delegate + private Map currentCoordinates + + ResolutionStrategyWithCurrent(ResolutionStrategy delegate, Map currentCoordinates) { + this.delegate = delegate + this.currentCoordinates = currentCoordinates + } + + ResolutionStrategyWithCurrent failOnVersionConflict() { + delegate.failOnVersionConflict() + return this + } + + void preferProjectModules() { + delegate.preferProjectModules() + } + + ResolutionStrategyWithCurrent activateDependencyLocking() { + delegate.activateDependencyLocking() + return this + } + + ResolutionStrategyWithCurrent force(Object... moduleVersionSelectorNotations) { + delegate.force(moduleVersionSelectorNotations) + return this + } + + ResolutionStrategyWithCurrent setForcedModules(Object... moduleVersionSelectorNotations) { + delegate.setForcedModules(moduleVersionSelectorNotations) + return this + } + + Set getForcedModules() { + return delegate.getForcedModules() + } + + ResolutionStrategyWithCurrent eachDependency(Action rule) { + delegate.eachDependency(rule) + return this + } + + void cacheDynamicVersionsFor(int value, String units) { + delegate.cacheDynamicVersionsFor(value, units) + } + + void cacheDynamicVersionsFor(int value, TimeUnit units) { + delegate.cacheDynamicVersionsFor(value, units) + } + + void cacheChangingModulesFor(int value, String units) { + delegate.cacheChangingModulesFor(value, units) + } + + void cacheChangingModulesFor(int value, TimeUnit units) { + delegate.cacheChangingModulesFor(value, units) + } + + ComponentSelectionRulesWithCurrent getComponentSelection() { + return new ComponentSelectionRulesWithCurrent(delegate.getComponentSelection(), currentCoordinates) + } + + ResolutionStrategyWithCurrent componentSelection(Action action) { + action.execute(getComponentSelection()) + return this + } + + ResolutionStrategyWithCurrent componentSelection(Closure closure) { + return componentSelection(new Action() { + @java.lang.Override + void execute(ComponentSelectionRulesWithCurrent componentSelectionRulesWithCurrent) { + closure.delegate = componentSelectionRulesWithCurrent + closure(componentSelectionRulesWithCurrent) + } + }) + } + + DependencySubstitutions getDependencySubstitution() { + return delegate.getDependencySubstitution() + } + + ResolutionStrategyWithCurrent dependencySubstitution(Action action) { + delegate.dependencySubstitution(action) + return this + } + + void sortArtifacts(SortOrder sortOrder) { + delegate.sortArtifacts(sortOrder) + } +} From 5670917561eee5a08b6226e7d8117639b9763d57 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Mon, 26 Aug 2019 17:48:44 +0200 Subject: [PATCH 04/16] Decorate ComponentSelection with current version for rule-source --- .../ComponentSelectionRulesWithCurrent.groovy | 12 ++- .../ComponentSelectionRuleSourceSpec.groovy | 87 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy index 28300d81..4eb996ed 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy @@ -4,6 +4,9 @@ import com.github.benmanes.gradle.versions.updates.Coordinate import org.gradle.api.Action import org.gradle.api.artifacts.ComponentSelection import org.gradle.api.artifacts.ComponentSelectionRules +import org.gradle.internal.rules.RuleAction +import org.gradle.internal.rules.RuleSourceBackedRuleAction +import org.gradle.model.internal.type.ModelType class ComponentSelectionRulesWithCurrent { @@ -40,7 +43,14 @@ class ComponentSelectionRulesWithCurrent { } ComponentSelectionRulesWithCurrent all(Object ruleSource) { - delegate.all(ruleSource) + RuleAction ruleAction = RuleSourceBackedRuleAction.create(ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) + delegate.all(new Action() { + void execute(ComponentSelection inner) { + Coordinate candidateCoordinate = Coordinate.from(inner.candidate) + Coordinate current = currentCoordinates.get(candidateCoordinate.key) + ruleAction.execute(new ComponentSelectionWithCurrent(inner, current.version), []) + } + }) return this } diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy new file mode 100644 index 00000000..c8476fd8 --- /dev/null +++ b/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy @@ -0,0 +1,87 @@ +package com.github.benmanes.gradle.versions + +import org.gradle.testkit.runner.GradleRunner +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Specification +import spock.lang.Unroll + +final class ComponentSelectionRuleSourceSpec extends Specification { + + @Rule TemporaryFolder testProjectDir = new TemporaryFolder() + private File buildFile + private List pluginClasspath + + def 'setup'() { + def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.txt") + if (pluginClasspathResource == null) { + throw new IllegalStateException( + "Did not find plugin classpath resource, run `testClasses` build task.") + } + + pluginClasspath = pluginClasspathResource.readLines().collect { new File(it) } + } + + @Unroll + def 'component selection works with rule-source'() { + given: + def classpathString = pluginClasspath + .collect { it.absolutePath.replace('\\', '\\\\') } // escape backslashes in Windows paths + .collect { "'$it'" } + .join(", ") + def mavenRepoUrl = getClass().getResource('/maven/').toURI() + def srdErrWriter = new StringWriter() + + buildFile = testProjectDir.newFile('build.gradle') + buildFile << + """ + import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ComponentSelectionWithCurrent + + buildscript { + dependencies { + classpath files($classpathString) + } + } + + apply plugin: 'java' + apply plugin: "com.github.ben-manes.versions" + + repositories { + maven { + url '${mavenRepoUrl}' + } + } + + dependencies { + compile 'com.google.inject:guice:2.0' + } + + dependencyUpdates.resolutionStrategy { + componentSelection { + all(new Rule()) + } + } + + class Rule { + + @Mutate + void select(ComponentSelectionWithCurrent selection) { + if (selection.candidate.version == "3.1" && selection.current == "2.0") { + selection.reject("Guice 3.1 not allowed") + } + } + } + """.stripIndent() + + when: + def result = GradleRunner.create() + .withProjectDir(testProjectDir.root) + .withArguments('dependencyUpdates') + .forwardStdError(srdErrWriter) + .build() + + then: + result.output.contains('com.google.inject:guice [2.0 -> 3.0]') + srdErrWriter.toString().empty + } +} From 701146bf69fa1501892b88d9b82304a99df68d56 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Tue, 27 Aug 2019 09:36:26 +0200 Subject: [PATCH 05/16] Extract common code --- .../ComponentSelectionRulesWithCurrent.groovy | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy index 4eb996ed..90bc57c9 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy @@ -21,9 +21,7 @@ class ComponentSelectionRulesWithCurrent { ComponentSelectionRulesWithCurrent all(Action selectionAction) { delegate.all(new Action() { void execute(ComponentSelection inner) { - Coordinate candidateCoordinate = Coordinate.from(inner.candidate) - Coordinate current = currentCoordinates.get(candidateCoordinate.key) - selectionAction.execute(new ComponentSelectionWithCurrent(inner, current.version)) + selectionAction.execute(wrapComponentSelection(inner)) } }) return this @@ -32,9 +30,7 @@ class ComponentSelectionRulesWithCurrent { ComponentSelectionRulesWithCurrent all(Closure closure) { delegate.all(new Action() { void execute(ComponentSelection inner) { - Coordinate candidateCoordinate = Coordinate.from(inner.candidate) - Coordinate current = currentCoordinates.get(candidateCoordinate.key) - ComponentSelectionWithCurrent wrapped = new ComponentSelectionWithCurrent(inner, current.version) + ComponentSelectionWithCurrent wrapped = wrapComponentSelection(inner) closure.delegate = wrapped closure(wrapped) } @@ -46,9 +42,7 @@ class ComponentSelectionRulesWithCurrent { RuleAction ruleAction = RuleSourceBackedRuleAction.create(ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) delegate.all(new Action() { void execute(ComponentSelection inner) { - Coordinate candidateCoordinate = Coordinate.from(inner.candidate) - Coordinate current = currentCoordinates.get(candidateCoordinate.key) - ruleAction.execute(new ComponentSelectionWithCurrent(inner, current.version), []) + ruleAction.execute(wrapComponentSelection(inner), []) } }) return this @@ -68,4 +62,11 @@ class ComponentSelectionRulesWithCurrent { delegate.withModule(id, ruleSource) return this } + + private ComponentSelectionWithCurrent wrapComponentSelection(ComponentSelection inner) { + Coordinate candidateCoordinate = Coordinate.from(inner.candidate) + Coordinate current = currentCoordinates.get(candidateCoordinate.key) + ComponentSelectionWithCurrent wrapped = new ComponentSelectionWithCurrent(inner, current.version) + return wrapped + } } From 9c3ae56274fb7962654cc7a36dc761b513f8a791 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 07:27:55 +0200 Subject: [PATCH 06/16] Decorate calls to withModule --- .../ComponentSelectionRulesWithCurrent.groovy | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy index 90bc57c9..f4843dde 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy @@ -49,17 +49,32 @@ class ComponentSelectionRulesWithCurrent { } ComponentSelectionRulesWithCurrent withModule(Object id, Action selectionAction) { - delegate.withModule(id, selectionAction) + delegate.withModule(id, new Action() { + void execute(ComponentSelection inner) { + selectionAction.execute(wrapComponentSelection(inner)) + } + }) return this } ComponentSelectionRulesWithCurrent withModule(Object id, Closure closure) { - delegate.withModule(id, closure) + delegate.withModule(id, new Action() { + void execute(ComponentSelection inner) { + ComponentSelectionWithCurrent wrapped = wrapComponentSelection(inner) + closure.delegate = wrapped + closure(wrapped) + } + }) return this } ComponentSelectionRulesWithCurrent withModule(Object id, Object ruleSource) { - delegate.withModule(id, ruleSource) + RuleAction ruleAction = RuleSourceBackedRuleAction.create(ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) + delegate.withModule(id, new Action() { + void execute(ComponentSelection inner) { + ruleAction.execute(wrapComponentSelection(inner), []) + } + }) return this } From d99696500e9bd98c1af399a311b411576ef41010 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 07:34:33 +0200 Subject: [PATCH 07/16] Add newer gradle versions to test --- .../gradle/versions/DifferentGradleVersionsSpec.groovy | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy index dfad9abc..b95c9a7f 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy @@ -95,7 +95,13 @@ final class DifferentGradleVersionsSpec extends Specification { '4.8', '4.9', '4.10', - '5.0' + '5.0', + '5.1.1', + '5.2.1', + '5.3.1', + '5.4.1', + '5.5.1', + '5.6.1' ] } From 5cdcc00988236fb1c9c1a71f810f86e0bd968a41 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 07:56:20 +0200 Subject: [PATCH 08/16] Fix line length issues --- .../gradle/versions/updates/Resolver.groovy | 12 ++++++++---- .../ComponentSelectionRulesWithCurrent.groovy | 18 ++++++++++++------ .../ResolutionStrategyWithCurrent.groovy | 12 ++++++++---- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy index b4345165..67579dff 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/Resolver.groovy @@ -74,7 +74,8 @@ class Resolver { /** Returns the version status of the configuration's dependencies at the given revision. */ public Set resolve(Configuration configuration, String revision) { Map coordinates = getCurrentCoordinates(configuration) - Configuration latestConfiguration = createLatestConfiguration(configuration, revision, coordinates) + Configuration latestConfiguration = createLatestConfiguration(configuration, revision, + coordinates) LenientConfiguration lenient = latestConfiguration.resolvedConfiguration.lenientConfiguration Set resolved = lenient.getFirstLevelModuleDependencies(SATISFIES_ALL) @@ -108,7 +109,8 @@ class Resolver { } /** Returns a copy of the configuration where dependencies will be resolved up to the revision. */ - private Configuration createLatestConfiguration(Configuration configuration, String revision, Map currentCoordinates) { + private Configuration createLatestConfiguration(Configuration configuration, String revision, + Map currentCoordinates) { List latest = configuration.dependencies.findAll { dependency -> dependency instanceof ExternalDependency }.collect { dependency -> @@ -167,12 +169,14 @@ class Resolver { } /** Adds a custom resolution strategy only applicable for the dependency updates task. */ - private void addCustomResolutionStrategy(Configuration configuration, Map currentCoordinates) { + private void addCustomResolutionStrategy(Configuration configuration, + Map currentCoordinates) { if (resolutionStrategy != null) { configuration.resolutionStrategy(new Action() { @java.lang.Override void execute(ResolutionStrategy inner) { - resolutionStrategy.execute(new ResolutionStrategyWithCurrent(inner as ResolutionStrategy, currentCoordinates)) + resolutionStrategy.execute(new ResolutionStrategyWithCurrent(inner as ResolutionStrategy, + currentCoordinates)) } }) } diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy index f4843dde..9eadcf7d 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy @@ -13,12 +13,14 @@ class ComponentSelectionRulesWithCurrent { private ComponentSelectionRules delegate private Map currentCoordinates - ComponentSelectionRulesWithCurrent(ComponentSelectionRules delegate, Map currentCoordinates) { + ComponentSelectionRulesWithCurrent(ComponentSelectionRules delegate, + Map currentCoordinates) { this.delegate = delegate this.currentCoordinates = currentCoordinates } - ComponentSelectionRulesWithCurrent all(Action selectionAction) { + ComponentSelectionRulesWithCurrent all( + Action selectionAction) { delegate.all(new Action() { void execute(ComponentSelection inner) { selectionAction.execute(wrapComponentSelection(inner)) @@ -39,7 +41,8 @@ class ComponentSelectionRulesWithCurrent { } ComponentSelectionRulesWithCurrent all(Object ruleSource) { - RuleAction ruleAction = RuleSourceBackedRuleAction.create(ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) + RuleAction ruleAction = RuleSourceBackedRuleAction.create( + ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) delegate.all(new Action() { void execute(ComponentSelection inner) { ruleAction.execute(wrapComponentSelection(inner), []) @@ -48,7 +51,8 @@ class ComponentSelectionRulesWithCurrent { return this } - ComponentSelectionRulesWithCurrent withModule(Object id, Action selectionAction) { + ComponentSelectionRulesWithCurrent withModule(Object id, + Action selectionAction) { delegate.withModule(id, new Action() { void execute(ComponentSelection inner) { selectionAction.execute(wrapComponentSelection(inner)) @@ -69,7 +73,8 @@ class ComponentSelectionRulesWithCurrent { } ComponentSelectionRulesWithCurrent withModule(Object id, Object ruleSource) { - RuleAction ruleAction = RuleSourceBackedRuleAction.create(ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) + RuleAction ruleAction = RuleSourceBackedRuleAction.create( + ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) delegate.withModule(id, new Action() { void execute(ComponentSelection inner) { ruleAction.execute(wrapComponentSelection(inner), []) @@ -81,7 +86,8 @@ class ComponentSelectionRulesWithCurrent { private ComponentSelectionWithCurrent wrapComponentSelection(ComponentSelection inner) { Coordinate candidateCoordinate = Coordinate.from(inner.candidate) Coordinate current = currentCoordinates.get(candidateCoordinate.key) - ComponentSelectionWithCurrent wrapped = new ComponentSelectionWithCurrent(inner, current.version) + ComponentSelectionWithCurrent wrapped = new ComponentSelectionWithCurrent(inner, + current.version) return wrapped } } diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy index 80c7a42f..0e439f7f 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy @@ -15,7 +15,8 @@ class ResolutionStrategyWithCurrent { private ResolutionStrategy delegate private Map currentCoordinates - ResolutionStrategyWithCurrent(ResolutionStrategy delegate, Map currentCoordinates) { + ResolutionStrategyWithCurrent(ResolutionStrategy delegate, + Map currentCoordinates) { this.delegate = delegate this.currentCoordinates = currentCoordinates } @@ -70,10 +71,12 @@ class ResolutionStrategyWithCurrent { } ComponentSelectionRulesWithCurrent getComponentSelection() { - return new ComponentSelectionRulesWithCurrent(delegate.getComponentSelection(), currentCoordinates) + return new ComponentSelectionRulesWithCurrent(delegate.getComponentSelection(), + currentCoordinates) } - ResolutionStrategyWithCurrent componentSelection(Action action) { + ResolutionStrategyWithCurrent componentSelection( + Action action) { action.execute(getComponentSelection()) return this } @@ -92,7 +95,8 @@ class ResolutionStrategyWithCurrent { return delegate.getDependencySubstitution() } - ResolutionStrategyWithCurrent dependencySubstitution(Action action) { + ResolutionStrategyWithCurrent dependencySubstitution( + Action action) { delegate.dependencySubstitution(action) return this } From a88153f4dafb7e297f9af65351049f079436e7c3 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 08:15:38 +0200 Subject: [PATCH 09/16] Use delegate annotation for ComponentSelectionWithCurrent --- .../ComponentSelectionWithCurrent.groovy | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy index a82790bf..7bfaa338 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy @@ -1,10 +1,10 @@ package com.github.benmanes.gradle.versions.updates.resolutionstrategy import org.gradle.api.artifacts.ComponentSelection -import org.gradle.api.artifacts.component.ModuleComponentIdentifier class ComponentSelectionWithCurrent { + @Delegate private ComponentSelection delegate private String current @@ -14,15 +14,7 @@ class ComponentSelectionWithCurrent { this.current = current } - ModuleComponentIdentifier getCandidate() { - return delegate.getCandidate() - } - String getCurrent() { return current } - - void reject(String reason) { - delegate.reject(reason) - } } From 558b0b654a8c3020a9e5beb3dd77859d4ede869d Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 08:20:13 +0200 Subject: [PATCH 10/16] Use TupleConstructor annotation for ComponentSelectionWithCurrent --- .../ComponentSelectionWithCurrent.groovy | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy index 7bfaa338..69115b79 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy @@ -1,7 +1,9 @@ package com.github.benmanes.gradle.versions.updates.resolutionstrategy +import groovy.transform.TupleConstructor import org.gradle.api.artifacts.ComponentSelection +@TupleConstructor(includeFields=true) class ComponentSelectionWithCurrent { @Delegate @@ -9,11 +11,6 @@ class ComponentSelectionWithCurrent { private String current - ComponentSelectionWithCurrent(ComponentSelection delegate, String current) { - this.delegate = delegate - this.current = current - } - String getCurrent() { return current } From fb4277774c0b8b1a825ab66a14eba726e9e89ceb Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 08:50:03 +0200 Subject: [PATCH 11/16] Rename property to "currentVersion" --- .../ComponentSelectionWithCurrent.groovy | 8 ++------ .../versions/ComponentSelectionRuleSourceSpec.groovy | 2 +- .../gradle/versions/DifferentGradleVersionsSpec.groovy | 2 +- .../benmanes/gradle/versions/KotlinDslUsageSpec.groovy | 2 +- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy index 69115b79..1d84a7f6 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy @@ -7,11 +7,7 @@ import org.gradle.api.artifacts.ComponentSelection class ComponentSelectionWithCurrent { @Delegate - private ComponentSelection delegate + private final ComponentSelection delegate - private String current - - String getCurrent() { - return current - } + public final String currentVersion } diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy index c8476fd8..db31c374 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy @@ -66,7 +66,7 @@ final class ComponentSelectionRuleSourceSpec extends Specification { @Mutate void select(ComponentSelectionWithCurrent selection) { - if (selection.candidate.version == "3.1" && selection.current == "2.0") { + if (selection.candidate.version == "3.1" && selection.currentVersion == "2.0") { selection.reject("Guice 3.1 not allowed") } } diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy index b95c9a7f..a6f4bfa5 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/DifferentGradleVersionsSpec.groovy @@ -59,7 +59,7 @@ final class DifferentGradleVersionsSpec extends Specification { dependencyUpdates.resolutionStrategy { componentSelection { all { - if (candidate.version == "3.1" && current == "2.0") { + if (candidate.version == "3.1" && currentVersion == "2.0") { reject("Guice 3.1 not allowed") } } diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy index a5dac584..0a0c4d1d 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDslUsageSpec.groovy @@ -46,7 +46,7 @@ final class KotlinDslUsageSpec extends Specification { resolutionStrategy { componentSelection { all { - if (candidate.version == "3.1" && current != "") { + if (candidate.version == "3.1" && currentVersion != "") { reject("Guice 3.1 not allowed") } } From 4ebc4abd0f5dc72e17fe9acd1c0205f4c2f0a8b6 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 09:34:57 +0200 Subject: [PATCH 12/16] Use TupleConstructor annotation for ComponentSelectionRulesWithCurrent --- .../ComponentSelectionRulesWithCurrent.groovy | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy index 9eadcf7d..17395516 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy @@ -1,6 +1,7 @@ package com.github.benmanes.gradle.versions.updates.resolutionstrategy import com.github.benmanes.gradle.versions.updates.Coordinate +import groovy.transform.TupleConstructor import org.gradle.api.Action import org.gradle.api.artifacts.ComponentSelection import org.gradle.api.artifacts.ComponentSelectionRules @@ -8,16 +9,11 @@ import org.gradle.internal.rules.RuleAction import org.gradle.internal.rules.RuleSourceBackedRuleAction import org.gradle.model.internal.type.ModelType +@TupleConstructor(includeFields=true) class ComponentSelectionRulesWithCurrent { - private ComponentSelectionRules delegate - private Map currentCoordinates - - ComponentSelectionRulesWithCurrent(ComponentSelectionRules delegate, - Map currentCoordinates) { - this.delegate = delegate - this.currentCoordinates = currentCoordinates - } + private final ComponentSelectionRules delegate + private final Map currentCoordinates ComponentSelectionRulesWithCurrent all( Action selectionAction) { From 3d5119a814a7c2940ac9292c9898df95deed05da Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 09:44:23 +0200 Subject: [PATCH 13/16] Use tuple constructor annotation for ResolutionStrategyWithCurrent --- .../ResolutionStrategyWithCurrent.groovy | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy index 0e439f7f..fec4644e 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy @@ -1,6 +1,7 @@ package com.github.benmanes.gradle.versions.updates.resolutionstrategy import com.github.benmanes.gradle.versions.updates.Coordinate +import groovy.transform.TupleConstructor import org.gradle.api.Action import org.gradle.api.artifacts.DependencyResolveDetails import org.gradle.api.artifacts.DependencySubstitutions @@ -8,19 +9,12 @@ import org.gradle.api.artifacts.ModuleVersionSelector import org.gradle.api.artifacts.ResolutionStrategy import org.gradle.api.artifacts.ResolutionStrategy.SortOrder -import java.util.concurrent.TimeUnit; - +@TupleConstructor(includeFields = true) class ResolutionStrategyWithCurrent { private ResolutionStrategy delegate private Map currentCoordinates - ResolutionStrategyWithCurrent(ResolutionStrategy delegate, - Map currentCoordinates) { - this.delegate = delegate - this.currentCoordinates = currentCoordinates - } - ResolutionStrategyWithCurrent failOnVersionConflict() { delegate.failOnVersionConflict() return this From 118f516c8dfa774439510f28c71c1b4038e876d7 Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 10:23:11 +0200 Subject: [PATCH 14/16] Use delegate annotation on ResolutionStrategyWithCurrent --- .../ResolutionStrategyWithCurrent.groovy | 36 ++----------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy index fec4644e..e96f2764 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ResolutionStrategyWithCurrent.groovy @@ -5,14 +5,14 @@ import groovy.transform.TupleConstructor import org.gradle.api.Action import org.gradle.api.artifacts.DependencyResolveDetails import org.gradle.api.artifacts.DependencySubstitutions -import org.gradle.api.artifacts.ModuleVersionSelector import org.gradle.api.artifacts.ResolutionStrategy -import org.gradle.api.artifacts.ResolutionStrategy.SortOrder @TupleConstructor(includeFields = true) class ResolutionStrategyWithCurrent { + @Delegate(interfaces = false, excludes = ['componentSelection', 'getComponentSelection']) private ResolutionStrategy delegate + private Map currentCoordinates ResolutionStrategyWithCurrent failOnVersionConflict() { @@ -20,10 +20,6 @@ class ResolutionStrategyWithCurrent { return this } - void preferProjectModules() { - delegate.preferProjectModules() - } - ResolutionStrategyWithCurrent activateDependencyLocking() { delegate.activateDependencyLocking() return this @@ -39,31 +35,11 @@ class ResolutionStrategyWithCurrent { return this } - Set getForcedModules() { - return delegate.getForcedModules() - } - ResolutionStrategyWithCurrent eachDependency(Action rule) { delegate.eachDependency(rule) return this } - void cacheDynamicVersionsFor(int value, String units) { - delegate.cacheDynamicVersionsFor(value, units) - } - - void cacheDynamicVersionsFor(int value, TimeUnit units) { - delegate.cacheDynamicVersionsFor(value, units) - } - - void cacheChangingModulesFor(int value, String units) { - delegate.cacheChangingModulesFor(value, units) - } - - void cacheChangingModulesFor(int value, TimeUnit units) { - delegate.cacheChangingModulesFor(value, units) - } - ComponentSelectionRulesWithCurrent getComponentSelection() { return new ComponentSelectionRulesWithCurrent(delegate.getComponentSelection(), currentCoordinates) @@ -85,17 +61,9 @@ class ResolutionStrategyWithCurrent { }) } - DependencySubstitutions getDependencySubstitution() { - return delegate.getDependencySubstitution() - } - ResolutionStrategyWithCurrent dependencySubstitution( Action action) { delegate.dependencySubstitution(action) return this } - - void sortArtifacts(SortOrder sortOrder) { - delegate.sortArtifacts(sortOrder) - } } From e3f310ec3c8fd98305f2bfdf2067ff80979fbe2d Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 11:03:40 +0200 Subject: [PATCH 15/16] Fix codenarc warnings --- .../gradle/versions/updates/DependencyUpdatesTask.groovy | 8 ++++---- .../ComponentSelectionRulesWithCurrent.groovy | 8 ++++---- .../ComponentSelectionWithCurrent.groovy | 4 ++-- .../versions/ComponentSelectionRuleSourceSpec.groovy | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy index 219dadc5..66551056 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/DependencyUpdatesTask.groovy @@ -52,8 +52,8 @@ class DependencyUpdatesTask extends DefaultTask { @Input boolean checkForGradleUpdate = true - Object outputFormatter = 'plain'; - Action resolutionStrategy = null; + Object outputFormatter = 'plain' + Action resolutionStrategyAction = null DependencyUpdatesTask() { description = 'Displays the dependency updates for the project.' @@ -66,7 +66,7 @@ class DependencyUpdatesTask extends DefaultTask { def dependencyUpdates() { project.evaluationDependsOnChildren() - def evaluator = new DependencyUpdates(project, resolutionStrategy, revisionLevel(), + def evaluator = new DependencyUpdates(project, resolutionStrategyAction, revisionLevel(), outputFormatterProp(), outputDirectory(), getReportfileName(), checkForGradleUpdate, gradleReleaseChannelLevel()) DependencyUpdatesReporter reporter = evaluator.run() reporter?.write() @@ -77,7 +77,7 @@ class DependencyUpdatesTask extends DefaultTask { * @param resolutionStrategy the resolution strategy */ void resolutionStrategy(final Action resolutionStrategy) { - this.resolutionStrategy = resolutionStrategy + this.resolutionStrategyAction = resolutionStrategy } /** Returns the resolution revision level. */ diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy index 17395516..097b5180 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionRulesWithCurrent.groovy @@ -38,7 +38,7 @@ class ComponentSelectionRulesWithCurrent { ComponentSelectionRulesWithCurrent all(Object ruleSource) { RuleAction ruleAction = RuleSourceBackedRuleAction.create( - ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) + ModelType.of(ComponentSelectionWithCurrent), ruleSource) delegate.all(new Action() { void execute(ComponentSelection inner) { ruleAction.execute(wrapComponentSelection(inner), []) @@ -70,7 +70,7 @@ class ComponentSelectionRulesWithCurrent { ComponentSelectionRulesWithCurrent withModule(Object id, Object ruleSource) { RuleAction ruleAction = RuleSourceBackedRuleAction.create( - ModelType.of(ComponentSelectionWithCurrent.class), ruleSource) + ModelType.of(ComponentSelectionWithCurrent), ruleSource) delegate.withModule(id, new Action() { void execute(ComponentSelection inner) { ruleAction.execute(wrapComponentSelection(inner), []) @@ -82,8 +82,8 @@ class ComponentSelectionRulesWithCurrent { private ComponentSelectionWithCurrent wrapComponentSelection(ComponentSelection inner) { Coordinate candidateCoordinate = Coordinate.from(inner.candidate) Coordinate current = currentCoordinates.get(candidateCoordinate.key) - ComponentSelectionWithCurrent wrapped = new ComponentSelectionWithCurrent(inner, - current.version) + ComponentSelectionWithCurrent wrapped = new ComponentSelectionWithCurrent(current.version, + inner) return wrapped } } diff --git a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy index 1d84a7f6..61bb201b 100644 --- a/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy +++ b/src/main/groovy/com/github/benmanes/gradle/versions/updates/resolutionstrategy/ComponentSelectionWithCurrent.groovy @@ -6,8 +6,8 @@ import org.gradle.api.artifacts.ComponentSelection @TupleConstructor(includeFields=true) class ComponentSelectionWithCurrent { + final String currentVersion + @Delegate private final ComponentSelection delegate - - public final String currentVersion } diff --git a/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy b/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy index db31c374..fbfb93dd 100644 --- a/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy +++ b/src/test/groovy/com/github/benmanes/gradle/versions/ComponentSelectionRuleSourceSpec.groovy @@ -13,10 +13,10 @@ final class ComponentSelectionRuleSourceSpec extends Specification { private List pluginClasspath def 'setup'() { - def pluginClasspathResource = getClass().classLoader.findResource("plugin-classpath.txt") + def pluginClasspathResource = getClass().classLoader.findResource('plugin-classpath.txt') if (pluginClasspathResource == null) { throw new IllegalStateException( - "Did not find plugin classpath resource, run `testClasses` build task.") + 'Did not find plugin classpath resource, run `testClasses` build task.') } pluginClasspath = pluginClasspathResource.readLines().collect { new File(it) } @@ -28,7 +28,7 @@ final class ComponentSelectionRuleSourceSpec extends Specification { def classpathString = pluginClasspath .collect { it.absolutePath.replace('\\', '\\\\') } // escape backslashes in Windows paths .collect { "'$it'" } - .join(", ") + .join(', ') def mavenRepoUrl = getClass().getResource('/maven/').toURI() def srdErrWriter = new StringWriter() From c0b850601d74fd961762e6c8a80be980608714da Mon Sep 17 00:00:00 2001 From: Frantisek Nagy Date: Wed, 28 Aug 2019 12:18:30 +0200 Subject: [PATCH 16/16] Update readme --- README.md | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c800498f..01510c2b 100644 --- a/README.md +++ b/README.md @@ -86,17 +86,22 @@ The strategy can be specified either on the task or as a system property for ad gradle dependencyUpdates -Drevision=release ``` -The latest versions can be further filtered using [Component Selection Rules][component_selection_rules]. -For example, to disallow release candidates as upgradable versions a selection rule could be defined as: +The latest versions can be further filtered using [Component Selection Rules][component_selection_rules]. +The current version of a component can be retrieved with `currentVersion` property. For example, to +disallow release candidates as upgradable versions from stable versions, a selection rule could be +defined as: ```groovy dependencyUpdates.resolutionStrategy { componentSelection { rules -> - rules.all { ComponentSelection selection -> - boolean rejected = ['alpha', 'beta', 'rc', 'cr', 'm', 'preview', 'b', 'ea'].any { qualifier -> - selection.candidate.version ==~ /(?i).*[.-]$qualifier[.\d-+]*/ + rules.all { + def isNonStable = { String version -> + ['alpha', 'beta', 'rc', 'cr', 'm', 'preview', 'b', 'ea'].any { qualifier -> + version ==~ /(?i).*[.-]\$qualifier[.\\d-+]*/ + } } - if (rejected) { + + if (isNonStable(candidate.version) && !isNonStable(currentVersion)) { selection.reject('Release candidate') } } @@ -113,10 +118,10 @@ tasks.named("dependencyUpdates") { resolutionStrategy { componentSelection { all { - val rejected = listOf("alpha", "beta", "rc", "cr", "m", "preview", "b", "ea").any { qualifier -> - candidate.version.matches(Regex("(?i).*[.-]$qualifier[.\\d-+]*")) + fun isNonStable(version: String) = listOf("alpha", "beta", "rc", "cr", "m", "preview", "b", "ea").any { qualifier -> + version.matches(Regex("(?i).*[.-]\$qualifier[.\\d-+]*")) } - if (rejected) { + if (isNonStable(candidate.version) && !isNonStable(currentVersion)) { reject("Release candidate") } }