[Maven] Kotlin compiler plugin should respect model's compile source roots

#KT-13995 Fixed

Merge-request: KT-MR-8236
Merged-by: Aleksei Cherepanov <aleksei.cherepanov@jetbrains.com>
This commit is contained in:
Aleksei.Cherepanov
2023-01-11 10:19:31 +00:00
committed by Space Team
parent e2640b4afa
commit 99de93bbd6
9 changed files with 168 additions and 3 deletions
@@ -13,7 +13,6 @@
<junit.version>4.13.1</junit.version>
<main.class>coffee.CoffeeApp</main.class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.8.255-SNAPSHOT</kotlin.version>
</properties>
<dependencies>
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>test-respect-compile-source-root-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>add-custom-sourceRoots-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>add-custom-sourceRoots</goal>
</goals>
</execution>
</executions>
</plugin>
<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>src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,3 @@
fun foo(){
println("1")
}
@@ -0,0 +1 @@
invoker.buildResult = failure
@@ -0,0 +1,37 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>add-custom-sourceRoots-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>add-custom-sourceRoots-plugin Maven Mojo</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,44 @@
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.*;
import java.nio.file.Files;
import java.util.List;
@Mojo(name = "add-custom-sourceRoots", defaultPhase = LifecyclePhase.COMPILE)
public class AddCustomSourceRootsMojo
extends AbstractMojo {
@Parameter(defaultValue = "${project}", required = true, readonly = true)
public MavenProject project;
public void execute()
throws MojoExecutionException {
File targetGeneratedFolder = new File(project.getBasedir().getAbsolutePath(), "target/gen");
if (!targetGeneratedFolder.exists()) {
if (!targetGeneratedFolder.mkdirs()) {
throw new MojoExecutionException("Failed to create target directory.");
}
}
File generatedKtFile = new File(targetGeneratedFolder, "new.kt");
try {
String str = "fun foo(){\n" +
" println(\"2\")\n" +
"}";
BufferedWriter writer = new BufferedWriter(new FileWriter(generatedKtFile));
writer.write(str);
writer.close();
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage());
}
getLog().info("path: " + generatedKtFile.getAbsolutePath());
project.addCompileSourceRoot(targetGeneratedFolder.getAbsolutePath());
getLog().info("CompileSourceRoots: " + project.getCompileSourceRoots().toString());
}
}
@@ -0,0 +1,16 @@
<?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-respect-compile-source-root</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>myPlugin</module>
<module>app</module>
</modules>
<packaging>pom</packaging>
</project>
@@ -0,0 +1,4 @@
source(new File(basedir, "../../../verify-common.bsh").getAbsolutePath());
assertBuildLogHasLine("[INFO] BUILD FAILURE");
assertBuildLogHasLineThatContains("Conflicting overloads:");
@@ -74,8 +74,10 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
private boolean multiPlatform = false;
protected List<String> getSourceFilePaths() {
if (sourceDirs != null && !sourceDirs.isEmpty()) return sourceDirs;
return project.getCompileSourceRoots();
List<String> list = new ArrayList<>();
if (sourceDirs != null && !sourceDirs.isEmpty()) list.addAll(sourceDirs);
list.addAll(project.getCompileSourceRoots());
return list;
}
@NotNull