From dd632ba5751634aafa0c24bd0fea017368b15893 Mon Sep 17 00:00:00 2001 From: John Engelman Date: Tue, 16 Jun 2020 11:16:21 -0500 Subject: [PATCH] fix: remove deprecated features in component variant --- .../plugins/shadow/ShadowJavaPlugin.groovy | 6 +- .../plugins/shadow/PublishingSpec.groovy | 99 +++++++++++++++++++ 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy index e67dcd7c5..5e766a73a 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy @@ -46,14 +46,14 @@ class ShadowJavaPlugin implements Plugin { it.attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, Usage.JAVA_RUNTIME)) it.attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category, Category.LIBRARY)) it.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, LibraryElements.JAR)) - it.attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.EMBEDDED)) - // means no relocation of packages - it.attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8) + it.attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.SHADOWED)) } outgoing.artifact(project.tasks.getByName(SHADOW_JAR_TASK_NAME)) } } + project.configurations.shadowRuntimeElements.extendsFrom project.configurations.shadow + project.components.java { addVariantsFromConfiguration(project.configurations.shadowRuntimeElements) { mapToOptional() // make it a Maven optional dependency diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy index df9f2a984..7a346b2bb 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy @@ -2,6 +2,9 @@ package com.github.jengelman.gradle.plugins.shadow import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification +import groovy.json.JsonSlurper +import org.gradle.api.attributes.Bundling +import org.gradle.api.attributes.Usage import spock.lang.Issue class PublishingSpec extends PluginSpecification { @@ -261,4 +264,100 @@ class PublishingSpec extends PluginSpecification { assert dependency.artifactId.text() == 'b' assert dependency.version.text() == '1.0' } + + def "publish shadow jar with maven-publish plugin and Gradle metadata"() { + given: + repo.module('shadow', 'a', '1.0') + .insertFile('a.properties', 'a') + .insertFile('a2.properties', 'a2') + .publish() + repo.module('shadow', 'b', '1.0') + .insertFile('b.properties', 'b') + .publish() + + settingsFile << """ + rootProject.name = 'maven' + """ + buildFile << """ + apply plugin: 'maven-publish' + dependencies { + compile 'shadow:a:1.0' + compile 'shadow:b:1.0' + shadow 'shadow:b:1.0' + } + group = 'com.acme' + version = '1.0' + publishing { + publications { + java(MavenPublication) { + from components.java + } + } + repositories { + maven { + url "${publishingRepo.uri}" + } + } + } + """.stripIndent() + + when: + runner.withArguments('publish').build() + + then: + File mainJar = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0.jar').canonicalFile + File shadowJar = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0-all.jar').canonicalFile + assert mainJar.exists() + assert shadowJar.exists() + + and: + contains(shadowJar, ['a.properties', 'a2.properties']) + + and: "publishes both a POM file and a Gradle metadata file" + File pom = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0.pom').canonicalFile + File gmm = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0.module').canonicalFile + pom.exists() + gmm.exists() + + when: "POM file corresponds to a regular Java publication" + def pomContents = new XmlSlurper().parse(pom) + pomContents.dependencies.size() == 2 + + then: + def dependency1 = pomContents.dependencies[0].dependency[0] + dependency1.groupId.text() == 'shadow' + dependency1.artifactId.text() == 'a' + dependency1.version.text() == '1.0' + + def dependency2 = pomContents.dependencies[0].dependency[1] + dependency2.groupId.text() == 'shadow' + dependency2.artifactId.text() == 'b' + dependency2.version.text() == '1.0' + + when: "Gradle module metadata contains the Shadow variants" + def gmmContents = new JsonSlurper().parse(gmm) + + then: + gmmContents.variants.size() == 3 + gmmContents.variants.name as Set == ['apiElements', 'runtimeElements', 'shadowRuntimeElements'] as Set + + def apiVariant = gmmContents.variants.find { it.name == 'apiElements' } + apiVariant.attributes[Usage.USAGE_ATTRIBUTE.name] == Usage.JAVA_API + apiVariant.attributes[Bundling.BUNDLING_ATTRIBUTE.name] == Bundling.EXTERNAL + apiVariant.dependencies.size() == 2 + apiVariant.dependencies.module as Set == ['a', 'b'] as Set + + def runtimeVariant = gmmContents.variants.find { it.name == 'runtimeElements' } + runtimeVariant.attributes[Usage.USAGE_ATTRIBUTE.name] == Usage.JAVA_RUNTIME + runtimeVariant.attributes[Bundling.BUNDLING_ATTRIBUTE.name] == Bundling.EXTERNAL + runtimeVariant.dependencies.size() == 2 + apiVariant.dependencies.module as Set == ['a', 'b'] as Set + + def shadowRuntimeVariant = gmmContents.variants.find { it.name == 'shadowRuntimeElements' } + shadowRuntimeVariant.attributes[Usage.USAGE_ATTRIBUTE.name] == Usage.JAVA_RUNTIME + shadowRuntimeVariant.attributes[Bundling.BUNDLING_ATTRIBUTE.name] == Bundling.SHADOWED + shadowRuntimeVariant.dependencies.size() == 1 + shadowRuntimeVariant.dependencies.module as Set == ['b'] as Set + + } }