Skip to content

Commit

Permalink
Introduce ResolvedDependencySelector to use resolved versions for gen…
Browse files Browse the repository at this point in the history
…erated POM
  • Loading branch information
rpalcolea committed May 2, 2019
1 parent c8140c4 commit edd0a10
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.api.XmlProvider
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.DependencySet
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.Property
Expand Down Expand Up @@ -119,10 +120,15 @@ class JpiPomCustomizer {
Node dependenciesNode = pom.dependencies[0] as Node
dependenciesNode = dependenciesNode ?: pom.appendNode('dependencies')
(compileDependencies - coreDependencies + pluginDependencies + optionalPluginDependencies).each {
ModuleVersionIdentifier mvid = ResolvedDependencySelector.selectedModuleVersion(
project,
'compile',
it.group,
it.name)
Node dependency = dependenciesNode.appendNode('dependency')
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', it.version)
dependency.appendNode('version', mvid.version ?: it.version)
}
dependenciesNode.each { Node dependency ->
String groupId = dependency.groupId.text()
Expand Down Expand Up @@ -161,9 +167,14 @@ class JpiPomCustomizer {

coreDependencies.each {
Node dependency = dependenciesNode.appendNode('dependency')
ModuleVersionIdentifier mvid = ResolvedDependencySelector.selectedModuleVersion(
project,
'compile',
it.group,
it.name)
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', it.version)
dependency.appendNode('version', mvid.version ?: it.version)
dependency.appendNode('scope', 'provided')
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.jenkinsci.gradle.plugins.jpi

import groovy.transform.Memoized
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.component.ModuleComponentSelector
import org.gradle.api.artifacts.result.ResolvedDependencyResult

class ResolvedDependencySelector {
static ModuleVersionIdentifier selectedModuleVersion(Project project, String scope, String group, String name) {
ResolvedDependencyResult result = lookupDependency(project, scope, group, name)

Map<String, String> scoping = [compile: 'runtime', provided: 'compileOnly']

if (!result && scoping[scope]) {
result = lookupDependency(project, scoping[scope], group, name)
}

result?.selected?.moduleVersion
}

private static ResolvedDependencyResult lookupDependency(Project project, String scope, String group, String name) {
def resolvedDependencies = resolvedDependencyByConfiguration(project)[scope]
def result = resolvedDependencies.find { r ->
def requested = r.requested
(requested instanceof ModuleComponentSelector) &&
(requested.group == group) &&
(requested.module == name)
}
result
}

@Memoized
static Map<String, Set<? extends ResolvedDependencyResult>> resolvedDependencyByConfiguration(Project project) {
ConfigurationContainer configurations = project.configurations
Map<String, Set<? extends ResolvedDependencyResult>> dependencyMap = [:]
dependencyMap['runtime'] = resolvedDependencies(configurations.runtimeClasspath)
dependencyMap['compile'] = resolvedDependencies(configurations.compileClasspath)
dependencyMap['compileOnly'] = resolvedDependencies(configurations.compileOnly)
dependencyMap['test'] = resolvedDependencies(configurations.testRuntimeClasspath)
dependencyMap
}

static Set<? extends ResolvedDependencyResult> resolvedDependencies(Configuration configuration) {
configuration.incoming.resolutionResult.allDependencies.findAll {
it instanceof ResolvedDependencyResult
} as Set<? extends ResolvedDependencyResult>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,42 @@ class JpiPomCustomizerIntegrationSpec extends IntegrationSpec {
compareXml('plugin-dependencies-pom.xml', actualPomIn(projectDir))
}

def 'plugin with dynamic dependency - 1.9.+'() {
setup:
build << """\
jenkinsPlugin {
coreVersion = '1.580.1'
}
dependencies {
jenkinsPlugins 'org.jenkins-ci.plugins:credentials:1.9.+'
}
""".stripIndent()

when:
generatePom()

then:
compareXml('plugin-dependencies-pom.xml', actualPomIn(projectDir))
}

def 'plugin with dynamic dependency - latest.release'() {
setup:
build << """\
jenkinsPlugin {
coreVersion = '1.580.1'
}
dependencies {
jenkinsPlugins 'org.jenkins-ci.plugins:credentials:latest.release'
}
""".stripIndent()

when:
generatePom()

then:
!actualPomIn(projectDir).text.contains('latest.release')
}

def 'optional plugin dependencies'() {
setup:
build << """\
Expand Down Expand Up @@ -292,7 +328,7 @@ class JpiPomCustomizerIntegrationSpec extends IntegrationSpec {

void generatePom() {
gradleRunner()
.withArguments('generatePomFileForMavenJpiPublication')
.withArguments('generatePomFileForMavenJpiPublication')
.build()
}

Expand Down

0 comments on commit edd0a10

Please sign in to comment.