From 07d20c6ee158b0dc31451a579cdbd42851801e54 Mon Sep 17 00:00:00 2001 From: "Aleksei.Cherepanov" Date: Thu, 20 Apr 2023 10:14:32 +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-9663 Merged-by: Aleksei Cherepanov --- .../pom.xml | 104 ++++++++++++++++++ .../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 | 3 +- 6 files changed, 126 insertions(+), 1 deletion(-) 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..ceedbcdebed --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/java9/test-sourceRootsRegisteredSeveralTimes/pom.xml @@ -0,0 +1,104 @@ + + + 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 + + ${java.version} + ${java.version} + + + + 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..70eb59469aa 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 @@ -41,6 +41,7 @@ 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; @@ -82,7 +83,7 @@ public abstract class KotlinCompileMojoBase e List list = new ArrayList<>(); if (sourceDirs != null && !sourceDirs.isEmpty()) list.addAll(sourceDirs); list.addAll(project.getCompileSourceRoots()); - return list; + return list.stream().distinct().collect(Collectors.toList()); } @NotNull