Skip to content

Commit

Permalink
Add option to parse additional project resources (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden authored Oct 4, 2023
1 parent ababec4 commit 718abb3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ protected LargeSourceSet loadSourceSet(Path repositoryRoot, Environment env, Exe
List<NamedStyles> 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<SourceFile> sourceFiles = projectParser.listSourceFiles(project, styles, ctx);
List<SourceFile> sourceFileList = sourcesWithAutoDetectedStyles(sourceFiles);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> exclusions, Collection<String> 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<String> exclusions, Collection<String> plainTextMasks, int sizeThresholdMb, MavenSession session, SettingsDecrypter settingsDecrypter, boolean runPerSubmodule, boolean parseAdditionalResources) {
this.logger = logger;
this.baseDir = baseDir;
this.pomCacheEnabled = pomCacheEnabled;
Expand All @@ -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<SourceFile> listSourceFiles(MavenProject mavenProject, List<NamedStyles> styles,
Expand Down Expand Up @@ -184,12 +186,15 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, @Nullable X
}
return sourceFile;
}).filter(Objects::nonNull);
//Collect any additional files that were not parsed above.
int sourcesParsedBefore = alreadyParsed.size();
Stream<SourceFile> 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<SourceFile> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<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"));
Expand Down

0 comments on commit 718abb3

Please sign in to comment.