From a53933d8cedd8207043b9d64596e139d47c3b9a0 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 8 Aug 2023 14:07:50 +0200 Subject: [PATCH] Scripting: do not pack generic dependencies jar into maven-all The -all jar is compacted with proguard, and the proguard removes synthetic methods methods with $default suffix from classes regardless of the -keep flag. Therefore the calls to the $default metods lead to the NoSuchMethodError exception on runtime. Removing the main interface module from the proguarded jar solves the problem for now (but not generally, the use of "proguarded" jars as libraries should be discouraged.) #KT-60862 fixed --- build.gradle.kts | 1 + .../dependencies-maven-all/build.gradle.kts | 18 ++++++++- .../experimental/test/MavenAllResolverTest.kt | 39 +++++++++++++++++++ .../tools/kotlin-main-kts/build.gradle.kts | 1 + ...otlin-scripting-dependencies-maven-all.pom | 8 ++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 libraries/scripting/dependencies-maven-all/test/org/jetbrains/kotlin/script/experimental/test/MavenAllResolverTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 7fdcfe52cde..2014145b874 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -710,6 +710,7 @@ tasks { dependsOn(":kotlin-scripting-jvm-host-test:test") dependsOn(":kotlin-scripting-dependencies:test") dependsOn(":kotlin-scripting-dependencies-maven:test") + dependsOn(":kotlin-scripting-dependencies-maven-all:test") dependsOn(":kotlin-scripting-jsr223-test:test") // see comments on the task in kotlin-scripting-jvm-host-test // dependsOn(":kotlin-scripting-jvm-host-test:embeddableTest") diff --git a/libraries/scripting/dependencies-maven-all/build.gradle.kts b/libraries/scripting/dependencies-maven-all/build.gradle.kts index 7180cba0b4d..3e09668fb92 100644 --- a/libraries/scripting/dependencies-maven-all/build.gradle.kts +++ b/libraries/scripting/dependencies-maven-all/build.gradle.kts @@ -18,9 +18,18 @@ plugins { id("jps-compatible") } +val proguardLibraryJars by configurations.creating { + attributes { + attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME)) + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR)) + } +} + dependencies { + api(project(":kotlin-scripting-dependencies")) + proguardLibraryJars(project(":kotlin-scripting-dependencies")) + embedded(project(":kotlin-scripting-dependencies-maven")) { isTransitive = false } - embedded(project(":kotlin-scripting-dependencies")) { isTransitive = false } embedded("org.apache.maven.resolver:maven-resolver-connector-basic:1.9.2") embedded("org.apache.maven.resolver:maven-resolver-transport-file:1.9.2") @@ -29,11 +38,15 @@ dependencies { embedded("org.apache.maven:maven-core:3.8.7") embedded("org.apache.maven.wagon:wagon-http:3.5.3") embedded("commons-io:commons-io:2.11.0") + + testImplementation(commonDependency("junit")) + testRuntimeOnly("org.slf4j:slf4j-nop:1.7.36") + testImplementation(project(":kotlin-scripting-dependencies-maven-all")) } sourceSets { "main" {} - "test" {} + "test" { projectDefault() } } publish() @@ -114,6 +127,7 @@ val proguard by task { javaLauncher.set(project.getToolchainLauncherFor(JdkMajorVersion.JDK_1_8)) + libraryjars(mapOf("filter" to "!META-INF/versions/**"), proguardLibraryJars) libraryjars( files( javaLauncher.map { diff --git a/libraries/scripting/dependencies-maven-all/test/org/jetbrains/kotlin/script/experimental/test/MavenAllResolverTest.kt b/libraries/scripting/dependencies-maven-all/test/org/jetbrains/kotlin/script/experimental/test/MavenAllResolverTest.kt new file mode 100644 index 00000000000..63292055fc2 --- /dev/null +++ b/libraries/scripting/dependencies-maven-all/test/org/jetbrains/kotlin/script/experimental/test/MavenAllResolverTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.script.experimental.test + +import org.junit.Test +import kotlin.script.experimental.dependencies.RepositoryCoordinates +import kotlin.script.experimental.dependencies.impl.makeExternalDependenciesResolverOptions +import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver + +class MavenAllResolverTest { + + @Test + fun testProguardedApiWithDefaultValues() { + // tests that API methods with default values are accessible with proguarded jar, see KT-60862 for details + + data class Repository(val id: String, val url: String, val user: String = "", val password: String = "") + + class DependencyResolver(private val customRepo: Repository) { + init { + // NOTE: due to the non-standard dependencies, some code could be red in IDE + MavenDependenciesResolver().apply { + val options = mutableMapOf() + addRepository( + RepositoryCoordinates(customRepo.url), + makeExternalDependenciesResolverOptions(options), +// null // left here to show that avoiding the default values is a workaround for KT-60862 + ) + } + } + } + + DependencyResolver( + Repository("id", "http://example.org/repository") + ) + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-main-kts/build.gradle.kts b/libraries/tools/kotlin-main-kts/build.gradle.kts index 9148a71b400..326866089da 100644 --- a/libraries/tools/kotlin-main-kts/build.gradle.kts +++ b/libraries/tools/kotlin-main-kts/build.gradle.kts @@ -38,6 +38,7 @@ dependencies { embedded(project(":kotlin-scripting-common")) { isTransitive = false } embedded(project(":kotlin-scripting-jvm")) { isTransitive = false } embedded(project(":kotlin-scripting-jvm-host-unshaded")) { isTransitive = false } + embedded(project(":kotlin-scripting-dependencies")) { isTransitive = false } embedded(project(":kotlin-scripting-dependencies-maven-all")) { isTransitive = false } embedded("org.slf4j:slf4j-api:1.7.36") embedded("org.slf4j:slf4j-simple:1.7.36") diff --git a/repo/artifacts-tests/src/test/resources/org/jetbrains/kotlin/kotlin-scripting-dependencies-maven-all/kotlin-scripting-dependencies-maven-all.pom b/repo/artifacts-tests/src/test/resources/org/jetbrains/kotlin/kotlin-scripting-dependencies-maven-all/kotlin-scripting-dependencies-maven-all.pom index cf847ede339..29502e3e9af 100644 --- a/repo/artifacts-tests/src/test/resources/org/jetbrains/kotlin/kotlin-scripting-dependencies-maven-all/kotlin-scripting-dependencies-maven-all.pom +++ b/repo/artifacts-tests/src/test/resources/org/jetbrains/kotlin/kotlin-scripting-dependencies-maven-all/kotlin-scripting-dependencies-maven-all.pom @@ -25,4 +25,12 @@ scm:git:https://github.com/JetBrains/kotlin.git https://github.com/JetBrains/kotlin + + + org.jetbrains.kotlin + kotlin-scripting-dependencies + ArtifactsTest.version + compile + +