Skip to content

Conversation

@sibasispadhi
Copy link

Migrate examples/distro/build.gradle from Groovy to Kotlin DSL for improved consistency with the rest of the repository.

Most of the instrumentation repository (608 files) already uses Kotlin for Gradle build files. This migration converts one of the remaining Groovy files to match the established pattern.

Fixes #15794

@sibasispadhi sibasispadhi requested a review from a team as a code owner January 10, 2026 22:17
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 10, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@sibasispadhi sibasispadhi force-pushed the examples/convert-distro-to-kotlin branch from 4da64b3 to c15f82e Compare January 10, 2026 22:28
Copy link
Member

@jaydeluca jaydeluca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a merge conflict that needs to be resolved, in case you didn't see

@sibasispadhi sibasispadhi force-pushed the examples/convert-distro-to-kotlin branch from 87c1813 to 960a046 Compare January 15, 2026 02:06
@sibasispadhi sibasispadhi force-pushed the examples/convert-distro-to-kotlin branch 2 times, most recently from f2b1f33 to 61ce0fe Compare January 16, 2026 02:41
Convert the examples/distro/build.gradle to build.gradle.kts using
Kotlin DSL syntax. This improves type safety and IDE support while
maintaining all existing functionality.

Key changes:
- Rename build.gradle to build.gradle.kts
- Convert Groovy syntax to Kotlin DSL:
  - Use mapOf() instead of Groovy maps
  - Use apply(plugin = "name") instead of apply plugin: "name"
  - Use plugins.withType<JavaPlugin> for deferred configuration
  - Use tasks.named<Type>() for task configuration
- Update .github/scripts/update-sdk-version.sh to match new Kotlin
  DSL syntax ("opentelemetrySdk" to "version" format)
- Preserve extra["versions"] and extra["deps"] for use by Groovy
  subproject build files and applied scripts
- Set Java 8 release only for main source compilation (compileJava),
  allowing tests to use modern Java features

The conversion maintains equivalence to the original Groovy build
while following Gradle Kotlin DSL best practices.

Signed-off-by: Sibasis Padhi <[email protected]>
@sibasispadhi sibasispadhi force-pushed the examples/convert-distro-to-kotlin branch from 61ce0fe to b8ec320 Compare January 21, 2026 03:22
@sibasispadhi
Copy link
Author

@jaydeluca kindly review.

@jaydeluca
Copy link
Member

@jaydeluca kindly review.

Regarding the add("implementation", ...) syntax: inside plugins.withType { dependencies { } }, the Kotlin DSL doesn't provide the extension functions directly, so add() is the idiomatic approach for deferred configuration (consistent with how other Kotlin DSL builds handle this pattern).

are you sure? I don't see anything about that mentioned in the docs https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.artifacts.dsl/-dependency-handler/index.html

I'm also not sure you even need plugins.withType {?

I would think you could simplify this to just

  dependencies {
    implementation(platform("io.opentelemetry:opentelemetry-bom:${versions.getValue("opentelemetrySdk")}"))

    implementation(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom:${versions.getValue("opentelemetryJavaagent")}"))
    implementation(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:${versions.getValue("opentelemetryJavaagentAlpha")}"))

    testImplementation("org.mockito:mockito-core:5.21.0")

    testImplementation(enforcedPlatform("org.junit:junit-bom:5.14.2"))
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
  }

  tasks.test {
    useJUnitPlatform()
  }

  tasks.compileJava {
    options.release.set(8)
  }

but perhaps you could try it out

Remove unnecessary plugins.withType wrapper and use direct dependency
extension functions (implementation, testImplementation, etc.) instead
of add() calls. Also switch from tasks.named<Type>() to simpler
tasks.taskName syntax and use getValue() for safer map access.

These changes follow standard Gradle Kotlin DSL conventions and make
the build file more concise and readable.
The plugins.withType<JavaPlugin> wrapper is actually necessary in a
subprojects {} block because the Java plugin extensions (implementation,
testImplementation, etc.) aren't available until after the plugin is
applied. However, we can still use the cleaner extension function syntax
instead of add() calls inside the wrapper.

This combines the best of both approaches:
- plugins.withType for proper deferred configuration
- Extension functions (implementation()) for cleaner syntax
- getValue() for type-safe map access
After testing, the issue is that inside plugins.withType/withId callbacks,
the dependency extension functions (implementation(), testImplementation())
are not available in the scope. We must use add() inside the callback.

However, tasks can be configured outside the callback using tasks.named<Type>()
which provides type safety.

Final solution:
- plugins.withId("java") { dependencies { add(...) } }
- tasks.named<Test>() for type-safe task configuration
- getValue() for safer map access

This is the idiomatic Kotlin DSL pattern for subprojects configuration.
@sibasispadhi
Copy link
Author

@jaydeluca
After some trial and error, I discovered that the dependency extension functions
(implementation(), testImplementation()) aren't available inside plugins.withId()
callbacks in a subprojects block; this is a Kotlin DSL scoping limitation.

Final solution:

  • Used add() inside plugins.withId("java") { dependencies { } }
  • Moved task configuration outside using tasks.named() for type safety
  • Kept getValue() for safer map access

This hybrid approach gives us deferred configuration where needed (dependencies) and type safety where possible (tasks). Thanks for pushing me to explore different approaches. I definitely learned more about Kotlin DSL scoping through this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert examples/distro Gradle build to Kotlin

3 participants