From c4ef5eb0c6a366204dd465f7074177ae379841ef Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 24 May 2017 22:57:34 -0700 Subject: [PATCH 1/2] Build: Add back explicit exclusions and remove gradle exclusions When transitive dependencies are disable for a dependency, gradle adds a wildcard exclusion to the generated pom. However, some external tools like ivy have bugs with wildcards. This commit adds back the explicit generation of transitive excludes, and removes the gradle generated exclusions element from the pom. closes #24490 --- .../elasticsearch/gradle/BuildPlugin.groovy | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 64ebba7578c4f..eda9410616405 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -311,9 +311,16 @@ class BuildPlugin implements Plugin { /** * Returns a closure which can be used with a MavenPom for fixing problems with gradle generated poms. * - * The current fixup is to set compile time deps back to compile from runtime (known issue with maven-publish plugin). + * */ private static Closure fixupDependencies(Project project) { + // TODO: revisit this when upgrading to Gradle 2.14+, see Javadoc comment above return { XmlProvider xml -> // first find if we have dependencies at all, and grab the node NodeList depsNodes = xml.asNode().get('dependencies') @@ -321,15 +328,53 @@ class BuildPlugin implements Plugin { return } - // 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 + // check each dependency for any transitive deps for (Node depNode : depsNodes.get(0).children()) { + String groupId = depNode.get('groupId').get(0).text() + 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' } + + // remove any exclusions added by gradle, they contain wildcards and systems like ivy have bugs with wildcards + // see https://github.com/elastic/elasticsearch/issues/24490 + NodeList exclusionsNode = depNode.get('exclusions') + if (exclusionsNode.size() > 0) { + depNode.remove(exclusionsNode.get(0)) + } + + // collect the transitive deps now that we know what this dependency is + String depConfig = transitiveDepConfigName(groupId, artifactId, version) + Configuration configuration = project.configurations.findByName(depConfig) + if (configuration == null) { + continue // we did not make this dep non-transitive + } + Set artifacts = configuration.resolvedConfiguration.resolvedArtifacts + if (artifacts.size() <= 1) { + // this dep has no transitive deps (or the only artifact is itself) + continue + } + + // we now know we have something to exclude, so add exclusions for all artifacts except the main one + Node exclusions = depNode.appendNode('exclusions') + for (ResolvedArtifact artifact : artifacts) { + ModuleVersionIdentifier moduleVersionIdentifier = artifact.moduleVersion.id; + String depGroupId = moduleVersionIdentifier.group + String depArtifactId = moduleVersionIdentifier.name + // add exclusions for all artifacts except the main one + if (depGroupId != groupId || depArtifactId != artifactId) { + Node exclusion = exclusions.appendNode('exclusion') + exclusion.appendNode('groupId', depGroupId) + exclusion.appendNode('artifactId', depArtifactId) + } + } } } } From 59c66e9d4a9f8419f7f8dfbe6e15fc5f83a6dcd2 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 25 May 2017 12:21:38 -0700 Subject: [PATCH 2/2] remove outdated comment --- .../src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index eda9410616405..87d5ec9ae5336 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -320,7 +320,6 @@ class BuildPlugin implements Plugin { * */ private static Closure fixupDependencies(Project project) { - // TODO: revisit this when upgrading to Gradle 2.14+, see Javadoc comment above return { XmlProvider xml -> // first find if we have dependencies at all, and grab the node NodeList depsNodes = xml.asNode().get('dependencies')