Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rewrite-maven-plugin search for Kotlin source files in src/*/java if src/*/kotlin does not exist #937

Merged
merged 12 commits into from
Jan 28, 2025
Merged
24 changes: 13 additions & 11 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.jetbrains.annotations.NotNull;
MBoegers marked this conversation as resolved.
Show resolved Hide resolved
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ParseExceptionResult;
Expand Down Expand Up @@ -242,7 +243,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 +389,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 +466,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 @@ -493,6 +494,11 @@ private Stream<SourceFile> processTestSources(
.map(addProvenance(baseDir, markers, null));
}

private @NotNull 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 +588,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 +766,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
42 changes: 42 additions & 0 deletions src/test/java/org/openrewrite/maven/KotlinIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 {
MBoegers marked this conversation as resolved.
Show resolved Hide resolved
@MavenTest
void basic_kotlin_project(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"));
}
}
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 {

}
Loading