diff --git a/src/main/groovy/org/jenkinsci/gradle/plugins/jpi/JpiExtension.groovy b/src/main/groovy/org/jenkinsci/gradle/plugins/jpi/JpiExtension.groovy index 4eed48c3..36c38b5a 100644 --- a/src/main/groovy/org/jenkinsci/gradle/plugins/jpi/JpiExtension.groovy +++ b/src/main/groovy/org/jenkinsci/gradle/plugins/jpi/JpiExtension.groovy @@ -24,6 +24,7 @@ import org.gradle.api.provider.Property import org.gradle.api.tasks.Delete import org.gradle.api.tasks.SourceSet import org.gradle.util.ConfigureUtil +import org.jenkinsci.gradle.plugins.jpi.internal.DependencyLookup /** * This gets exposed to the project as 'jpi' to offer additional convenience methods. @@ -128,39 +129,11 @@ class JpiExtension { void setCoreVersion(String v) { jenkinsVersion.convention(v) this.coreVersion = v - def uiSamplesVersion = v - def testHarnessVersion = v - def servletApiArtifact = 'servlet-api' - def servletApiVersion = '2.4' - def findBugsGroup = 'findbugs' - def findBugsVersion = '1.0.0' if (new VersionNumber(this.coreVersion) <= new VersionNumber('1.419.99')) { throw new GradleException('The gradle-jpi-plugin requires Jenkins 1.420 or later') } - if (new VersionNumber(this.coreVersion) >= new VersionNumber('1.533')) { - uiSamplesVersion = '2.0' - } - - if (new VersionNumber(this.coreVersion) >= new VersionNumber('1.618')) { - findBugsGroup = 'com.google.code.findbugs' - findBugsVersion = '3.0.0' - } - - if (new VersionNumber(this.coreVersion) > new VersionNumber('1.644')) { - testHarnessVersion = '2.0' - } - - if (new VersionNumber(this.coreVersion) >= new VersionNumber('2.0')) { - servletApiArtifact = 'javax.servlet-api' - servletApiVersion = '3.1.0' - } - - if (new VersionNumber(this.coreVersion) >= new VersionNumber('2.64')) { - testHarnessVersion = '2.60' - } - // workarounds for JENKINS-26331 if (new VersionNumber(this.coreVersion) >= new VersionNumber('1.545') && new VersionNumber(this.coreVersion) < new VersionNumber('1.592')) { @@ -175,22 +148,10 @@ class JpiExtension { if (this.coreVersion) { jenkinsWarCoordinates = [group: 'org.jenkins-ci.main', name: 'jenkins-war', version: v] - project.dependencies { - testRuntimeOnly(jenkinsWarCoordinates) - - annotationProcessor "org.jenkins-ci.main:jenkins-core:$v" - - compileOnly( - [group: 'org.jenkins-ci.main', name: 'jenkins-core', version: v], - [group: findBugsGroup, name: 'annotations', version: findBugsVersion], - [group: 'javax.servlet', name: servletApiArtifact, version: servletApiVersion], - ) - - testImplementation("org.jenkins-ci.main:jenkins-core:$v") - testImplementation("org.jenkins-ci.main:jenkins-test-harness:${testHarnessVersion}") - testImplementation("org.jenkins-ci.main:ui-samples-plugin:${uiSamplesVersion}") - if (new VersionNumber(this.coreVersion) < new VersionNumber('1.505')) { - testImplementation('junit:junit-dep:4.10') + def lookup = new DependencyLookup() + for (String config : lookup.configurations()) { + lookup.find(config, this.coreVersion).each { + project.dependencies.add(config, it) } } } diff --git a/src/main/groovy/org/jenkinsci/gradle/plugins/jpi/internal/DependencyLookup.groovy b/src/main/groovy/org/jenkinsci/gradle/plugins/jpi/internal/DependencyLookup.groovy new file mode 100644 index 00000000..17b3aaa6 --- /dev/null +++ b/src/main/groovy/org/jenkinsci/gradle/plugins/jpi/internal/DependencyLookup.groovy @@ -0,0 +1,57 @@ +package org.jenkinsci.gradle.plugins.jpi.internal + +import groovy.transform.CompileStatic +import org.gradle.util.GradleVersion + +@CompileStatic +class DependencyLookup { + Set find(String configuration, String jenkinsVersion) { + String core = "org.jenkins-ci.main:jenkins-core:${jenkinsVersion}" + String war = "org.jenkins-ci.main:jenkins-war:${jenkinsVersion}" + String findbugs = 'com.google.code.findbugs:annotations:3.0.0' + def version = GradleVersion.version(jenkinsVersion) + if (version < GradleVersion.version('1.618')) { + findbugs = 'findbugs:annotations:1.0.0' + } + String servlet = 'javax.servlet:javax.servlet-api:3.1.0' + if (version < GradleVersion.version('2.0')) { + servlet = 'javax.servlet:servlet-api:2.4' + } + String testHarness = 'org.jenkins-ci.main:jenkins-test-harness:2.60' + if (version < GradleVersion.version('2.64')) { + testHarness = 'org.jenkins-ci.main:jenkins-test-harness:2.0' + } + if (version <= GradleVersion.version('1.644')) { + testHarness = "org.jenkins-ci.main:jenkins-test-harness:${jenkinsVersion}" + } + String uiSamples = 'org.jenkins-ci.main:ui-samples-plugin:2.0' + if (version < GradleVersion.version('1.533')) { + uiSamples = "org.jenkins-ci.main:ui-samples-plugin:${jenkinsVersion}" + } + switch (configuration) { + case 'annotationProcessor': + return [core] as Set + case 'compileOnly': + return [core, findbugs, servlet] as Set + case 'testImplementation': + Set deps = [core, testHarness, uiSamples] as Set + if (version < GradleVersion.version('1.505')) { + deps.add('junit:junit-dep:4.10') + } + return deps + case 'testRuntimeOnly': + return [war] as Set + default: + [] as Set + } + } + + Set configurations() { + [ + 'annotationProcessor', + 'compileOnly', + 'testImplementation', + 'testRuntimeOnly', + ] as Set + } +} diff --git a/src/test/groovy/org/jenkinsci/gradle/plugins/jpi/internal/DependencyLookupSpec.groovy b/src/test/groovy/org/jenkinsci/gradle/plugins/jpi/internal/DependencyLookupSpec.groovy new file mode 100644 index 00000000..e035b92e --- /dev/null +++ b/src/test/groovy/org/jenkinsci/gradle/plugins/jpi/internal/DependencyLookupSpec.groovy @@ -0,0 +1,70 @@ +package org.jenkinsci.gradle.plugins.jpi.internal + +import spock.lang.Specification +import spock.lang.Unroll + +class DependencyLookupSpec extends Specification { + def 'should get annotationProcessor dependencies by version'() { + given: + DependencyLookup lookup = new DependencyLookup() + + when: + def actual = lookup.find('annotationProcessor', '2.0') + + then: + actual == [ + 'org.jenkins-ci.main:jenkins-core:2.0', + ] as Set + } + + @Unroll + def 'should get compileOnly dependencies for #version'(String version, Set expected) { + given: + DependencyLookup lookup = new DependencyLookup() + + when: + def actual = lookup.find('compileOnly', version) + + then: + actual == expected + + where: + version | expected + '1.617' | ['org.jenkins-ci.main:jenkins-core:1.617', 'findbugs:annotations:1.0.0', 'javax.servlet:servlet-api:2.4'] as Set + '1.618' | ['org.jenkins-ci.main:jenkins-core:1.618', 'com.google.code.findbugs:annotations:3.0.0', 'javax.servlet:servlet-api:2.4'] as Set + '2.0' | ['org.jenkins-ci.main:jenkins-core:2.0', 'com.google.code.findbugs:annotations:3.0.0', 'javax.servlet:javax.servlet-api:3.1.0'] as Set + } + + @Unroll + def 'should get testImplementation dependencies for #version'(String version, Set expected) { + given: + DependencyLookup lookup = new DependencyLookup() + + when: + def actual = lookup.find('testImplementation', version) + + then: + actual == expected + + where: + version | expected + '1.504' | ['org.jenkins-ci.main:jenkins-core:1.504', 'org.jenkins-ci.main:jenkins-test-harness:1.504', 'org.jenkins-ci.main:ui-samples-plugin:1.504', 'junit:junit-dep:4.10'] as Set + '1.532' | ['org.jenkins-ci.main:jenkins-core:1.532', 'org.jenkins-ci.main:jenkins-test-harness:1.532', 'org.jenkins-ci.main:ui-samples-plugin:1.532'] as Set + '1.644' | ['org.jenkins-ci.main:jenkins-core:1.644', 'org.jenkins-ci.main:jenkins-test-harness:1.644', 'org.jenkins-ci.main:ui-samples-plugin:2.0'] as Set + '1.645' | ['org.jenkins-ci.main:jenkins-core:1.645', 'org.jenkins-ci.main:jenkins-test-harness:2.0', 'org.jenkins-ci.main:ui-samples-plugin:2.0'] as Set + '2.64' | ['org.jenkins-ci.main:jenkins-core:2.64', 'org.jenkins-ci.main:jenkins-test-harness:2.60', 'org.jenkins-ci.main:ui-samples-plugin:2.0'] as Set + } + + def 'should get testRuntimeOnly dependencies for version'() { + given: + DependencyLookup lookup = new DependencyLookup() + + when: + def actual = lookup.find('testRuntimeOnly', '2.222.3') + + then: + actual == [ + 'org.jenkins-ci.main:jenkins-war:2.222.3', + ] as Set + } +}