Skip to content

Commit

Permalink
Ensure that XmlParser doesn't try to re-parse pom.xml files that have…
Browse files Browse the repository at this point in the history
… already been parsed by MavenParser
  • Loading branch information
sambsnyd committed Oct 28, 2021
1 parent 16e3d42 commit 00dd505
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.16.0-SNAPSHOT</version>
<version>4.15.2-SNAPSHOT</version>
<name>rewrite-maven-plugin</name>

<packaging>maven-plugin</packaging>
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,8 @@ protected ResultsContainer listResults() throws MojoExecutionException {
}
}
ExecutionContext ctx = executionContext();
List<SourceFile> sourceFiles = new ArrayList<>();
MavenMojoProjectParser projectParser = new MavenMojoProjectParser(getLog(), baseDir, project, runtime);
Maven maven = projectParser.parseMaven(pomCacheEnabled, pomCacheDirectory, ctx);
if(maven != null) {
sourceFiles.add(maven);
}
sourceFiles.addAll(projectParser.listSourceFiles(styles, ctx));
MavenMojoProjectParser projectParser = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, project, runtime);
List<SourceFile> sourceFiles = projectParser.listSourceFiles(styles, ctx);

getLog().info("Running recipe(s)...");
List<Result> results = recipe.run(sourceFiles, ctx).stream()
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 @@ -31,7 +31,7 @@ public class CycloneDxBomMojo extends AbstractRewriteMojo {
public void execute() throws MojoExecutionException {
ExecutionContext ctx = executionContext();
Path baseDir = getBaseDir();
Maven maven = new MavenMojoProjectParser(getLog(), baseDir, project, runtime).parseMaven(pomCacheEnabled, pomCacheDirectory, ctx);
Maven maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, project, runtime).parseMaven(ctx);
File cycloneDxBom = buildCycloneDxBom(maven);
projectHelper.attachArtifact(project, "xml", "cyclonedx", cycloneDxBom);
}
Expand Down
135 changes: 75 additions & 60 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ public class MavenMojoProjectParser {
private final Path baseDir;
private final MavenProject mavenProject;
private final List<Marker> projectProvenance;
private final boolean pomCacheEnabled;
@Nullable private final String pomCacheDirectory;
@Nullable private List<Maven> poms = null;

public MavenMojoProjectParser(Log logger, Path baseDir, MavenProject mavenProject, RuntimeInformation runtime) {
public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, @Nullable String pomCacheDirectory, MavenProject mavenProject, RuntimeInformation runtime) {
this.logger = logger;
this.baseDir = baseDir;
this.mavenProject = mavenProject;
this.pomCacheEnabled = pomCacheEnabled;
this.pomCacheDirectory = pomCacheDirectory;

String javaRuntimeVersion = System.getProperty("java.runtime.version");
String javaVendor = System.getProperty("java.vm.vendor");
Expand Down Expand Up @@ -85,73 +90,79 @@ private GitProvenance gitProvenance(Path baseDir) {
}

@Nullable
public Maven parseMaven(boolean pomCacheEnabled, @Nullable String pomCacheDirectory, ExecutionContext ctx) {
public Maven parseMaven(ExecutionContext ctx) {
if(System.getProperty("skipMavenParsing") != null) {
return null;
}
List<Path> allPoms = new ArrayList<>();
allPoms.add(mavenProject.getFile().toPath());

// children
if (mavenProject.getCollectedProjects() != null) {
mavenProject.getCollectedProjects().stream()
.filter(collectedProject -> collectedProject != mavenProject)
.map(collectedProject -> collectedProject.getFile().toPath())
.forEach(allPoms::add);
}
if(poms == null) {
List<Path> allPoms = new ArrayList<>();
allPoms.add(mavenProject.getFile().toPath());

MavenProject parent = mavenProject.getParent();
while (parent != null && parent.getFile() != null) {
allPoms.add(parent.getFile().toPath());
parent = parent.getParent();
}
MavenParser.Builder mavenParserBuilder = MavenParser.builder()
.mavenConfig(baseDir.resolve(".mvn/maven.config"));

if (pomCacheEnabled) {
try {
if (pomCacheDirectory == null) {
//Default directory in the RocksdbMavenPomCache is ".rewrite-cache"
mavenParserBuilder.cache(new RocksdbMavenPomCache(Paths.get(System.getProperty("user.home"))));
} else {
mavenParserBuilder.cache(new RocksdbMavenPomCache(Paths.get(pomCacheDirectory)));
// children
if (mavenProject.getCollectedProjects() != null) {
mavenProject.getCollectedProjects().stream()
.filter(collectedProject -> collectedProject != mavenProject)
.map(collectedProject -> collectedProject.getFile().toPath())
.forEach(allPoms::add);
}

MavenProject parent = mavenProject.getParent();
while (parent != null && parent.getFile() != null) {
allPoms.add(parent.getFile().toPath());
parent = parent.getParent();
}
MavenParser.Builder mavenParserBuilder = MavenParser.builder()
.mavenConfig(baseDir.resolve(".mvn/maven.config"));

if (pomCacheEnabled) {
try {
if (pomCacheDirectory == null) {
//Default directory in the RocksdbMavenPomCache is ".rewrite-cache"
mavenParserBuilder.cache(new RocksdbMavenPomCache(Paths.get(System.getProperty("user.home"))));
} else {
mavenParserBuilder.cache(new RocksdbMavenPomCache(Paths.get(pomCacheDirectory)));
}
} catch (Exception e) {
logger.warn("Unable to initialize RocksdbMavenPomCache, falling back to InMemoryMavenPomCache");
logger.debug(e);
mavenParserBuilder.cache(new InMemoryMavenPomCache());
}
} catch (Exception e) {
logger.warn("Unable to initialize RocksdbMavenPomCache, falling back to InMemoryMavenPomCache");
logger.debug(e);
mavenParserBuilder.cache(new InMemoryMavenPomCache());
}
}

Path mavenSettings = Paths.get(System.getProperty("user.home")).resolve(".m2/settings.xml");
if (mavenSettings.toFile().exists()) {
MavenSettings settings = MavenSettings.parse(new Parser.Input(mavenSettings,
() -> {
try {
return Files.newInputStream(mavenSettings);
} catch (IOException e) {
logger.warn("Unable to load Maven settings from user home directory. Skipping.", e);
return null;
}
}),
ctx);
if (settings != null) {
new MavenExecutionContextView(ctx).setMavenSettings(settings);
if (settings.getActiveProfiles() != null) {
mavenParserBuilder.activeProfiles(settings.getActiveProfiles().getActiveProfiles().toArray(new String[]{}));
Path mavenSettings = Paths.get(System.getProperty("user.home")).resolve(".m2/settings.xml");
if (mavenSettings.toFile().exists()) {
MavenSettings settings = MavenSettings.parse(new Parser.Input(mavenSettings,
() -> {
try {
return Files.newInputStream(mavenSettings);
} catch (IOException e) {
logger.warn("Unable to load Maven settings from user home directory. Skipping.", e);
return null;
}
}),
ctx);
if (settings != null) {
new MavenExecutionContextView(ctx).setMavenSettings(settings);
if (settings.getActiveProfiles() != null) {
mavenParserBuilder.activeProfiles(settings.getActiveProfiles().getActiveProfiles().toArray(new String[]{}));
}
}
}
poms = mavenParserBuilder
.build()
.parse(allPoms, baseDir, ctx);
}
Maven pom = poms.stream()
.filter(it -> it.getModel().getGroupId().equals(mavenProject.getGroupId()) && it.getModel().getArtifactId().equals(mavenProject.getArtifactId()))
.findFirst()
.orElse(null);

Maven mavenSource = mavenParserBuilder
.build()
.parse(allPoms, baseDir, ctx)
.iterator()
.next();
for (Marker marker : projectProvenance) {
mavenSource = mavenSource.withMarkers(mavenSource.getMarkers().addIfAbsent(marker));
if(pom != null) {
for (Marker marker : projectProvenance) {
pom = pom.withMarkers(pom.getMarkers().addIfAbsent(marker));
}
}
return mavenSource;
return pom;
}

public List<SourceFile> listSourceFiles(Iterable<NamedStyles> styles,
Expand Down Expand Up @@ -180,10 +191,14 @@ public List<SourceFile> listSourceFiles(Iterable<NamedStyles> styles,

//Add provenance information to main source files
JavaSourceSet mainProvenance = JavaSourceSet.build("main", dependencies, ctx);
List<SourceFile> sourceFiles = new ArrayList<>(
ListUtils.map(javaParser.parse(mainJavaSources, baseDir, ctx),
addProvenance(baseDir, projectProvenance, mainProvenance, generatedSourcePaths))
);
List<SourceFile> sourceFiles = new ArrayList<>();
Maven maven = parseMaven(ctx);
if(maven != null) {
sourceFiles.add(maven);
alreadyParsed.add(maven.getSourcePath());
}
sourceFiles.addAll(ListUtils.map(javaParser.parse(mainJavaSources, baseDir, ctx),
addProvenance(baseDir, projectProvenance, mainProvenance, generatedSourcePaths)));
ResourceParser rp = new ResourceParser(logger);
sourceFiles.addAll(ListUtils.map(
rp.parse(baseDir, mavenProject.getBasedir().toPath().resolve("src/main/resources"), alreadyParsed),
Expand Down

0 comments on commit 00dd505

Please sign in to comment.