diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java index 5660dc94..02b1b093 100644 --- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java +++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java @@ -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()) { @@ -388,16 +388,16 @@ private Stream 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 generatedSourcePaths = listJavaSources(mavenProject.getBasedir().toPath().resolve(mavenProject.getBuild().getDirectory())); + String mavenSourceDirectory = mavenProject.getBuild().getSourceDirectory(); List 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 mainKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList(); + List mainKotlinSources = listKotlinSources(mavenProject, mavenSourceDirectory); alreadyParsed.addAll(mainKotlinSources); logInfo(mavenProject, "Parsing source files"); @@ -465,8 +465,8 @@ private Stream processTestSources( alreadyParsed.addAll(testJavaSources); // scan Kotlin files - String kotlinSourceDir = getKotlinDirectory(mavenProject.getBuild().getTestSourceDirectory()); - List testKotlinSources = (kotlinSourceDir != null) ? listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir)) : Collections.emptyList(); + String mavenTestSourceDirectory = mavenProject.getBuild().getTestSourceDirectory(); + List testKotlinSources = listKotlinSources(mavenProject, mavenTestSourceDirectory); alreadyParsed.addAll(testKotlinSources); Stream parsedJava = Stream.empty(); @@ -478,7 +478,7 @@ private Stream processTestSources( Stream 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 markers = new ArrayList<>(projectProvenance); @@ -493,6 +493,11 @@ private Stream processTestSources( .map(addProvenance(baseDir, markers, null)); } + private List 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; @@ -582,7 +587,7 @@ public Map parseMaven(List 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); @@ -760,10 +765,6 @@ private static List listJavaSources(Path sourceDirectory) throws MojoExecu return listSources(sourceDirectory, ".java"); } - private static List listKotlinSources(Path sourceDirectory) throws MojoExecutionException { - return listSources(sourceDirectory, ".kt"); - } - private static List listSources(Path sourceDirectory, String extension) throws MojoExecutionException { if (!Files.exists(sourceDirectory)) { return emptyList(); diff --git a/src/main/java/org/openrewrite/maven/package-info.java b/src/main/java/org/openrewrite/maven/package-info.java index 932f61ce..64cbe28c 100644 --- a/src/main/java/org/openrewrite/maven/package-info.java +++ b/src/main/java/org/openrewrite/maven/package-info.java @@ -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; diff --git a/src/test/java/org/openrewrite/maven/KotlinIT.java b/src/test/java/org/openrewrite/maven/KotlinIT.java new file mode 100644 index 00000000..cb98a8c4 --- /dev/null +++ b/src/test/java/org/openrewrite/maven/KotlinIT.java @@ -0,0 +1,53 @@ +/* + * Copyright 2020 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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")); + } + +} diff --git a/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_java/pom.xml b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_java/pom.xml new file mode 100644 index 00000000..ac0434e4 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_java/pom.xml @@ -0,0 +1,111 @@ + + 4.0.0 + + org.openrewrite.maven + basic_kotlin_project + 1.0 + jar + KotlinIT#basic_kotlin_project + + + 1.9.10 + 17 + ${jdk.version} + ${jdk.version} + ${jdk.version} + UTF-8 + + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + process-sources + + compile + + + + ${project.basedir}/src/main/java + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + ${java.version} + ${java.version} + + + + default-compile + none + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + org.openrewrite.kotlin.format.AutoFormat + + + + + org.openrewrite.recipe + rewrite-all + 1.3.4 + + + + + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + + + + mavenCentral + https://repo1.maven.org/maven2/ + + + diff --git a/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_java/src/main/java/sample/MyClass.kt b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_java/src/main/java/sample/MyClass.kt new file mode 100644 index 00000000..00842354 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_java/src/main/java/sample/MyClass.kt @@ -0,0 +1,5 @@ + package sample + + class MyClass { + + } diff --git a/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_test/pom.xml b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_test/pom.xml new file mode 100644 index 00000000..ac0434e4 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_test/pom.xml @@ -0,0 +1,111 @@ + + 4.0.0 + + org.openrewrite.maven + basic_kotlin_project + 1.0 + jar + KotlinIT#basic_kotlin_project + + + 1.9.10 + 17 + ${jdk.version} + ${jdk.version} + ${jdk.version} + UTF-8 + + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + process-sources + + compile + + + + ${project.basedir}/src/main/java + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + ${java.version} + ${java.version} + + + + default-compile + none + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + org.openrewrite.kotlin.format.AutoFormat + + + + + org.openrewrite.recipe + rewrite-all + 1.3.4 + + + + + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + + + + mavenCentral + https://repo1.maven.org/maven2/ + + + diff --git a/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_test/src/test/java/sample/MyTest.kt b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_test/src/test/java/sample/MyTest.kt new file mode 100644 index 00000000..460bf796 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/KotlinIT/kotlin_in_src_main_test/src/test/java/sample/MyTest.kt @@ -0,0 +1,4 @@ +package sample + + class MyTest { + } diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteDryRunIT/recipe_order/src/main/java/sample/EmptyBlockSample.java b/src/test/resources-its/org/openrewrite/maven/RewriteDryRunIT/recipe_order/src/main/java/sample/EmptyBlockSample.java index 98161ca1..dcedad3a 100644 --- a/src/test/resources-its/org/openrewrite/maven/RewriteDryRunIT/recipe_order/src/main/java/sample/EmptyBlockSample.java +++ b/src/test/resources-its/org/openrewrite/maven/RewriteDryRunIT/recipe_order/src/main/java/sample/EmptyBlockSample.java @@ -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 {