Skip to content

Commit c6f3096

Browse files
committed
Changed to handling custom resources via a LIBRARY_ELEMENTS_ATTRIBUTE value instead of an additional RESOURCE_ATTRIBUTE, which fixed subsetting them from the runtimeClasspath with an ArtifactView.
1 parent 8bfa08a commit c6f3096

File tree

7 files changed

+128
-163
lines changed

7 files changed

+128
-163
lines changed
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
11
import org.sdkotlin.buildlogic.artifacts.dsl.DependencyCreationExtension
2-
import org.sdkotlin.buildlogic.attributes.CustomResources.CUSTOM_RESOURCE
3-
import org.sdkotlin.buildlogic.attributes.DefaultResourceAttributeRule
4-
import org.sdkotlin.buildlogic.attributes.ResourceAttributeDependencyCreationExtension
5-
import org.sdkotlin.buildlogic.attributes.ResourceAttributes.RESOURCE_ATTRIBUTE
2+
import org.sdkotlin.buildlogic.attributes.CustomResources.CUSTOM_RESOURCES
3+
import org.sdkotlin.buildlogic.attributes.LibraryElementsAttributeDependencyCreationExtension
64

75
dependencies {
86

9-
attributesSchema {
10-
// Register a new attribute key for variant-aware consumption of
11-
// "custom" resources dependencies.
12-
attribute(RESOURCE_ATTRIBUTE)
13-
}
14-
15-
components {
16-
// Add the resource attribute to all components with a default
17-
// value of "none".
18-
all<DefaultResourceAttributeRule>()
19-
}
20-
21-
// Add a `DependencyHandler` extension for declaring dependencies on
22-
// artifacts with "custom" resources attributes.
7+
// Add a `DependencyHandler` extension for declaring a dependency on
8+
// the "custom resources" artifact variant.
239
extensions.add(
2410
DependencyCreationExtension::class.java,
2511
"customResources",
26-
ResourceAttributeDependencyCreationExtension(
12+
LibraryElementsAttributeDependencyCreationExtension(
2713
dependencyHandler = this,
2814
objects = objects,
29-
resourceAttributeValue = CUSTOM_RESOURCE
15+
libraryElementsAttributeValue = CUSTOM_RESOURCES
3016
)
3117
)
3218
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import org.sdkotlin.buildlogic.attributes.CustomResources.CUSTOM_RESOURCE
2-
import org.sdkotlin.buildlogic.attributes.ResourceAttributes.applyResourceAttributes
1+
import org.gradle.api.artifacts.type.ArtifactTypeDefinition.JVM_RESOURCES_DIRECTORY
2+
import org.sdkotlin.buildlogic.attributes.CustomResources.CUSTOM_RESOURCES
3+
import org.sdkotlin.buildlogic.attributes.LibraryElementsAttributes.applyLibraryElementsAttributes
34

45
@Suppress("UnstableApiUsage")
56
configurations {
67

7-
// Create variant-aware consumable configuration for "custom" resources
8+
// Create a variant-aware consumable configuration for "custom resources"
89
// artifacts.
910
consumable("customElements") {
1011
attributes {
11-
applyResourceAttributes(objects, CUSTOM_RESOURCE)
12+
applyLibraryElementsAttributes(objects, CUSTOM_RESOURCES)
1213
}
1314
}
1415
}
@@ -19,7 +20,9 @@ artifacts {
1920
val customResourceDirectory: Directory =
2021
project.layout.projectDirectory.dir("src/main/custom")
2122

22-
// No build step is necessary, so directly add the directory as an
23-
// artifact to the variant-aware consumable configuration.
24-
add("customElements", customResourceDirectory)
23+
// No build step is necessary, so directly add the directory as an
24+
// artifact to the variant-aware consumable configuration.
25+
add("customElements", customResourceDirectory) {
26+
type = JVM_RESOURCES_DIRECTORY
27+
}
2528
}

build-logic/src/main/kotlin/org.sdkotlin.buildlogic.kotlin-project.gradle.kts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
import org.sdkotlin.buildlogic.attributes.CustomResources.CUSTOM_RESOURCES
3+
import org.sdkotlin.buildlogic.attributes.LibraryElementsAttributes.applyLibraryElementsAttributes
24

35
plugins {
46
kotlin("jvm")
@@ -47,4 +49,43 @@ tasks {
4749
)
4850
}
4951
}
52+
53+
register("printCustomResourcesClasspath") {
54+
55+
group = "custom-resources"
56+
description =
57+
"Prints the custom resources subset of the runtime classpath"
58+
59+
val fileCollection: Provider<FileCollection> = provider {
60+
configurations.runtimeClasspath.get().incoming.artifactView {
61+
attributes {
62+
applyLibraryElementsAttributes(objects, CUSTOM_RESOURCES)
63+
}
64+
}.files
65+
}
66+
67+
doLast {
68+
69+
val customResourcesAsPath = fileCollection.get().asPath
70+
71+
println("customResourcesAsPath: $customResourcesAsPath")
72+
}
73+
}
74+
75+
register("printRuntimeClasspath") {
76+
77+
group = "custom-resources"
78+
description = "Prints the runtime classpath"
79+
80+
val fileCollection: Provider<FileCollection> = provider {
81+
configurations.runtimeClasspath.get()
82+
}
83+
84+
doLast {
85+
86+
val runtimeClasspathAsPath = fileCollection.get().asPath
87+
88+
println("runtimeClasspathAsPath: $runtimeClasspathAsPath")
89+
}
90+
}
5091
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package org.sdkotlin.buildlogic.attributes
22

3-
import org.sdkotlin.buildlogic.attributes.ResourceAttributes.RESOURCE_ATTRIBUTE
3+
import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
44

55
/**
66
* A companion object for the custom resources convention plugins.
77
*/
88
object CustomResources {
99

1010
/**
11-
* The [RESOURCE_ATTRIBUTE] value for the "custom" resource dependency
12-
* variant.
11+
* The [LIBRARY_ELEMENTS_ATTRIBUTE] value for the "custom resources"
12+
* dependency variant.
1313
*/
14-
const val CUSTOM_RESOURCE = "Custom"
14+
const val CUSTOM_RESOURCES = "custom resources"
1515
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.sdkotlin.buildlogic.attributes
2+
3+
import org.gradle.api.artifacts.Dependency
4+
import org.gradle.api.artifacts.ProjectDependency
5+
import org.gradle.api.artifacts.dsl.DependencyHandler
6+
import org.gradle.api.attributes.AttributeContainer
7+
import org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE
8+
import org.gradle.api.attributes.Bundling.EXTERNAL
9+
import org.gradle.api.attributes.Category
10+
import org.gradle.api.attributes.Category.CATEGORY_ATTRIBUTE
11+
import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
12+
import org.gradle.api.attributes.Usage.JAVA_RUNTIME
13+
import org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE
14+
import org.gradle.api.model.ObjectFactory
15+
import org.gradle.kotlin.dsl.named
16+
import org.sdkotlin.buildlogic.artifacts.dsl.DependencyCreationExtension
17+
import org.sdkotlin.buildlogic.attributes.LibraryElementsAttributes.applyLibraryElementsAttributes
18+
19+
/**
20+
* A namespace for custom [LIBRARY_ELEMENTS_ATTRIBUTE] utilities.
21+
*/
22+
object LibraryElementsAttributes {
23+
24+
/**
25+
* Helper function to set the ecosystem-independent standard attributes for
26+
* [LIBRARY_ELEMENTS_ATTRIBUTE]-based variants.
27+
*/
28+
fun AttributeContainer.applyLibraryElementsAttributes(
29+
objects: ObjectFactory,
30+
libraryElementsAttributeValue: String
31+
) {
32+
attribute(USAGE_ATTRIBUTE, objects.named(JAVA_RUNTIME))
33+
attribute(CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
34+
attribute(LIBRARY_ELEMENTS_ATTRIBUTE,
35+
objects.named(libraryElementsAttributeValue))
36+
attribute(BUNDLING_ATTRIBUTE, objects.named(EXTERNAL))
37+
}
38+
}
39+
40+
/**
41+
* A [DependencyHandler] extension that sets the standard attributes for
42+
* [LIBRARY_ELEMENTS_ATTRIBUTE]-based variants of a declared [Dependency].
43+
*/
44+
class LibraryElementsAttributeDependencyCreationExtension(
45+
private val dependencyHandler: DependencyHandler,
46+
private val objects: ObjectFactory,
47+
private val libraryElementsAttributeValue: String
48+
) : DependencyCreationExtension {
49+
50+
override fun invoke(notation: Any): Dependency {
51+
52+
val dependency = dependencyHandler.create(notation)
53+
54+
require(dependency is ProjectDependency) {
55+
"Dependency type ${dependency::class.qualifiedName} unknown!"
56+
}
57+
58+
dependency.attributes {
59+
applyLibraryElementsAttributes(
60+
objects,
61+
libraryElementsAttributeValue
62+
)
63+
}
64+
65+
return dependency
66+
}
67+
}

build-logic/src/main/kotlin/org/sdkotlin/buildlogic/attributes/ResourceAttributes.kt

Lines changed: 0 additions & 100 deletions
This file was deleted.

subprojects/app/build.gradle.kts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import org.sdkotlin.buildlogic.attributes.CustomResources.CUSTOM_RESOURCE
2-
import org.sdkotlin.buildlogic.attributes.ResourceAttributes.applyResourceAttributes
3-
41
plugins {
52
application
63
alias(libs.plugins.springboot.plugin)
@@ -36,32 +33,3 @@ dependencies {
3633
application {
3734
mainClass = "org.sdkotlin.springdemo.SpringBootAppKt"
3835
}
39-
40-
tasks {
41-
42-
register("printCustomClasspath") {
43-
44-
group = "custom"
45-
description = "Print the custom classpath"
46-
47-
val fileCollection: FileCollection =
48-
configurations.runtimeClasspath.get().incoming.artifactView {
49-
50-
lenient(false)
51-
52-
@Suppress("UnstableApiUsage")
53-
withVariantReselection()
54-
55-
attributes {
56-
applyResourceAttributes(objects, CUSTOM_RESOURCE)
57-
}
58-
}.files
59-
60-
doLast {
61-
62-
val customResourcesAsPath = fileCollection.asPath
63-
64-
println("customResourcesAsPath: $customResourcesAsPath")
65-
}
66-
}
67-
}

0 commit comments

Comments
 (0)