Skip to content

Commit de41041

Browse files
authored
Shadowed dependencies should be hidden from pom dependencies (#73467) (#73487)
1 parent e95bd59 commit de41041

File tree

2 files changed

+131
-25
lines changed

2 files changed

+131
-25
lines changed

buildSrc/src/integTest/groovy/org/elasticsearch/gradle/PublishPluginFuncTest.groovy

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,127 @@ class PublishPluginFuncTest extends AbstractGradleFuncTest {
5151
)
5252
}
5353

54+
def "hides runtime dependencies and handles shadow dependencies"() {
55+
given:
56+
buildFile << """
57+
plugins {
58+
id 'elasticsearch.java'
59+
id 'elasticsearch.publish'
60+
id 'com.github.johnrengelman.shadow'
61+
}
62+
63+
repositories {
64+
mavenCentral()
65+
}
66+
67+
dependencies {
68+
implementation 'org.slf4j:log4j-over-slf4j:1.7.30'
69+
shadow 'org.slf4j:slf4j-api:1.7.30'
70+
}
71+
72+
publishing {
73+
repositories {
74+
maven {
75+
url = "\$buildDir/repo"
76+
}
77+
}
78+
}
79+
version = "1.0"
80+
group = 'org.acme'
81+
description = 'some description'
82+
"""
83+
84+
when:
85+
def result = gradleRunner('assemble', '--stacktrace').build()
86+
87+
then:
88+
result.task(":generatePom").outcome == TaskOutcome.SUCCESS
89+
file("build/distributions/hello-world-1.0-original.jar").exists()
90+
file("build/distributions/hello-world-1.0.jar").exists()
91+
file("build/distributions/hello-world-1.0-javadoc.jar").exists()
92+
file("build/distributions/hello-world-1.0-sources.jar").exists()
93+
file("build/distributions/hello-world-1.0.pom").exists()
94+
assertXmlEquals(file("build/distributions/hello-world-1.0.pom").text, """
95+
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
96+
<modelVersion>4.0.0</modelVersion>
97+
<groupId>org.acme</groupId>
98+
<artifactId>hello-world</artifactId>
99+
<version>1.0</version>
100+
<name>hello-world</name>
101+
<description>some description</description>
102+
<dependencies>
103+
<dependency>
104+
<groupId>org.slf4j</groupId>
105+
<artifactId>slf4j-api</artifactId>
106+
<version>1.7.30</version>
107+
<scope>runtime</scope>
108+
</dependency>
109+
</dependencies>
110+
</project>"""
111+
)
112+
}
113+
114+
def "handles project shadow dependencies"() {
115+
given:
116+
settingsFile << "include ':someLib'"
117+
file('someLib').mkdirs()
118+
buildFile << """
119+
plugins {
120+
id 'elasticsearch.java'
121+
id 'elasticsearch.publish'
122+
id 'com.github.johnrengelman.shadow'
123+
}
124+
125+
dependencies {
126+
shadow project(":someLib")
127+
}
128+
publishing {
129+
repositories {
130+
maven {
131+
url = "\$buildDir/repo"
132+
}
133+
}
134+
}
135+
136+
allprojects {
137+
apply plugin: 'elasticsearch.java'
138+
version = "1.0"
139+
group = 'org.acme'
140+
}
141+
142+
description = 'some description'
143+
"""
144+
145+
when:
146+
def result = gradleRunner(':assemble', '--stacktrace').build()
147+
148+
then:
149+
result.task(":generatePom").outcome == TaskOutcome.SUCCESS
150+
file("build/distributions/hello-world-1.0-original.jar").exists()
151+
file("build/distributions/hello-world-1.0.jar").exists()
152+
file("build/distributions/hello-world-1.0-javadoc.jar").exists()
153+
file("build/distributions/hello-world-1.0-sources.jar").exists()
154+
file("build/distributions/hello-world-1.0.pom").exists()
155+
assertXmlEquals(file("build/distributions/hello-world-1.0.pom").text, """
156+
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
157+
<modelVersion>4.0.0</modelVersion>
158+
<groupId>org.acme</groupId>
159+
<artifactId>hello-world</artifactId>
160+
<version>1.0</version>
161+
<name>hello-world</name>
162+
<description>some description</description>
163+
<dependencies>
164+
<dependency>
165+
<groupId>org.acme</groupId>
166+
<artifactId>someLib</artifactId>
167+
<version>1.0</version>
168+
<scope>runtime</scope>
169+
</dependency>
170+
</dependencies>
171+
</project>"""
172+
)
173+
}
174+
54175
def "generates artifacts for shadowed elasticsearch plugin"() {
55176
given:
56177
file('license.txt') << "License file"

buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
package org.elasticsearch.gradle;
1010

11-
import com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin;
11+
import com.github.jengelman.gradle.plugins.shadow.ShadowExtension;
1212
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin;
1313
import groovy.util.Node;
14-
import groovy.util.NodeList;
1514
import org.elasticsearch.gradle.info.BuildParams;
1615
import org.elasticsearch.gradle.precommit.PomValidationPrecommitPlugin;
1716
import org.elasticsearch.gradle.util.Util;
@@ -20,7 +19,6 @@
2019
import org.gradle.api.Project;
2120
import org.gradle.api.Task;
2221
import org.gradle.api.XmlProvider;
23-
import org.gradle.api.artifacts.ProjectDependency;
2422
import org.gradle.api.plugins.BasePlugin;
2523
import org.gradle.api.plugins.BasePluginConvention;
2624
import org.gradle.api.plugins.JavaPlugin;
@@ -52,8 +50,13 @@ public void apply(Project project) {
5250
private void configurePublications(Project project) {
5351
PublishingExtension publishingExtension = project.getExtensions().getByType(PublishingExtension.class);
5452
MavenPublication publication = publishingExtension.getPublications().create("elastic", MavenPublication.class);
55-
project.getPlugins().withType(JavaPlugin.class, plugin -> publication.from(project.getComponents().getByName("java")));
56-
project.getPlugins().withType(ShadowPlugin.class, plugin -> configureWithShadowPlugin(project, publication));
53+
project.afterEvaluate(project1 -> {
54+
if (project1.getPlugins().hasPlugin(ShadowPlugin.class)) {
55+
configureWithShadowPlugin(project1, publication);
56+
} else if (project1.getPlugins().hasPlugin(JavaPlugin.class)) {
57+
publication.from(project.getComponents().getByName("java"));
58+
}
59+
});
5760
}
5861

5962
private static String getArchivesBaseName(Project project) {
@@ -101,26 +104,8 @@ private static void addNameAndDescriptiontoPom(Project project, NamedDomainObjec
101104
}
102105

103106
private static void configureWithShadowPlugin(Project project, MavenPublication publication) {
104-
// Workaround for https://github.com/johnrengelman/shadow/issues/334
105-
// Here we manually add any project dependencies in the "shadow" configuration to our generated POM
106-
publication.getPom().withXml(xml -> {
107-
Node root = xml.asNode();
108-
NodeList dependencies = (NodeList) root.get("dependencies");
109-
Node dependenciesNode = (dependencies.size() == 0)
110-
? root.appendNode("dependencies")
111-
: (Node) ((NodeList) root.get("dependencies")).get(0);
112-
project.getConfigurations().getByName(ShadowBasePlugin.getCONFIGURATION_NAME()).getAllDependencies().all(dependency -> {
113-
if (dependency instanceof ProjectDependency) {
114-
Node dependencyNode = dependenciesNode.appendNode("dependency");
115-
dependencyNode.appendNode("groupId", dependency.getGroup());
116-
ProjectDependency projectDependency = (ProjectDependency) dependency;
117-
String artifactId = getArchivesBaseName(projectDependency.getDependencyProject());
118-
dependencyNode.appendNode("artifactId", artifactId);
119-
dependencyNode.appendNode("version", dependency.getVersion());
120-
dependencyNode.appendNode("scope", "compile");
121-
}
122-
});
123-
});
107+
ShadowExtension shadow = project.getExtensions().getByType(ShadowExtension.class);
108+
shadow.component(publication);
124109
}
125110

126111
private static void addScmInfo(XmlProvider xml) {

0 commit comments

Comments
 (0)