Skip to content

Commit

Permalink
Fix tooling API metadata - bundling attribute (#12378)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jjohannes committed Mar 3, 2020
1 parent d3081be commit 50234ac
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
Expand Up @@ -233,6 +233,10 @@ open class ShadedJarPlugin : Plugin<Project> {
tasks.named<Jar>("sourcesJar") {
from(sourcesPath.incoming.artifactView { lenient(true) }.files)
}
val sourcesElements by configurations
sourcesElements.attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED))
}
}

private
Expand Down
@@ -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]")
}
}

0 comments on commit 50234ac

Please sign in to comment.