Skip to content

Commit 92c7d49

Browse files
Aleksei.CherepanovSpace Team
Aleksei.Cherepanov
authored and
Space Team
committed
[Maven] Filter duplicated source roots to avoid multiple module declarations exception
After fixing of KT-13995 (99de93b) 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 <[email protected]> (cherry picked from commit 803a110)
1 parent 035172c commit 92c7d49

File tree

6 files changed

+129
-4
lines changed

6 files changed

+129
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.jetbrains.kotlin</groupId>
8+
<artifactId>test-sourceRootsRegisteredSeveralTimes</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.13.1</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.jetbrains.kotlin</groupId>
19+
<artifactId>kotlin-stdlib</artifactId>
20+
<version>${kotlin.version}</version>
21+
</dependency>
22+
</dependencies>
23+
24+
<properties>
25+
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
26+
<maven.compiler.source>17</maven.compiler.source>
27+
<maven.compiler.target>17</maven.compiler.target>
28+
<maven.compiler.parameters>true</maven.compiler.parameters>
29+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30+
</properties>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<artifactId>kotlin-maven-plugin</artifactId>
36+
<groupId>org.jetbrains.kotlin</groupId>
37+
<version>${kotlin.version}</version>
38+
<executions>
39+
<execution>
40+
<id>compile</id>
41+
<goals>
42+
<goal>compile</goal>
43+
</goals>
44+
<configuration>
45+
<sourceDirs>
46+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
47+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
48+
</sourceDirs>
49+
</configuration>
50+
</execution>
51+
<execution>
52+
<id>test-compile</id>
53+
<goals>
54+
<goal>test-compile</goal>
55+
</goals>
56+
<configuration>
57+
<sourceDirs>
58+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
59+
<sourceDir>${project.basedir}/src/test/java</sourceDir>
60+
</sourceDirs>
61+
</configuration>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<version>3.10.1</version>
69+
<configuration>
70+
<source>1.9</source>
71+
<target>1.9</target>
72+
</configuration>
73+
<executions>
74+
<execution>
75+
<id>default-compile</id>
76+
<phase>none</phase>
77+
</execution>
78+
<execution>
79+
<id>default-testCompile</id>
80+
<phase>none</phase>
81+
</execution>
82+
<execution>
83+
<id>java-compile</id>
84+
<phase>compile</phase>
85+
<goals>
86+
<goal>compile</goal>
87+
</goals>
88+
</execution>
89+
<execution>
90+
<id>java-test-compile</id>
91+
<phase>test-compile</phase>
92+
<goals>
93+
<goal>testCompile</goal>
94+
</goals>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package foo.bar;
2+
3+
public class Foo {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module foo.bar {
2+
exports foo.bar;
3+
4+
5+
requires kotlin.stdlib;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package koo.bar
2+
3+
class Koo {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java.io.*;
2+
3+
File file = new File(basedir, "target/test-sourceRootsRegisteredSeveralTimes-1.0-SNAPSHOT.jar");
4+
if (!file.exists() || !file.isFile()) {
5+
throw new FileNotFoundException("Could not find generated JAR: " + file);
6+
}

libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import org.jetbrains.kotlin.config.Services;
3636

3737
import java.io.File;
38+
import java.io.IOException;
3839
import java.lang.reflect.Field;
3940
import java.util.*;
4041
import java.util.regex.Matcher;
4142
import java.util.regex.Pattern;
43+
import java.util.stream.Collectors;
4244

4345
import static org.jetbrains.kotlin.maven.Util.joinArrays;
4446

@@ -74,10 +76,12 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
7476
private boolean multiPlatform = false;
7577

7678
protected List<String> getSourceFilePaths() {
77-
List<String> list = new ArrayList<>();
78-
if (sourceDirs != null && !sourceDirs.isEmpty()) list.addAll(sourceDirs);
79-
list.addAll(project.getCompileSourceRoots());
80-
return list;
79+
List<String> sourceFilePaths = new ArrayList<>();
80+
if (sourceDirs != null && !sourceDirs.isEmpty()) sourceFilePaths.addAll(sourceDirs);
81+
sourceFilePaths.addAll(project.getCompileSourceRoots());
82+
83+
return sourceFilePaths.stream().map(path -> new File(path).toPath().normalize().toString())
84+
.distinct().collect(Collectors.toList());
8185
}
8286

8387
@NotNull

0 commit comments

Comments
 (0)