From 576aaac9767071f768c4735801c23903c6d22be5 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 8 Aug 2016 11:50:35 -0700 Subject: [PATCH] Build: Fix compile time deps in poms and simplify transitive exclusions This change works around a known issue with using the maven-publish gralde plugin. All deps are marked in the generated pom as runtime. With this change, they are set back to compile time. This also simplified the transitive dependencies exclusion to work the same as how it was fixed in gradle 2.14 (wildcard exclusions). closes #19835 --- .../elasticsearch/gradle/BuildPlugin.groovy | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index f825819cb3e..48c099074de 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -270,7 +270,7 @@ class BuildPlugin implements Plugin { // add exclusions to the pom directly, for each of the transitive deps of this project's deps project.modifyPom { MavenPom pom -> - pom.withXml(removeTransitiveDependencies(project)) + pom.withXml(fixupDependencies(project)) } } @@ -299,9 +299,16 @@ class BuildPlugin implements Plugin { } } - /** Returns a closure which can be used with a MavenPom for removing transitive dependencies. */ - private static Closure removeTransitiveDependencies(Project project) { - // TODO: remove this when enforcing gradle 2.13+, it now properly handles exclusions + /** + * Returns a closure which can be used with a MavenPom for fixing problems with gradle generated poms. + * + *
    + *
  • Remove transitive dependencies (using wildcard exclusions, fixed in gradle 2.14)
  • + *
  • Set compile time deps back to compile from runtime (known issue with maven-publish plugin) + *
+ */ + private static Closure fixupDependencies(Project project) { + // TODO: remove this when enforcing gradle 2.14+, it now properly handles exclusions return { XmlProvider xml -> // first find if we have dependencies at all, and grab the node NodeList depsNodes = xml.asNode().get('dependencies') @@ -315,6 +322,15 @@ class BuildPlugin implements Plugin { String artifactId = depNode.get('artifactId').get(0).text() String version = depNode.get('version').get(0).text() + // fix deps incorrectly marked as runtime back to compile time deps + // see https://discuss.gradle.org/t/maven-publish-plugin-generated-pom-making-dependency-scope-runtime/7494/4 + boolean isCompileDep = project.configurations.compile.allDependencies.find { dep -> + dep.name == depNode.artifactId.text() + } + if (depNode.scope.text() == 'runtime' && isCompileDep) { + depNode.scope*.value = 'compile' + } + // collect the transitive deps now that we know what this dependency is String depConfig = transitiveDepConfigName(groupId, artifactId, version) Configuration configuration = project.configurations.findByName(depConfig) @@ -327,17 +343,10 @@ class BuildPlugin implements Plugin { continue } - // we now know we have something to exclude, so add the exclusion elements - Node exclusions = depNode.appendNode('exclusions') - for (ResolvedArtifact transitiveArtifact : artifacts) { - ModuleVersionIdentifier transitiveDep = transitiveArtifact.moduleVersion.id - if (transitiveDep.group == groupId && transitiveDep.name == artifactId) { - continue; // don't exclude the dependency itself! - } - Node exclusion = exclusions.appendNode('exclusion') - exclusion.appendNode('groupId', transitiveDep.group) - exclusion.appendNode('artifactId', transitiveDep.name) - } + // we now know we have something to exclude, so add a wildcard exclusion element + Node exclusion = depNode.appendNode('exclusions').appendNode('exclusion') + exclusion.appendNode('groupId', '*') + exclusion.appendNode('artifactId', '*') } } } @@ -349,7 +358,7 @@ class BuildPlugin implements Plugin { publications { all { MavenPublication publication -> // we only deal with maven // add exclusions to the pom directly, for each of the transitive deps of this project's deps - publication.pom.withXml(removeTransitiveDependencies(project)) + publication.pom.withXml(fixupDependencies(project)) } } }