Skip to content

Commit

Permalink
Make rewrite-maven-plugin search for Kotlin source files in src/*/jav…
Browse files Browse the repository at this point in the history
…a if src/*/kotlin does not exist (#937)

* add test case

which fails initially because 0 kotlin source files will be found in the non-existent src/main/kotlin

* minor cleanup

* intentionally mess up formatting

* #936 in case of missing src/*/kotlin fall back to scanning for Kotlin sources in src/*/java instead

* #936 rename variable in processTestSources more aptly to kotlinTestSourceDir (was kotlinSourceDir)

* #936 refactor listKotlinSources()

* #936 also verify that Kotlin source files are found unter src/test/java

* #936 fix debug log message

* #936 separate tests of src/main/java and src/test/java into two independent testcases

* #936 removed @NotNull annotation and annotated package as @NullMarked

* polishing with best practices subset

---------

Co-authored-by: Merlin Bögershausen <[email protected]>
  • Loading branch information
reisners and MBoegers authored Jan 28, 2025
1 parent 6e8dac7 commit e572785
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 13 deletions.
25 changes: 13 additions & 12 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private SourceFile logParseErrors(SourceFile source) {
source.getMarkers().findFirst(ParseExceptionResult.class).ifPresent(e -> {
if (firstWarningLogged.compareAndSet(false, true)) {
logger.warn("There were problems parsing some source files" +
(mavenSession.getRequest().isShowErrors() ? "" : ", run with --errors to see full stack traces"));
(mavenSession.getRequest().isShowErrors() ? "" : ", run with --errors to see full stack traces"));
}
logger.warn("There were problems parsing " + source.getSourcePath());
if (mavenSession.getRequest().isShowErrors()) {
Expand Down Expand Up @@ -388,16 +388,16 @@ private Stream<SourceFile> processMainSources(
// Some annotation processors output generated sources to the /target directory. These are added for parsing but
// should be filtered out of the final SourceFile list.
List<Path> generatedSourcePaths = listJavaSources(mavenProject.getBasedir().toPath().resolve(mavenProject.getBuild().getDirectory()));
String mavenSourceDirectory = mavenProject.getBuild().getSourceDirectory();
List<Path> mainJavaSources = Stream.concat(
generatedSourcePaths.stream(),
listJavaSources(mavenProject.getBasedir().toPath().resolve(mavenProject.getBuild().getSourceDirectory())).stream()
listJavaSources(mavenProject.getBasedir().toPath().resolve(mavenSourceDirectory)).stream()
).collect(toList());

alreadyParsed.addAll(mainJavaSources);

// scan Kotlin files
String kotlinSourceDir = getKotlinDirectory(mavenProject.getBuild().getSourceDirectory());
List<Path> mainKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList();
List<Path> mainKotlinSources = listKotlinSources(mavenProject, mavenSourceDirectory);
alreadyParsed.addAll(mainKotlinSources);

logInfo(mavenProject, "Parsing source files");
Expand Down Expand Up @@ -465,8 +465,8 @@ private Stream<SourceFile> processTestSources(
alreadyParsed.addAll(testJavaSources);

// scan Kotlin files
String kotlinSourceDir = getKotlinDirectory(mavenProject.getBuild().getTestSourceDirectory());
List<Path> testKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList();
String mavenTestSourceDirectory = mavenProject.getBuild().getTestSourceDirectory();
List<Path> testKotlinSources = listKotlinSources(mavenProject, mavenTestSourceDirectory);
alreadyParsed.addAll(testKotlinSources);

Stream<SourceFile> parsedJava = Stream.empty();
Expand All @@ -478,7 +478,7 @@ private Stream<SourceFile> processTestSources(
Stream<SourceFile> parsedKotlin = Stream.empty();
if (!testKotlinSources.isEmpty()) {
parsedKotlin = kotlinParserBuilder.build().parse(testKotlinSources, baseDir, ctx);
logDebug(mavenProject, "Scanned " + testKotlinSources.size() + " kotlin source files in main scope.");
logDebug(mavenProject, "Scanned " + testKotlinSources.size() + " kotlin source files in test scope.");
}

List<Marker> markers = new ArrayList<>(projectProvenance);
Expand All @@ -493,6 +493,11 @@ private Stream<SourceFile> processTestSources(
.map(addProvenance(baseDir, markers, null));
}

private List<Path> listKotlinSources(MavenProject mavenProject, String fallbackSourceDirectory) throws MojoExecutionException {
String kotlinSourceDir = getKotlinDirectory(fallbackSourceDirectory);
return listSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir != null ? kotlinSourceDir : fallbackSourceDirectory), ".kt");
}

private @Nullable String getKotlinDirectory(@Nullable String sourceDirectory) {
if (sourceDirectory == null) {
return null;
Expand Down Expand Up @@ -582,7 +587,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
throw new MojoFailureException(
mavenProject,
"Failed to parse or resolve the Maven POM file or one of its dependencies; " +
"We can not reliably continue without this information.",
"We can not reliably continue without this information.",
parseExceptionResult.get().getMessage());
}
projectMap.put(mavenProject, (Xml.Document) document);
Expand Down Expand Up @@ -760,10 +765,6 @@ private static List<Path> listJavaSources(Path sourceDirectory) throws MojoExecu
return listSources(sourceDirectory, ".java");
}

private static List<Path> listKotlinSources(Path sourceDirectory) throws MojoExecutionException {
return listSources(sourceDirectory, ".kt");
}

private static List<Path> listSources(Path sourceDirectory, String extension) throws MojoExecutionException {
if (!Files.exists(sourceDirectory)) {
return emptyList();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/openrewrite/maven/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@org.jspecify.annotations.NullMarked
package org.openrewrite.maven;
53 changes: 53 additions & 0 deletions src/test/java/org/openrewrite/maven/KotlinIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.maven;

import com.soebes.itf.jupiter.extension.*;
import com.soebes.itf.jupiter.maven.MavenExecutionResult;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;

@MavenJupiterExtension
@MavenOption(MavenCLIOptions.NO_TRANSFER_PROGRESS)
@MavenOption(MavenCLIExtra.MUTE_PLUGIN_VALIDATION_WARNING)
@MavenOption(MavenCLIOptions.VERBOSE)
@DisabledOnOs(OS.WINDOWS)
@MavenGoal("install")
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:run")
class KotlinIT {
@MavenTest
void kotlin_in_src_main_java(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.debug()
.anySatisfy(line -> assertThat(line).contains("Scanned 1 kotlin source files in main scope."))
.anySatisfy(line -> assertThat(line).contains("org.openrewrite.kotlin.format.AutoFormat"));
}

@MavenTest
void kotlin_in_src_main_test(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.debug()
.anySatisfy(line -> assertThat(line).contains("Scanned 1 kotlin source files in test scope."))
.anySatisfy(line -> assertThat(line).contains("org.openrewrite.kotlin.format.AutoFormat"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.openrewrite.maven</groupId>
<artifactId>basic_kotlin_project</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>KotlinIT#basic_kotlin_project</name>

<properties>
<kotlin.version>1.9.10</kotlin.version>
<jdk.version>17</jdk.version>
<java.version>${jdk.version}</java.version>
<maven.compiler.source>${jdk.version}</maven.compiler.source>
<maven.compiler.target>${jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</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>

<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.kotlin.format.AutoFormat</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-all</artifactId>
<version>1.3.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

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

<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sample

class MyClass {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.openrewrite.maven</groupId>
<artifactId>basic_kotlin_project</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>KotlinIT#basic_kotlin_project</name>

<properties>
<kotlin.version>1.9.10</kotlin.version>
<jdk.version>17</jdk.version>
<java.version>${jdk.version}</java.version>
<maven.compiler.source>${jdk.version}</maven.compiler.source>
<maven.compiler.target>${jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</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>

<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.kotlin.format.AutoFormat</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-all</artifactId>
<version>1.3.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

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

<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sample

class MyTest {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sample;

import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;

public class EmptyBlockSample {
Expand Down

0 comments on commit e572785

Please sign in to comment.