From 803a11003f4fe52f335700b6de18eb42de31f3a2 Mon Sep 17 00:00:00 2001 From: "Aleksei.Cherepanov" Date: Fri, 21 Apr 2023 12:07:30 +0000 Subject: [PATCH] [Maven] Filter duplicated source roots to avoid multiple module declarations exception After fixing of KT-13995 (99de93bb) there is a case when several maven plugins register the same source roots twice, which leads to the Kotlin compiler exception "Too many source module declarations found". kotlin-maven-plugin should take care of what it passes to the Kotlin compiler to avoid it #KT-58048 Fixed Merge-request: KT-MR-9716 Merged-by: Aleksei Cherepanov --- .../pom.xml | 101 ++++++++++++++++++ .../src/main/java/foo/bar/Foo.java | 4 + .../src/main/java/module-info.java | 6 ++ .../src/main/kotlin/koo/bar/Koo.kt | 4 + .../verify.bsh | 6 ++ .../kotlin/maven/KotlinCompileMojoBase.java | 12 ++- 6 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/pom.xml create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/foo/bar/Foo.java create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/module-info.java create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/kotlin/koo/bar/Koo.kt create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/verify.bsh diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/pom.xml b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/pom.xml new file mode 100644 index 00000000000..d69aabf692e --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + + org.jetbrains.kotlin + test-sourceRootsRegisteredSeveralTimes + 1.0-SNAPSHOT + + + + junit + junit + 4.13.1 + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + + + true + 17 + 17 + true + UTF-8 + + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + compile + + compile + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + + + + test-compile + + test-compile + + + + ${project.basedir}/src/test/kotlin + ${project.basedir}/src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + 1.9 + 1.9 + + + + default-compile + none + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + + + + + diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/foo/bar/Foo.java b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/foo/bar/Foo.java new file mode 100644 index 00000000000..4fa5201b7e1 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/foo/bar/Foo.java @@ -0,0 +1,4 @@ +package foo.bar; + +public class Foo { +} diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/module-info.java b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/module-info.java new file mode 100644 index 00000000000..cb68639ee71 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/java/module-info.java @@ -0,0 +1,6 @@ +module foo.bar { + exports foo.bar; + + + requires kotlin.stdlib; +} diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/kotlin/koo/bar/Koo.kt b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/kotlin/koo/bar/Koo.kt new file mode 100644 index 00000000000..9b6f13d88b5 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/src/main/kotlin/koo/bar/Koo.kt @@ -0,0 +1,4 @@ +package koo.bar + +class Koo { +} diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/verify.bsh b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/verify.bsh new file mode 100644 index 00000000000..573d8510644 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/verify.bsh @@ -0,0 +1,6 @@ +import java.io.*; + +File file = new File(basedir, "target/test-sourceRootsRegisteredSeveralTimes-1.0-SNAPSHOT.jar"); +if (!file.exists() || !file.isFile()) { + throw new FileNotFoundException("Could not find generated JAR: " + file); +} diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java index 5ae03c7434e..2bfe90d3e7d 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java @@ -37,10 +37,12 @@ import org.jetbrains.kotlin.config.KotlinCompilerVersion; import org.jetbrains.kotlin.config.Services; import java.io.File; +import java.io.IOException; import java.lang.reflect.Field; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static org.jetbrains.kotlin.maven.Util.joinArrays; @@ -79,10 +81,12 @@ public abstract class KotlinCompileMojoBase e private boolean multiPlatform = false; protected List getSourceFilePaths() { - List list = new ArrayList<>(); - if (sourceDirs != null && !sourceDirs.isEmpty()) list.addAll(sourceDirs); - list.addAll(project.getCompileSourceRoots()); - return list; + List sourceFilePaths = new ArrayList<>(); + if (sourceDirs != null && !sourceDirs.isEmpty()) sourceFilePaths.addAll(sourceDirs); + sourceFilePaths.addAll(project.getCompileSourceRoots()); + + return sourceFilePaths.stream().map(path -> new File(path).toPath().normalize().toString()) + .distinct().collect(Collectors.toList()); } @NotNull