Skip to content

Commit 8d2770c

Browse files
authored
Merge pull request #19876 from rjernst/gradle_pom_runtime_deps
Build: Fix compile time deps in poms and simplify transitive exclusions
2 parents 0996ae0 + 576aaac commit 8d2770c

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class BuildPlugin implements Plugin<Project> {
270270

271271
// add exclusions to the pom directly, for each of the transitive deps of this project's deps
272272
project.modifyPom { MavenPom pom ->
273-
pom.withXml(removeTransitiveDependencies(project))
273+
pom.withXml(fixupDependencies(project))
274274
}
275275
}
276276

@@ -299,9 +299,16 @@ class BuildPlugin implements Plugin<Project> {
299299
}
300300
}
301301

302-
/** Returns a closure which can be used with a MavenPom for removing transitive dependencies. */
303-
private static Closure removeTransitiveDependencies(Project project) {
304-
// TODO: remove this when enforcing gradle 2.13+, it now properly handles exclusions
302+
/**
303+
* Returns a closure which can be used with a MavenPom for fixing problems with gradle generated poms.
304+
*
305+
* <ul>
306+
* <li>Remove transitive dependencies (using wildcard exclusions, fixed in gradle 2.14)</li>
307+
* <li>Set compile time deps back to compile from runtime (known issue with maven-publish plugin)
308+
* </ul>
309+
*/
310+
private static Closure fixupDependencies(Project project) {
311+
// TODO: remove this when enforcing gradle 2.14+, it now properly handles exclusions
305312
return { XmlProvider xml ->
306313
// first find if we have dependencies at all, and grab the node
307314
NodeList depsNodes = xml.asNode().get('dependencies')
@@ -315,6 +322,15 @@ class BuildPlugin implements Plugin<Project> {
315322
String artifactId = depNode.get('artifactId').get(0).text()
316323
String version = depNode.get('version').get(0).text()
317324

325+
// fix deps incorrectly marked as runtime back to compile time deps
326+
// see https://discuss.gradle.org/t/maven-publish-plugin-generated-pom-making-dependency-scope-runtime/7494/4
327+
boolean isCompileDep = project.configurations.compile.allDependencies.find { dep ->
328+
dep.name == depNode.artifactId.text()
329+
}
330+
if (depNode.scope.text() == 'runtime' && isCompileDep) {
331+
depNode.scope*.value = 'compile'
332+
}
333+
318334
// collect the transitive deps now that we know what this dependency is
319335
String depConfig = transitiveDepConfigName(groupId, artifactId, version)
320336
Configuration configuration = project.configurations.findByName(depConfig)
@@ -327,17 +343,10 @@ class BuildPlugin implements Plugin<Project> {
327343
continue
328344
}
329345

330-
// we now know we have something to exclude, so add the exclusion elements
331-
Node exclusions = depNode.appendNode('exclusions')
332-
for (ResolvedArtifact transitiveArtifact : artifacts) {
333-
ModuleVersionIdentifier transitiveDep = transitiveArtifact.moduleVersion.id
334-
if (transitiveDep.group == groupId && transitiveDep.name == artifactId) {
335-
continue; // don't exclude the dependency itself!
336-
}
337-
Node exclusion = exclusions.appendNode('exclusion')
338-
exclusion.appendNode('groupId', transitiveDep.group)
339-
exclusion.appendNode('artifactId', transitiveDep.name)
340-
}
346+
// we now know we have something to exclude, so add a wildcard exclusion element
347+
Node exclusion = depNode.appendNode('exclusions').appendNode('exclusion')
348+
exclusion.appendNode('groupId', '*')
349+
exclusion.appendNode('artifactId', '*')
341350
}
342351
}
343352
}
@@ -349,7 +358,7 @@ class BuildPlugin implements Plugin<Project> {
349358
publications {
350359
all { MavenPublication publication -> // we only deal with maven
351360
// add exclusions to the pom directly, for each of the transitive deps of this project's deps
352-
publication.pom.withXml(removeTransitiveDependencies(project))
361+
publication.pom.withXml(fixupDependencies(project))
353362
}
354363
}
355364
}

0 commit comments

Comments
 (0)