1919package org.elasticsearch.gradle
2020
2121import com.carrotsearch.gradle.junit4.RandomizedTestingTask
22+ import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
2223import org.apache.tools.ant.taskdefs.condition.Os
2324import org.eclipse.jgit.lib.Constants
2425import org.eclipse.jgit.lib.RepositoryBuilder
@@ -36,12 +37,14 @@ import org.gradle.api.artifacts.ModuleDependency
3637import org.gradle.api.artifacts.ModuleVersionIdentifier
3738import org.gradle.api.artifacts.ProjectDependency
3839import org.gradle.api.artifacts.ResolvedArtifact
40+ import org.gradle.api.artifacts.SelfResolvingDependency
3941import org.gradle.api.artifacts.dsl.RepositoryHandler
4042import org.gradle.api.execution.TaskExecutionGraph
4143import org.gradle.api.plugins.JavaPlugin
4244import org.gradle.api.publish.maven.MavenPublication
4345import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
4446import org.gradle.api.publish.maven.tasks.GenerateMavenPom
47+ import org.gradle.api.tasks.SourceSet
4548import org.gradle.api.tasks.bundling.Jar
4649import org.gradle.api.tasks.compile.GroovyCompile
4750import org.gradle.api.tasks.compile.JavaCompile
@@ -498,7 +501,41 @@ class BuildPlugin implements Plugin<Project> {
498501 }
499502 }
500503 }
504+ project. plugins. withType(ShadowPlugin ). whenPluginAdded {
505+ project. publishing {
506+ publications {
507+ nebula(MavenPublication ) {
508+ artifact project. tasks. shadowJar
509+ artifactId = project. archivesBaseName
510+ /*
511+ * Configure the pom to include the "shadow" as compile dependencies
512+ * because that is how we're using them but remove all other dependencies
513+ * because they've been shaded into the jar.
514+ */
515+ pom. withXml { XmlProvider xml ->
516+ Node root = xml. asNode()
517+ root. remove(root. dependencies)
518+ Node dependenciesNode = root. appendNode(' dependencies' )
519+ project. configurations. shadow. allDependencies. each {
520+ if (false == it instanceof SelfResolvingDependency ) {
521+ Node dependencyNode = dependenciesNode. appendNode(' dependency' )
522+ dependencyNode. appendNode(' groupId' , it. group)
523+ dependencyNode. appendNode(' artifactId' , it. name)
524+ dependencyNode. appendNode(' version' , it. version)
525+ dependencyNode. appendNode(' scope' , ' compile' )
526+ }
527+ }
528+ // Be tidy and remove the element if it is empty
529+ if (dependenciesNode. children. empty) {
530+ root. remove(dependenciesNode)
531+ }
532+ }
533+ }
534+ }
535+ }
536+ }
501537 }
538+
502539 }
503540
504541 /* * Adds compiler settings to the project */
@@ -660,6 +697,28 @@ class BuildPlugin implements Plugin<Project> {
660697 }
661698 }
662699 }
700+ project. plugins. withType(ShadowPlugin ). whenPluginAdded {
701+ /*
702+ * When we use the shadow plugin we entirely replace the
703+ * normal jar with the shadow jar so we no longer want to run
704+ * the jar task.
705+ */
706+ project. tasks. jar. enabled = false
707+ project. tasks. shadowJar {
708+ /*
709+ * Replace the default "shadow" classifier with null
710+ * which will leave the classifier off of the file name.
711+ */
712+ classifier = null
713+ /*
714+ * Not all cases need service files merged but it is
715+ * better to be safe
716+ */
717+ mergeServiceFiles()
718+ }
719+ // Make sure we assemble the shadow jar
720+ project. tasks. assemble. dependsOn project. tasks. shadowJar
721+ }
663722 }
664723
665724 /* * Returns a closure of common configuration shared by unit and integration tests. */
@@ -744,6 +803,18 @@ class BuildPlugin implements Plugin<Project> {
744803 }
745804
746805 exclude ' **/*$*.class'
806+
807+ project. plugins. withType(ShadowPlugin ). whenPluginAdded {
808+ /*
809+ * If we make a shaded jar we test against it.
810+ */
811+ classpath - = project. tasks. compileJava. outputs. files
812+ classpath - = project. configurations. compile
813+ classpath - = project. configurations. runtime
814+ classpath + = project. configurations. shadow
815+ classpath + = project. tasks. shadowJar. outputs. files
816+ dependsOn project. tasks. shadowJar
817+ }
747818 }
748819 }
749820
@@ -766,7 +837,26 @@ class BuildPlugin implements Plugin<Project> {
766837 additionalTest. dependsOn(project. tasks. testClasses)
767838 test. dependsOn(additionalTest)
768839 });
769- return test
840+
841+ project. plugins. withType(ShadowPlugin ). whenPluginAdded {
842+ /*
843+ * We need somewhere to configure dependencies that we don't wish
844+ * to shade into the jar. The shadow plugin creates a "shadow"
845+ * configuration which is *almost* exactly that. It is never
846+ * bundled into the shaded jar but is used for main source
847+ * compilation. Unfortunately, by default it is not used for
848+ * *test* source compilation and isn't used in tests at all. This
849+ * change makes it available for test compilation.
850+ *
851+ * Note that this isn't going to work properly with qa projects
852+ * but they have no business applying the shadow plugin in the
853+ * firstplace.
854+ */
855+ SourceSet testSourceSet = project. sourceSets. findByName(' test' )
856+ if (testSourceSet != null ) {
857+ testSourceSet. compileClasspath + = project. configurations. shadow
858+ }
859+ }
770860 }
771861
772862 private static configurePrecommit (Project project ) {
0 commit comments