Skip to content

Commit fa57b83

Browse files
committed
Throw MojoFailureException on failure to parse or resolve POM
1 parent f592159 commit fa57b83

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.apache.maven.model.Plugin;
2222
import org.apache.maven.model.Profile;
2323
import org.apache.maven.model.Repository;
24+
import org.apache.maven.plugin.AbstractMojoExecutionException;
2425
import org.apache.maven.plugin.MojoExecutionException;
26+
import org.apache.maven.plugin.MojoFailureException;
2527
import org.apache.maven.plugin.logging.Log;
2628
import org.apache.maven.project.MavenProject;
2729
import org.apache.maven.rtinfo.RuntimeInformation;
@@ -50,7 +52,6 @@
5052
import org.openrewrite.maven.tree.ProfileActivation;
5153
import org.openrewrite.maven.utilities.MavenWrapper;
5254
import org.openrewrite.style.NamedStyles;
53-
import org.openrewrite.tree.ParseError;
5455
import org.openrewrite.xml.tree.Xml;
5556

5657
import java.io.File;
@@ -132,7 +133,7 @@ public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled,
132133
}
133134

134135
public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, List<NamedStyles> styles,
135-
ExecutionContext ctx) throws DependencyResolutionRequiredException, MojoExecutionException {
136+
ExecutionContext ctx) throws DependencyResolutionRequiredException, AbstractMojoExecutionException {
136137
if (runPerSubmodule) {
137138
//If running per submodule, parse the source files for only the current project.
138139
List<Marker> projectProvenance = generateProvenance(mavenProject);
@@ -241,9 +242,7 @@ private SourceFile logParseErrors(SourceFile source) {
241242
logger.warn("There were problems parsing some source files" +
242243
(mavenSession.getRequest().isShowErrors() ? "" : ", run with --errors to see full stack traces"));
243244
}
244-
String pomMessage = source instanceof Xml.Document
245-
? "; the pom could not be resolved. Some recipes may not function correctly" : "";
246-
logger.warn("There were problems parsing " + source.getSourcePath() + pomMessage);
245+
logger.warn("There were problems parsing " + source.getSourcePath());
247246
if (mavenSession.getRequest().isShowErrors()) {
248247
logger.warn(e.getMessage());
249248
}
@@ -514,11 +513,11 @@ private static JavaSourceSet sourceSet(String name, List<Path> dependencies, Jav
514513
return JavaSourceSet.build(name, dependencies);
515514
}
516515

517-
public Xml.@Nullable Document parseMaven(MavenProject mavenProject, List<Marker> projectProvenance, ExecutionContext ctx) {
516+
public Xml.@Nullable Document parseMaven(MavenProject mavenProject, List<Marker> projectProvenance, ExecutionContext ctx) throws MojoFailureException {
518517
return parseMaven(singletonList(mavenProject), singletonMap(mavenProject, projectProvenance), ctx).get(mavenProject);
519518
}
520519

521-
public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjects, Map<MavenProject, List<Marker>> projectProvenances, ExecutionContext ctx) {
520+
public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjects, Map<MavenProject, List<Marker>> projectProvenances, ExecutionContext ctx) throws MojoFailureException {
522521
if (skipMavenParsing) {
523522
logger.info("Skipping Maven parsing...");
524523
return emptyMap();
@@ -578,11 +577,15 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
578577
Path path = baseDir.resolve(document.getSourcePath());
579578
MavenProject mavenProject = projectsByPath.get(path);
580579
if (mavenProject != null) {
581-
if (document instanceof Xml.Document) {
582-
projectMap.put(mavenProject, (Xml.Document) document);
583-
} else if (document instanceof ParseError) {
584-
logError(mavenProject, "Parse error in Maven Project File '" + path + "': " + document);
580+
Optional<ParseExceptionResult> parseExceptionResult = document.getMarkers().findFirst(ParseExceptionResult.class);
581+
if (parseExceptionResult.isPresent()) {
582+
throw new MojoFailureException(
583+
mavenProject,
584+
"Failed to parse or resolve the Maven POM file or one of its dependencies; " +
585+
"We can not reliably continue without this information.",
586+
parseExceptionResult.get().getMessage());
585587
}
588+
projectMap.put(mavenProject, (Xml.Document) document);
586589
}
587590
}
588591
for (MavenProject mavenProject : mavenProjects) {
@@ -770,6 +773,7 @@ private static List<Path> listSources(Path sourceDirectory, String extension) th
770773
}
771774

772775
private static final Map<Path, GitProvenance> REPO_ROOT_TO_PROVENANCE = new HashMap<>();
776+
773777
private @Nullable GitProvenance gitProvenance(Path baseDir, @Nullable BuildEnvironment buildEnvironment) {
774778
try {
775779
// Computing git provenance can be expensive for repositories with many commits, ensure we do it only once

0 commit comments

Comments
 (0)