4747import org .openrewrite .marker .ci .BuildEnvironment ;
4848import org .openrewrite .maven .cache .InMemoryMavenPomCache ;
4949import org .openrewrite .maven .cache .MavenPomCache ;
50+ import org .openrewrite .maven .internal .RawPom ;
5051import org .openrewrite .maven .internal .RawRepositories ;
52+ import org .openrewrite .maven .tree .Pom ;
5153import org .openrewrite .maven .tree .ProfileActivation ;
54+ import org .openrewrite .maven .tree .ResolvedGroupArtifactVersion ;
5255import org .openrewrite .maven .utilities .MavenWrapper ;
5356import org .openrewrite .style .NamedStyles ;
5457import org .openrewrite .xml .tree .Xml ;
5558
5659import java .io .File ;
5760import java .io .IOException ;
61+ import java .io .InputStream ;
5862import java .nio .charset .Charset ;
5963import java .nio .file .*;
6064import java .nio .file .attribute .BasicFileAttributes ;
@@ -88,6 +92,7 @@ public class MavenMojoProjectParser {
8892
8993 private static final String MVN_JVM_CONFIG = ".mvn/jvm.config" ;
9094 private static final String MVN_MAVEN_CONFIG = ".mvn/maven.config" ;
95+ private static final String MAVEN_COMPILER_PLUGIN = "org.apache.maven.plugins:maven-compiler-plugin" ;
9196
9297 @ Nullable
9398 public static MavenPomCache POM_CACHE ;
@@ -137,7 +142,7 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, List<NamedS
137142 Xml .Document maven = parseMaven (mavenProject , projectProvenance , ctx );
138143 return listSourceFiles (mavenProject , maven , projectProvenance , styles , ctx );
139144 } else {
140- //If running across all project , iterate and parse source files from each project
145+ //If running across all projects , iterate and parse source files from each project
141146 Map <MavenProject , List <Marker >> projectProvenances = mavenSession .getProjects ().stream ()
142147 .collect (Collectors .toMap (Function .identity (), this ::generateProvenance ));
143148 Map <MavenProject , Xml .Document > projectMap = parseMaven (mavenSession .getProjects (), projectProvenances , ctx );
@@ -216,7 +221,7 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, Xml.@Nullab
216221 }
217222
218223 private static Optional <Charset > getCharset (MavenProject mavenProject ) {
219- String compilerPluginKey = "org.apache.maven.plugins:maven-compiler-plugin" ;
224+ String compilerPluginKey = MAVEN_COMPILER_PLUGIN ;
220225 Plugin plugin = Optional .ofNullable (mavenProject .getPlugin (compilerPluginKey ))
221226 .orElseGet (() -> mavenProject .getPluginManagement ().getPluginsAsMap ().get (compilerPluginKey ));
222227 if (plugin != null && plugin .getConfiguration () instanceof Xpp3Dom ) {
@@ -267,7 +272,7 @@ private static JavaVersion getSrcMainJavaVersion(MavenProject mavenProject) {
267272 String sourceCompatibility = null ;
268273 String targetCompatibility = null ;
269274
270- Plugin compilerPlugin = mavenProject .getPlugin ("org.apache.maven.plugins:maven-compiler-plugin" );
275+ Plugin compilerPlugin = mavenProject .getPlugin (MAVEN_COMPILER_PLUGIN );
271276 if (compilerPlugin != null && compilerPlugin .getConfiguration () instanceof Xpp3Dom ) {
272277 Xpp3Dom dom = (Xpp3Dom ) compilerPlugin .getConfiguration ();
273278 Xpp3Dom release = dom .getChild ("release" );
@@ -310,7 +315,7 @@ static JavaVersion getSrcTestJavaVersion(MavenProject mavenProject) {
310315 String sourceCompatibility = null ;
311316 String targetCompatibility = null ;
312317
313- Plugin compilerPlugin = mavenProject .getPlugin ("org.apache.maven.plugins:maven-compiler-plugin" );
318+ Plugin compilerPlugin = mavenProject .getPlugin (MAVEN_COMPILER_PLUGIN );
314319 if (compilerPlugin != null && compilerPlugin .getConfiguration () instanceof Xpp3Dom ) {
315320 Xpp3Dom dom = (Xpp3Dom ) compilerPlugin .getConfiguration ();
316321 Xpp3Dom release = dom .getChild ("testRelease" );
@@ -417,7 +422,7 @@ private Stream<SourceFile> processMainSources(
417422 }
418423
419424 List <Marker > mainProjectProvenance = new ArrayList <>(projectProvenance );
420- mainProjectProvenance .add (sourceSet ("main" , dependencies , typeCache ));
425+ mainProjectProvenance .add (JavaSourceSet . build ("main" , dependencies ));
421426 mainProjectProvenance .add (getSrcMainJavaVersion (mavenProject ));
422427
423428 //Filter out any generated source files from the returned list, as we do not want to apply the recipe to the
@@ -474,7 +479,7 @@ private Stream<SourceFile> processTestSources(
474479 }
475480
476481 List <Marker > markers = new ArrayList <>(projectProvenance );
477- markers .add (sourceSet ("test" , testDependencies , typeCache ));
482+ markers .add (JavaSourceSet . build ("test" , testDependencies ));
478483 markers .add (getSrcTestJavaVersion (mavenProject ));
479484
480485 // Any resources parsed from "test/resources" should also have the test source set added to them.
@@ -506,10 +511,6 @@ private Stream<SourceFile> processTestSources(
506511 return null ;
507512 }
508513
509- private static JavaSourceSet sourceSet (String name , List <Path > dependencies , JavaTypeCache typeCache ) {
510- return JavaSourceSet .build (name , dependencies );
511- }
512-
513514 public Xml .@ Nullable Document parseMaven (MavenProject mavenProject , List <Marker > projectProvenance , ExecutionContext ctx ) throws MojoFailureException {
514515 return parseMaven (singletonList (mavenProject ), singletonMap (mavenProject , projectProvenance ), ctx ).get (mavenProject );
515516 }
@@ -520,30 +521,29 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
520521 return emptyMap ();
521522 }
522523
523- MavenProject topLevelProject = mavenSession .getTopLevelProject ();
524- logInfo (topLevelProject , "Resolving Poms..." );
525-
526- Set <Path > allPoms = new LinkedHashSet <>();
527- mavenProjects .forEach (p -> collectPoms (p , allPoms ));
528- for (MavenProject mavenProject : mavenProjects ) {
529- mavenSession .getProjectDependencyGraph ().getUpstreamProjects (mavenProject , true ).forEach (p -> collectPoms (p , allPoms ));
530- }
531- MavenParser .Builder mavenParserBuilder = MavenParser .builder ().mavenConfig (baseDir .resolve (MVN_MAVEN_CONFIG ));
532-
533524 MavenSettings settings = buildSettings ();
534525 MavenExecutionContextView mavenExecutionContext = MavenExecutionContextView .view (ctx );
535526 mavenExecutionContext .setMavenSettings (settings );
536527 mavenExecutionContext .setResolutionListener (new MavenLoggingResolutionEventListener (logger ));
537528
538- if (pomCacheEnabled ) {
539- //The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
540- //If the flag is set to false, only the default, in-memory cache is used.
541- mavenExecutionContext .setPomCache (getPomCache (pomCacheDirectory , logger ));
529+ // The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
530+ // If the flag is set to false, only the default, in-memory cache is used.
531+ MavenPomCache pomCache = pomCacheEnabled ? getPomCache (pomCacheDirectory , logger ) : mavenExecutionContext .getPomCache ();
532+ mavenExecutionContext .setPomCache (pomCache );
533+
534+ MavenProject topLevelProject = mavenSession .getTopLevelProject ();
535+ logInfo (topLevelProject , "Resolving Poms..." );
536+
537+ Set <Path > allPoms = new LinkedHashSet <>();
538+ mavenProjects .forEach (p -> collectPoms (p , allPoms , mavenExecutionContext ));
539+ for (MavenProject mavenProject : mavenProjects ) {
540+ mavenSession .getProjectDependencyGraph ().getUpstreamProjects (mavenProject , true ).forEach (p -> collectPoms (p , allPoms , mavenExecutionContext ));
542541 }
543542
543+ MavenParser .Builder mavenParserBuilder = MavenParser .builder ().mavenConfig (baseDir .resolve (MVN_MAVEN_CONFIG ));
544544 List <String > activeProfiles = topLevelProject .getActiveProfiles ().stream ().map (Profile ::getId ).collect (toList ());
545545 if (!activeProfiles .isEmpty ()) {
546- mavenParserBuilder .activeProfiles (activeProfiles .toArray (new String []{} ));
546+ mavenParserBuilder .activeProfiles (activeProfiles .toArray (new String [0 ] ));
547547 }
548548
549549 List <SourceFile > mavens = mavenParserBuilder .build ()
@@ -611,16 +611,22 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
611611 *
612612 * @param project A maven project to examine for any children/parent poms.
613613 * @param paths A list of paths to poms that have been collected so far.
614+ * @param ctx The execution context for the current project.
614615 */
615- private void collectPoms (MavenProject project , Set <Path > paths ) {
616- paths .add (pomPath (project ));
616+ private void collectPoms (MavenProject project , Set <Path > paths , MavenExecutionContextView ctx ) {
617+ if (!paths .add (pomPath (project ))) {
618+ return ;
619+ }
620+
621+ ResolvedGroupArtifactVersion gav = createResolvedGAV (project , ctx );
622+ ctx .getPomCache ().putPom (gav , createPom (project ));
617623
618624 // children
619625 if (project .getCollectedProjects () != null ) {
620626 for (MavenProject child : project .getCollectedProjects ()) {
621627 Path path = pomPath (child );
622628 if (!paths .contains (path )) {
623- collectPoms (child , paths );
629+ collectPoms (child , paths , ctx );
624630 }
625631 }
626632 }
@@ -629,7 +635,7 @@ private void collectPoms(MavenProject project, Set<Path> paths) {
629635 while (parent != null && parent .getFile () != null ) {
630636 Path path = pomPath (parent );
631637 if (!paths .contains (path )) {
632- collectPoms (parent , paths );
638+ collectPoms (parent , paths , ctx );
633639 }
634640 parent = parent .getParent ();
635641 }
@@ -644,7 +650,7 @@ private static Path pomPath(MavenProject mavenProject) {
644650 // org.eclipse.tycho:tycho-packaging-plugin:update-consumer-pom produces a synthetic pom
645651 if (pomPath .endsWith (".tycho-consumer-pom.xml" )) {
646652 Path normalPom = mavenProject .getBasedir ().toPath ().resolve ("pom.xml" );
647- // check for existence of the POM, since Tycho can work pom-less
653+ // check for the existence of the POM, since Tycho can work pom-less
648654 if (Files .isReadable (normalPom ) && Files .isRegularFile (normalPom )) {
649655 return normalPom ;
650656 }
@@ -807,4 +813,24 @@ private void logDebug(MavenProject mavenProject, String message) {
807813 private static <E extends Throwable > E sneakyThrow (Throwable e ) throws E {
808814 return (E ) e ;
809815 }
816+
817+ private static ResolvedGroupArtifactVersion createResolvedGAV (MavenProject project , MavenExecutionContextView ctx ) {
818+ return new ResolvedGroupArtifactVersion (
819+ ctx .getLocalRepository ().getUri (),
820+ project .getGroupId (),
821+ project .getArtifactId (),
822+ project .getVersion (),
823+ project .getVersion ().endsWith ("-SNAPSHOT" ) ? null : project .getVersion ()
824+ );
825+ }
826+
827+ private static @ Nullable Pom createPom (MavenProject project ) {
828+ Path pomPath = project .getFile ().toPath ();
829+ try (InputStream is = Files .newInputStream (pomPath )) {
830+ RawPom rawPom = RawPom .parse (is , null );
831+ return rawPom .toPom (project .getBasedir ().toPath ().relativize (pomPath ), null );
832+ } catch (IOException e ) {
833+ return null ;
834+ }
835+ }
810836}
0 commit comments