Skip to content

Commit

Permalink
Only add BuildTool marker to Maven Wrapper and pom.xml files
Browse files Browse the repository at this point in the history
Fixes #649
  • Loading branch information
timtebeek committed Jan 24, 2024
1 parent d323f46 commit f71d7d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
39 changes: 20 additions & 19 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, @Nullable X
Set<Path> 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()));
}

Expand Down Expand Up @@ -200,23 +201,24 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, @Nullable X

// Collect any additional files that were not parsed above.
int sourcesParsedBefore = alreadyParsed.size();
Stream<SourceFile> parsedResourceFiles;

// Always parse Maven wrapper files, such that UpdateMavenWrapper can use the version information in BuildTool.
Stream<SourceFile> 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);
Expand Down Expand Up @@ -292,7 +294,6 @@ public List<Marker> 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(),
Expand Down Expand Up @@ -351,8 +352,8 @@ private Stream<SourceFile> processMainSources(
List<Marker> 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<SourceFile> sourceFiles = Stream.concat(parsedJava, parsedKotlin)
.filter(s -> !s.getSourcePath().startsWith(buildDirectory))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Marker> 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,
Expand All @@ -37,9 +50,5 @@ void givenNoJavaVersionInformationExistsInMavenThenJavaSpecificationVersionShoul
false,
true
);
List<Marker> 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"));
}
}

0 comments on commit f71d7d4

Please sign in to comment.