Skip to content

Commit

Permalink
fix: remove deprecated features in component variant
Browse files Browse the repository at this point in the history
  • Loading branch information
John Engelman committed Jun 16, 2020
1 parent 89d6e36 commit dd632ba
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ class ShadowJavaPlugin implements Plugin<Project> {
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)

This comment has been minimized.

Copy link
@melix

melix Jun 16, 2020

Contributor

We're going to make it easier to declare in a future Gradle version (see gradle/gradle#13395 which has a use case directly inspired by this).
In the mean time, you can set the TargetJvmVersion attribute to the java compile target version, which is an acceptable approximation, similarly to this:

https://github.com/gradle/gradle/blob/2fb2b398c07b8960ceda36386fbee50231ba7a44/subprojects/plugins/src/main/java/org/gradle/api/plugins/internal/support/DefaultJvmEcosystemUtilities.java#L179

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

}
}

0 comments on commit dd632ba

Please sign in to comment.