diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java index 69cb92b8..77e07726 100644 --- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java +++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java @@ -165,7 +165,8 @@ public Stream listSourceFiles(MavenProject mavenProject, @Nullable X Set alreadyParsed = new HashSet<>(); if (maven != null) { - sourceFiles = Stream.of(maven); + sourceFiles = Stream.of(maven) + .map(xml -> xml.withMarkers(xml.getMarkers().addIfAbsent(buildTool))); alreadyParsed.add(baseDir.resolve(maven.getSourcePath())); } @@ -200,23 +201,24 @@ public Stream listSourceFiles(MavenProject mavenProject, @Nullable X // Collect any additional files that were not parsed above. int sourcesParsedBefore = alreadyParsed.size(); - Stream parsedResourceFiles; + + // Always parse Maven wrapper files, such that UpdateMavenWrapper can use the version information in BuildTool. + Stream parsedResourceFiles = Stream.of( + MavenWrapper.WRAPPER_BATCH_LOCATION, + MavenWrapper.WRAPPER_JAR_LOCATION, + MavenWrapper.WRAPPER_PROPERTIES_LOCATION, + MavenWrapper.WRAPPER_SCRIPT_LOCATION) + .flatMap(path -> rp.parse(mavenProject.getBasedir().toPath().resolve(path), alreadyParsed)) + .map(s -> s.withMarkers(s.getMarkers().addIfAbsent(buildTool))); + logDebug(mavenProject, "Parsed " + (alreadyParsed.size() - sourcesParsedBefore) + " Maven wrapper files found within the project."); + + // Parse any additional files found within the project if configured to do so. if (parseAdditionalResources) { - parsedResourceFiles = rp.parse(mavenProject.getBasedir().toPath(), alreadyParsed) - .map(addProvenance(baseDir, projectProvenance, null)); + parsedResourceFiles = Stream.concat(parsedResourceFiles, rp.parse(mavenProject.getBasedir().toPath(), alreadyParsed)); logDebug(mavenProject, "Parsed " + (alreadyParsed.size() - sourcesParsedBefore) + " additional files found within the project."); - } else { - // Only parse Maven wrapper files, such that UpdateMavenWrapper can use the version information. - parsedResourceFiles = Stream.of( - MavenWrapper.WRAPPER_BATCH_LOCATION, - MavenWrapper.WRAPPER_JAR_LOCATION, - MavenWrapper.WRAPPER_PROPERTIES_LOCATION, - MavenWrapper.WRAPPER_SCRIPT_LOCATION) - .flatMap(path -> rp.parse(mavenProject.getBasedir().toPath().resolve(path), alreadyParsed)) - .map(addProvenance(baseDir, projectProvenance, null)); - logDebug(mavenProject, "Parsed " + (alreadyParsed.size() - sourcesParsedBefore) + " Maven wrapper files found within the project."); - } - sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles); + } + sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles + .map(addProvenance(baseDir, projectProvenance, null))); // log parse errors here at the end, so that we don't log parse errors for files that were excluded return sourceFiles.map(this::logParseErrors); @@ -292,7 +294,6 @@ public List generateProvenance(MavenProject mavenProject) { buildEnvironment, gitProvenance(baseDir, buildEnvironment), OperatingSystemProvenance.current(), - buildTool, new JavaVersion(randomId(), javaRuntimeVersion, javaVendor, sourceCompatibility, targetCompatibility), new JavaProject(randomId(), mavenProject.getName(), new JavaProject.Publication( mavenProject.getGroupId(), @@ -351,8 +352,8 @@ private Stream processMainSources( List mainProjectProvenance = new ArrayList<>(projectProvenance); mainProjectProvenance.add(sourceSet("main", dependencies, typeCache)); - //Filter out any generated source files from the returned list, as we do not want to apply the recipe to the - //generated files. + // Filter out any generated source files from the returned list, as we do not want to apply the recipe to the + // generated files. Path buildDirectory = baseDir.relativize(Paths.get(mavenProject.getBuild().getDirectory())); Stream sourceFiles = Stream.concat(parsedJava, parsedKotlin) .filter(s -> !s.getSourcePath().startsWith(buildDirectory)) diff --git a/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java b/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java index 7d9f1916..acf64074 100644 --- a/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java +++ b/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.openrewrite.java.marker.JavaVersion; +import org.openrewrite.marker.BuildTool; import org.openrewrite.marker.Marker; import java.nio.file.Path; @@ -22,7 +23,19 @@ class MavenMojoProjectParserTest { @Test @DisplayName("Given No Java version information exists in Maven Then java.specification.version should be used") void givenNoJavaVersionInformationExistsInMavenThenJavaSpecificationVersionShouldBeUsed(@TempDir Path dir) { - MavenMojoProjectParser sut = new MavenMojoProjectParser( + MavenMojoProjectParser sut = getMavenMojoProjectParser(dir); + List markers = sut.generateProvenance(new MavenProject()); + JavaVersion marker = markers.stream().filter(JavaVersion.class::isInstance).map(JavaVersion.class::cast).findFirst().get(); + assertThat(marker.getSourceCompatibility()).isEqualTo(System.getProperty("java.specification.version")); + assertThat(marker.getTargetCompatibility()).isEqualTo(System.getProperty("java.specification.version")); + + // The build tool marker should not be present on just any source file, only on the pom.xml & wrapper files + // https://github.com/openrewrite/rewrite-maven-plugin/issues/649 + assertThat(markers).noneMatch(m -> m instanceof BuildTool); + } + + private static MavenMojoProjectParser getMavenMojoProjectParser(Path dir) { + return new MavenMojoProjectParser( new SystemStreamLog(), dir, false, @@ -37,9 +50,5 @@ void givenNoJavaVersionInformationExistsInMavenThenJavaSpecificationVersionShoul false, true ); - List markers = sut.generateProvenance(new MavenProject()); - JavaVersion marker = markers.stream().filter(JavaVersion.class::isInstance).map(JavaVersion.class::cast).findFirst().get(); - assertThat(marker.getSourceCompatibility()).isEqualTo(System.getProperty("java.specification.version")); - assertThat(marker.getTargetCompatibility()).isEqualTo(System.getProperty("java.specification.version")); } }