Skip to content

Commit 99de93b

Browse files
Aleksei.CherepanovSpace Team
Aleksei.Cherepanov
authored and
Space Team
committed
[Maven] Kotlin compiler plugin should respect model's compile source roots
#KT-13995 Fixed Merge-request: KT-MR-8236 Merged-by: Aleksei Cherepanov <[email protected]>
1 parent e2640b4 commit 99de93b

File tree

9 files changed

+168
-3
lines changed

9 files changed

+168
-3
lines changed

libraries/tools/kotlin-maven-plugin-test/src/it/test-enable-extensions-kapt-allopen/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<junit.version>4.13.1</junit.version>
1414
<main.class>coffee.CoffeeApp</main.class>
1515
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16-
<kotlin.version>1.8.255-SNAPSHOT</kotlin.version>
1716
</properties>
1817

1918
<dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>org.jetbrains.kotlin</groupId>
9+
<artifactId>test-respect-compile-source-root-app</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.jetbrains.kotlin</groupId>
19+
<artifactId>kotlin-stdlib</artifactId>
20+
<version>${kotlin.version}</version>
21+
</dependency>
22+
</dependencies>
23+
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.jetbrains.kotlin</groupId>
28+
<artifactId>add-custom-sourceRoots-maven-plugin</artifactId>
29+
<version>0.0.1-SNAPSHOT</version>
30+
<executions>
31+
<execution>
32+
<goals>
33+
<goal>add-custom-sourceRoots</goal>
34+
</goals>
35+
</execution>
36+
</executions>
37+
38+
</plugin>
39+
<plugin>
40+
<artifactId>kotlin-maven-plugin</artifactId>
41+
<groupId>org.jetbrains.kotlin</groupId>
42+
<version>${kotlin.version}</version>
43+
<executions>
44+
<execution>
45+
<id>compile</id>
46+
<goals>
47+
<goal>compile</goal>
48+
</goals>
49+
<configuration>
50+
<sourceDirs>
51+
<sourceDir>src/main/kotlin</sourceDir>
52+
</sourceDirs>
53+
</configuration>
54+
</execution>
55+
</executions>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fun foo(){
2+
println("1")
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.buildResult = failure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>org.jetbrains.kotlin</groupId>
7+
<artifactId>add-custom-sourceRoots-maven-plugin</artifactId>
8+
<packaging>maven-plugin</packaging>
9+
<version>0.0.1-SNAPSHOT</version>
10+
11+
<name>add-custom-sourceRoots-plugin Maven Mojo</name>
12+
<url>http://maven.apache.org</url>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.apache.maven</groupId>
22+
<artifactId>maven-plugin-api</artifactId>
23+
<version>${maven.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.maven.plugin-tools</groupId>
27+
<artifactId>maven-plugin-annotations</artifactId>
28+
<version>3.7.0</version>
29+
<scope>provided</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.apache.maven</groupId>
33+
<artifactId>maven-core</artifactId>
34+
<version>${maven.version}</version>
35+
</dependency>
36+
</dependencies>
37+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.apache.maven.model.Dependency;
2+
import org.apache.maven.plugin.AbstractMojo;
3+
import org.apache.maven.plugin.MojoExecutionException;
4+
import org.apache.maven.plugins.annotations.LifecyclePhase;
5+
import org.apache.maven.plugins.annotations.Mojo;
6+
import org.apache.maven.plugins.annotations.Parameter;
7+
import org.apache.maven.project.MavenProject;
8+
9+
import java.io.*;
10+
import java.nio.file.Files;
11+
import java.util.List;
12+
13+
@Mojo(name = "add-custom-sourceRoots", defaultPhase = LifecyclePhase.COMPILE)
14+
public class AddCustomSourceRootsMojo
15+
extends AbstractMojo {
16+
@Parameter(defaultValue = "${project}", required = true, readonly = true)
17+
public MavenProject project;
18+
19+
public void execute()
20+
throws MojoExecutionException {
21+
File targetGeneratedFolder = new File(project.getBasedir().getAbsolutePath(), "target/gen");
22+
if (!targetGeneratedFolder.exists()) {
23+
if (!targetGeneratedFolder.mkdirs()) {
24+
throw new MojoExecutionException("Failed to create target directory.");
25+
}
26+
}
27+
File generatedKtFile = new File(targetGeneratedFolder, "new.kt");
28+
29+
try {
30+
String str = "fun foo(){\n" +
31+
" println(\"2\")\n" +
32+
"}";
33+
BufferedWriter writer = new BufferedWriter(new FileWriter(generatedKtFile));
34+
writer.write(str);
35+
writer.close();
36+
} catch (IOException e) {
37+
throw new MojoExecutionException(e.getMessage());
38+
}
39+
40+
getLog().info("path: " + generatedKtFile.getAbsolutePath());
41+
project.addCompileSourceRoot(targetGeneratedFolder.getAbsolutePath());
42+
getLog().info("CompileSourceRoots: " + project.getCompileSourceRoots().toString());
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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-respect-compile-source-root</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<modules>
11+
<module>myPlugin</module>
12+
<module>app</module>
13+
</modules>
14+
<packaging>pom</packaging>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source(new File(basedir, "../../../verify-common.bsh").getAbsolutePath());
2+
3+
assertBuildLogHasLine("[INFO] BUILD FAILURE");
4+
assertBuildLogHasLineThatContains("Conflicting overloads:");

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
7474
private boolean multiPlatform = false;
7575

7676
protected List<String> getSourceFilePaths() {
77-
if (sourceDirs != null && !sourceDirs.isEmpty()) return sourceDirs;
78-
return project.getCompileSourceRoots();
77+
List<String> list = new ArrayList<>();
78+
if (sourceDirs != null && !sourceDirs.isEmpty()) list.addAll(sourceDirs);
79+
list.addAll(project.getCompileSourceRoots());
80+
return list;
7981
}
8082

8183
@NotNull

0 commit comments

Comments
 (0)