Skip to content

Commit

Permalink
Shadowed dependencies should be hidden from pom dependencies (#73467) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed May 27, 2021
1 parent e95bd59 commit de41041
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,127 @@ class PublishPluginFuncTest extends AbstractGradleFuncTest {
)
}

def "hides runtime dependencies and handles shadow dependencies"() {
given:
buildFile << """
plugins {
id 'elasticsearch.java'
id 'elasticsearch.publish'
id 'com.github.johnrengelman.shadow'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.slf4j:log4j-over-slf4j:1.7.30'
shadow 'org.slf4j:slf4j-api:1.7.30'
}
publishing {
repositories {
maven {
url = "\$buildDir/repo"
}
}
}
version = "1.0"
group = 'org.acme'
description = 'some description'
"""

when:
def result = gradleRunner('assemble', '--stacktrace').build()

then:
result.task(":generatePom").outcome == TaskOutcome.SUCCESS
file("build/distributions/hello-world-1.0-original.jar").exists()
file("build/distributions/hello-world-1.0.jar").exists()
file("build/distributions/hello-world-1.0-javadoc.jar").exists()
file("build/distributions/hello-world-1.0-sources.jar").exists()
file("build/distributions/hello-world-1.0.pom").exists()
assertXmlEquals(file("build/distributions/hello-world-1.0.pom").text, """
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>hello-world</artifactId>
<version>1.0</version>
<name>hello-world</name>
<description>some description</description>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>"""
)
}

def "handles project shadow dependencies"() {
given:
settingsFile << "include ':someLib'"
file('someLib').mkdirs()
buildFile << """
plugins {
id 'elasticsearch.java'
id 'elasticsearch.publish'
id 'com.github.johnrengelman.shadow'
}
dependencies {
shadow project(":someLib")
}
publishing {
repositories {
maven {
url = "\$buildDir/repo"
}
}
}
allprojects {
apply plugin: 'elasticsearch.java'
version = "1.0"
group = 'org.acme'
}
description = 'some description'
"""

when:
def result = gradleRunner(':assemble', '--stacktrace').build()

then:
result.task(":generatePom").outcome == TaskOutcome.SUCCESS
file("build/distributions/hello-world-1.0-original.jar").exists()
file("build/distributions/hello-world-1.0.jar").exists()
file("build/distributions/hello-world-1.0-javadoc.jar").exists()
file("build/distributions/hello-world-1.0-sources.jar").exists()
file("build/distributions/hello-world-1.0.pom").exists()
assertXmlEquals(file("build/distributions/hello-world-1.0.pom").text, """
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>hello-world</artifactId>
<version>1.0</version>
<name>hello-world</name>
<description>some description</description>
<dependencies>
<dependency>
<groupId>org.acme</groupId>
<artifactId>someLib</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>"""
)
}

def "generates artifacts for shadowed elasticsearch plugin"() {
given:
file('license.txt') << "License file"
Expand Down
35 changes: 10 additions & 25 deletions buildSrc/src/main/java/org/elasticsearch/gradle/PublishPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

package org.elasticsearch.gradle;

import com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin;
import com.github.jengelman.gradle.plugins.shadow.ShadowExtension;
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin;
import groovy.util.Node;
import groovy.util.NodeList;
import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.precommit.PomValidationPrecommitPlugin;
import org.elasticsearch.gradle.util.Util;
Expand All @@ -20,7 +19,6 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.XmlProvider;
import org.gradle.api.artifacts.ProjectDependency;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.BasePluginConvention;
import org.gradle.api.plugins.JavaPlugin;
Expand Down Expand Up @@ -52,8 +50,13 @@ public void apply(Project project) {
private void configurePublications(Project project) {
PublishingExtension publishingExtension = project.getExtensions().getByType(PublishingExtension.class);
MavenPublication publication = publishingExtension.getPublications().create("elastic", MavenPublication.class);
project.getPlugins().withType(JavaPlugin.class, plugin -> publication.from(project.getComponents().getByName("java")));
project.getPlugins().withType(ShadowPlugin.class, plugin -> configureWithShadowPlugin(project, publication));
project.afterEvaluate(project1 -> {
if (project1.getPlugins().hasPlugin(ShadowPlugin.class)) {
configureWithShadowPlugin(project1, publication);
} else if (project1.getPlugins().hasPlugin(JavaPlugin.class)) {
publication.from(project.getComponents().getByName("java"));
}
});
}

private static String getArchivesBaseName(Project project) {
Expand Down Expand Up @@ -101,26 +104,8 @@ private static void addNameAndDescriptiontoPom(Project project, NamedDomainObjec
}

private static void configureWithShadowPlugin(Project project, MavenPublication publication) {
// Workaround for https://github.com/johnrengelman/shadow/issues/334
// Here we manually add any project dependencies in the "shadow" configuration to our generated POM
publication.getPom().withXml(xml -> {
Node root = xml.asNode();
NodeList dependencies = (NodeList) root.get("dependencies");
Node dependenciesNode = (dependencies.size() == 0)
? root.appendNode("dependencies")
: (Node) ((NodeList) root.get("dependencies")).get(0);
project.getConfigurations().getByName(ShadowBasePlugin.getCONFIGURATION_NAME()).getAllDependencies().all(dependency -> {
if (dependency instanceof ProjectDependency) {
Node dependencyNode = dependenciesNode.appendNode("dependency");
dependencyNode.appendNode("groupId", dependency.getGroup());
ProjectDependency projectDependency = (ProjectDependency) dependency;
String artifactId = getArchivesBaseName(projectDependency.getDependencyProject());
dependencyNode.appendNode("artifactId", artifactId);
dependencyNode.appendNode("version", dependency.getVersion());
dependencyNode.appendNode("scope", "compile");
}
});
});
ShadowExtension shadow = project.getExtensions().getByType(ShadowExtension.class);
shadow.component(publication);
}

private static void addScmInfo(XmlProvider xml) {
Expand Down

0 comments on commit de41041

Please sign in to comment.