From 50234ac4a107d9c7c5c95ccccb31d90a54b64c18 Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Mon, 2 Mar 2020 16:08:49 +0100 Subject: [PATCH] Fix tooling API metadata - bundling attribute (#12378) There are two different bundlings used in the metadata: runtime variant: SHADOWED sources variant: EXTERNAL Because of that, a resolution without requested attributes is not able to identify the 'runtime' variant as the preferred one. To resolve that, we now set the sources variant to SHADOWED as well to match the library variant it belongs to. Although it technically does not contain all the sources of the shadowed classes. Then legacy behavior like: 'configurations.compile.files' (resolving the Tooling API directly through the 'compile' configuration) still works as before. The runtime variant is selected. This commit also adds integration test coverage: This includes the typical scenarios - compileClasspath and runtimeClasspath - and also the case where you just create a configuration without attributes - similar to resolving the legacy 'default' or 'compile' configurations. --- .../gradlebuild/packaging/ShadedJarPlugin.kt | 4 + .../ToolingApiResolveIntegrationTest.groovy | 102 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 subprojects/tooling-api/src/integTest/groovy/org/gradle/integtests/tooling/ToolingApiResolveIntegrationTest.groovy diff --git a/buildSrc/subprojects/packaging/src/main/kotlin/org/gradle/gradlebuild/packaging/ShadedJarPlugin.kt b/buildSrc/subprojects/packaging/src/main/kotlin/org/gradle/gradlebuild/packaging/ShadedJarPlugin.kt index 76f75c62e43d..9ae39f3caa37 100644 --- a/buildSrc/subprojects/packaging/src/main/kotlin/org/gradle/gradlebuild/packaging/ShadedJarPlugin.kt +++ b/buildSrc/subprojects/packaging/src/main/kotlin/org/gradle/gradlebuild/packaging/ShadedJarPlugin.kt @@ -233,6 +233,10 @@ open class ShadedJarPlugin : Plugin { tasks.named("sourcesJar") { from(sourcesPath.incoming.artifactView { lenient(true) }.files) } + val sourcesElements by configurations + sourcesElements.attributes { + attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED)) + } } private diff --git a/subprojects/tooling-api/src/integTest/groovy/org/gradle/integtests/tooling/ToolingApiResolveIntegrationTest.groovy b/subprojects/tooling-api/src/integTest/groovy/org/gradle/integtests/tooling/ToolingApiResolveIntegrationTest.groovy new file mode 100644 index 000000000000..03a5586ee44b --- /dev/null +++ b/subprojects/tooling-api/src/integTest/groovy/org/gradle/integtests/tooling/ToolingApiResolveIntegrationTest.groovy @@ -0,0 +1,102 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.gradle.integtests.tooling + +import org.gradle.integtests.fixtures.AbstractIntegrationSpec +import org.gradle.util.GradleVersion +import spock.lang.Unroll + +class ToolingApiResolveIntegrationTest extends AbstractIntegrationSpec { + + @Unroll + def "can resolve tooling API via #configuration"() { + given: + def tapiVersion = GradleVersion.current().version + buildFile << """ + plugins { + id 'java-library' + } + repositories { + maven { url '${buildContext.libsRepo.toURI().toURL()}' } + ${mavenCentralRepository()} + } + + configurations { customConf } + + dependencies { + implementation 'org.gradle:gradle-tooling-api:${tapiVersion}' + customConf 'org.gradle:gradle-tooling-api:${tapiVersion}' + } + + tasks.register('resolve') { + doLast { + println configurations.${configuration}.files.collect { it.name } + } + } + """ + + when: + succeeds 'resolve' + + then: + outputContains("[gradle-tooling-api-${tapiVersion}.jar, slf4j-api-1.7.28.jar]") + + where: + configuration << ['compileClasspath', 'runtimeClasspath', 'customConf'] + } + + def "can resolve sources variant of tooling API"() { + given: + def tapiVersion = GradleVersion.current().version + buildFile << """ + plugins { + id 'java-library' + } + repositories { + maven { url '${buildContext.libsRepo.toURI().toURL()}' } + ${mavenCentralRepository()} + } + + configurations { + sources { + extendsFrom runtimeClasspath + canBeConsumed = false + visible = false + attributes { + attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION)) + attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, DocsType.SOURCES)) + } + } + } + + dependencies { + implementation "org.gradle:gradle-tooling-api:${tapiVersion}" + } + + tasks.register('resolve') { + doLast { + println configurations.sources.files.collect { it.name } + } + } + """ + + when: + succeeds 'resolve' + + then: + outputContains("[gradle-tooling-api-${tapiVersion}-sources.jar]") + } +}