From 718abb3315e16b20170559f1bc0dfa8ce0adbc65 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 4 Oct 2023 14:45:17 +0200 Subject: [PATCH] Add option to parse additional project resources (#639) --- .../maven/AbstractRewriteMojo.java | 2 +- .../openrewrite/maven/CycloneDxBomMojo.java | 2 +- .../maven/MavenMojoProjectParser.java | 19 ++++++++++++------- .../maven/MavenMojoProjectParserTest.java | 18 ++++++++++++++++-- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java index 14b8a734..c38b614b 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java @@ -252,7 +252,7 @@ protected LargeSourceSet loadSourceSet(Path repositoryRoot, Environment env, Exe List styles = loadStyles(project, env); //Parse and collect source files from each project in the maven session. - MavenMojoProjectParser projectParser = new MavenMojoProjectParser(getLog(), repositoryRoot, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter, runPerSubmodule); + MavenMojoProjectParser projectParser = new MavenMojoProjectParser(getLog(), repositoryRoot, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter, runPerSubmodule, true); Stream sourceFiles = projectParser.listSourceFiles(project, styles, ctx); List sourceFileList = sourcesWithAutoDetectedStyles(sourceFiles); diff --git a/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java b/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java index 14c6bcc9..fa62ad29 100644 --- a/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java +++ b/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java @@ -47,7 +47,7 @@ public class CycloneDxBomMojo extends AbstractRewriteMojo { public void execute() throws MojoExecutionException { ExecutionContext ctx = executionContext(); Path baseDir = getBuildRoot(); - Xml.Document maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter, runPerSubmodule).parseMaven(project, Collections.emptyList(), ctx); + Xml.Document maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter, runPerSubmodule, true).parseMaven(project, Collections.emptyList(), ctx); if (maven != null) { File cycloneDxBom = buildCycloneDxBom(maven); projectHelper.attachArtifact(project, "xml", "cyclonedx", cycloneDxBom); diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java index 5dc54482..1f00d1e7 100644 --- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java +++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java @@ -109,9 +109,10 @@ public class MavenMojoProjectParser { private final MavenSession mavenSession; private final SettingsDecrypter settingsDecrypter; private final boolean runPerSubmodule; + private final boolean parseAdditionalResources; @SuppressWarnings("BooleanParameter") - public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, @Nullable String pomCacheDirectory, RuntimeInformation runtime, boolean skipMavenParsing, Collection exclusions, Collection plainTextMasks, int sizeThresholdMb, MavenSession session, SettingsDecrypter settingsDecrypter, boolean runPerSubmodule) { + public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, @Nullable String pomCacheDirectory, RuntimeInformation runtime, boolean skipMavenParsing, Collection exclusions, Collection plainTextMasks, int sizeThresholdMb, MavenSession session, SettingsDecrypter settingsDecrypter, boolean runPerSubmodule, boolean parseAdditionalResources) { this.logger = logger; this.baseDir = baseDir; this.pomCacheEnabled = pomCacheEnabled; @@ -124,6 +125,7 @@ public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, this.mavenSession = session; this.settingsDecrypter = settingsDecrypter; this.runPerSubmodule = runPerSubmodule; + this.parseAdditionalResources = parseAdditionalResources; } public Stream listSourceFiles(MavenProject mavenProject, List styles, @@ -184,12 +186,15 @@ public Stream listSourceFiles(MavenProject mavenProject, @Nullable X } return sourceFile; }).filter(Objects::nonNull); - //Collect any additional files that were not parsed above. - int sourcesParsedBefore = alreadyParsed.size(); - Stream parsedResourceFiles = rp.parse(mavenProject.getBasedir().toPath(), alreadyParsed) - .map(addProvenance(baseDir, projectProvenance, null)); - logDebug(mavenProject, "Parsed " + (alreadyParsed.size() - sourcesParsedBefore) + " additional files found within the project."); - sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles); + + // Collect any additional files that were not parsed above. + if (parseAdditionalResources) { + int sourcesParsedBefore = alreadyParsed.size(); + Stream parsedResourceFiles = rp.parse(mavenProject.getBasedir().toPath(), alreadyParsed) + .map(addProvenance(baseDir, projectProvenance, null)); + logDebug(mavenProject, "Parsed " + (alreadyParsed.size() - sourcesParsedBefore) + " additional files found within the project."); + sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles); + } // 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); diff --git a/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java b/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java index ccef3bf1..7d9f1916 100644 --- a/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java +++ b/src/test/java/org/openrewrite/maven/MavenMojoProjectParserTest.java @@ -10,9 +10,9 @@ import org.openrewrite.marker.Marker; import java.nio.file.Path; -import java.util.Collections; import java.util.List; +import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; /** @@ -22,7 +22,21 @@ 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(new SystemStreamLog(), dir, false, null, new DefaultRuntimeInformation(), false, Collections.EMPTY_LIST, Collections.EMPTY_LIST, -1, null, null, false); + MavenMojoProjectParser sut = new MavenMojoProjectParser( + new SystemStreamLog(), + dir, + false, + null, + new DefaultRuntimeInformation(), + false, + emptyList(), + emptyList(), + -1, + null, + null, + 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"));