Skip to content

Commit

Permalink
[Maven] Filter duplicated source roots to avoid multiple module decla…
Browse files Browse the repository at this point in the history
…rations 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 <aleksei.cherepanov@jetbrains.com>
(cherry picked from commit 803a110)
  • Loading branch information
Aleksei.Cherepanov authored and qodana-bot committed Apr 22, 2023
1 parent 035172c commit 92c7d49
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.jetbrains.kotlin</groupId>
<artifactId>test-sourceRootsRegisteredSeveralTimes</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>

<properties>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.parameters>true</maven.compiler.parameters>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package foo.bar;

public class Foo {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module foo.bar {
exports foo.bar;


requires kotlin.stdlib;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package koo.bar

class Koo {
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
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;

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

protected List<String> getSourceFilePaths() {
List<String> list = new ArrayList<>();
if (sourceDirs != null && !sourceDirs.isEmpty()) list.addAll(sourceDirs);
list.addAll(project.getCompileSourceRoots());
return list;
List<String> 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
Expand Down

0 comments on commit 92c7d49

Please sign in to comment.