Skip to content

Commit 4cb8bde

Browse files
authored
✨ BOM support (#239)
Fixes #225 I avoided rolling this out more extensively because I anticipate this use case is far more common with test plugins compared to others - but we now have more escape hatches for users and can monitor use cases as we go
1 parent 889d0b1 commit 4cb8bde

5 files changed

Lines changed: 91 additions & 8 deletions

File tree

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ development and/or in production
1313

1414
> The work on this software project is in no way associated with my employer nor with the role I'm having at my employer. Any requests for changes will be decided upon exclusively by myself based on my personal preferences. I maintain this project as much or as little as my spare time permits.
1515
16+
## Mockito Example
17+
18+
The Java community is quickly preparing for upcoming JDK restrictions that will improve the security and stability of the java community. One of these restrictions is the removal of the ability to self-attach javaagents.
19+
20+
You may have come across this plugin because you encountered the following message in your build:
21+
> Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build as described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org.mockito/org/mockito/Mockito.html#0.3
22+
23+
This plugin enables you to quickly and easily resolve these constraints by adding the Mockito agent to your build as a javaagent, while maintaining compatability with the Gradle concepts such as up to date checks, test task caching, and configuration caching.
24+
25+
26+
```kotlin
27+
plugins {
28+
java
29+
id("com.ryandens.javaagent-application") version "0.9.0"
30+
}
31+
32+
dependencies {
33+
testImplementation(platform("org.mockito:mockito-bom:5.18.0"))
34+
testImplementation("org.mockito:mockito-core")
35+
testJavaagent("org.mockito:mockito-core")
36+
}
37+
```
38+
39+
> ℹ️ **Note:** The [Javaagent Test Plugin](#java-test-task-integration) automatically resolves dependency versions for javaagents dependencies that did not request one from the test runtime classpath.
40+
1641
## Application Plugin integration
1742

1843
This Gradle plugin tightly integrates with the [Gradle application plugin](https://docs.gradle.org/current/userguide/application_plugin.html)
@@ -22,7 +47,7 @@ then specify the javaagent you would like to attach in the dependencies block
2247
```kotlin
2348
plugins {
2449
application
25-
id("com.ryandens.javaagent-application") version "0.6.1"
50+
id("com.ryandens.javaagent-application") version "0.9.0"
2651
}
2752

2853
application {
@@ -52,7 +77,7 @@ would like to attach in the dependencies block.
5277
plugins {
5378
java
5479
id("com.google.cloud.tools.jib") version "3.1.4"
55-
id("com.ryandens.javaagent-jib") version "0.6.1"
80+
id("com.ryandens.javaagent-jib") version "0.9.0"
5681
}
5782

5883
jib.container {
@@ -75,7 +100,7 @@ Testing (IAST) product.
75100
```kotlin
76101
plugins {
77102
java
78-
id("com.ryandens.javaagent-test") version "0.6.1"
103+
id("com.ryandens.javaagent-test") version "0.9.0"
79104
}
80105

81106
tasks.named<Test>("test") {
@@ -112,8 +137,8 @@ desired application distribution or execution method.
112137
```kotlin
113138
plugins {
114139
application
115-
id("com.ryandens.javaagent-otel-modification") version "0.6.1"
116-
id("com.ryandens.javaagent-application") version "0.6.1"
140+
id("com.ryandens.javaagent-otel-modification") version "0.9.0"
141+
id("com.ryandens.javaagent-application") version "0.9.0"
117142
}
118143

119144
dependencies {

example-projects/buildSrc/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ repositories {
88
}
99
dependencies {
1010
/* uncomment when doing local testing on this project, leave commented out for functional test to resolve latest plugin
11-
implementation("com.ryandens:otel:0.6.1")
12-
implementation("com.ryandens:plugin:0.6.1")
11+
implementation("com.ryandens:otel:0.9.0")
12+
implementation("com.ryandens:plugin:0.9.0")
1313
*/
1414
implementation("io.opentelemetry.instrumentation.muzzle-generation:io.opentelemetry.instrumentation.muzzle-generation.gradle.plugin:2.8.0-alpha")
1515
implementation("io.opentelemetry.instrumentation.muzzle-check:io.opentelemetry.instrumentation.muzzle-check.gradle.plugin:2.8.0-alpha")

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
55
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
66
org.gradle.configuration-cache=true
77
org.gradle.caching=true
8-
version=0.8.0
8+
version=0.9.0
99
group=com.ryandens

plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentTestPlugin.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ryandens.javaagent
22

3+
import com.ryandens.javaagent.utils.JavaagentVersionUtil
34
import org.gradle.api.NamedDomainObjectProvider
45
import org.gradle.api.Plugin
56
import org.gradle.api.Project
@@ -27,6 +28,18 @@ class JavaagentTestPlugin :
2728
// we expect javaagents to come as shaded JARs
2829
it.isTransitive = false
2930
it.extendsFrom(javaagentConfiguration.get())
31+
it.resolutionStrategy { strategy ->
32+
strategy.eachDependency { dep ->
33+
if (dep.requested.version.isNullOrBlank()) {
34+
dep.useVersion(
35+
JavaagentVersionUtil.versionFromDependencyAndConfiguration(
36+
dep.requested,
37+
project.configurations.named("testRuntimeClasspath"),
38+
),
39+
)
40+
}
41+
}
42+
}
3043
}
3144

3245
val extension = project.extensions.create("javaagentTest", JavaagentTestExtension::class.java)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ryandens.javaagent.utils
2+
3+
import org.gradle.api.NamedDomainObjectProvider
4+
import org.gradle.api.artifacts.Configuration
5+
import org.gradle.api.artifacts.ModuleVersionSelector
6+
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
7+
8+
/**
9+
* Utility class for determining the version of a dependency in the javaagent configuration from other configurations
10+
*/
11+
object JavaagentVersionUtil {
12+
/**
13+
* Determines the version of a dependency in the javaagent configuration from another configuration based on the
14+
* requested [ModuleVersionSelector] and the provided [NamedDomainObjectProvider] of [Configuration]s to search.
15+
*
16+
* @param requested the requested dependency
17+
* @param configuration the configuration to search
18+
* @return the version of the dependency to use in the javaagent configuration
19+
*/
20+
fun versionFromDependencyAndConfiguration(
21+
requested: ModuleVersionSelector,
22+
configuration: NamedDomainObjectProvider<Configuration>,
23+
): String {
24+
val artifacts =
25+
configuration
26+
.get()
27+
.incoming
28+
.artifactView { viewConfiguration ->
29+
viewConfiguration.componentFilter {
30+
it.displayName.startsWith("${requested.group}:${requested.name}:")
31+
}
32+
}.artifacts
33+
34+
if (artifacts.count() == 0) {
35+
throw IllegalArgumentException(
36+
"No artifacts found for ${requested.group}:${requested.name} in configuration ${configuration.name}",
37+
)
38+
} else if (artifacts.count() > 1) {
39+
throw IllegalArgumentException(
40+
"Multiple artifacts found for ${requested.group}:${requested.name} in configuration ${configuration.name}",
41+
)
42+
}
43+
return (artifacts.single().id.componentIdentifier as ModuleComponentIdentifier).version
44+
}
45+
}

0 commit comments

Comments
 (0)