From 7b2093aaf4c8a946dbf62cba3f42e5690aba05ad Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Wed, 11 Sep 2024 16:52:29 -0700 Subject: [PATCH] Implement support for Maven's prerequisites mechanism --- .../openrewrite/maven/internal/RawPom.java | 12 ++ .../java/org/openrewrite/maven/tree/Pom.java | 3 + .../openrewrite/maven/tree/Prerequisites.java | 32 +++ .../openrewrite/maven/tree/ResolvedPom.java | 4 + .../openrewrite/maven/MavenParserTest.java | 194 +++++++++++------- .../maven/internal/RawPomTest.java | 17 +- 6 files changed, 183 insertions(+), 79 deletions(-) create mode 100644 rewrite-maven/src/main/java/org/openrewrite/maven/tree/Prerequisites.java diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/RawPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/RawPom.java index 4befdcfd431..2468380c964 100755 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/RawPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/RawPom.java @@ -90,6 +90,9 @@ public class RawPom { @Nullable String description; + @Nullable + Prerequisites prerequisites; + @Nullable String packaging; @@ -192,6 +195,14 @@ public Licenses(@JacksonXmlProperty(localName = "license") List license } } + @Getter + public static class Prerequisites { + + @JacksonXmlProperty(localName = "maven") + @Nullable + public String maven; + } + @Getter public static class Profiles { private final List profiles; @@ -371,6 +382,7 @@ public Pom toPom(@Nullable Path inputPath, @Nullable MavenRepository repo) { null)) .name(name) .obsoletePomVersion(pomVersion) + .prerequisites(prerequisites == null ? null : new org.openrewrite.maven.tree.Prerequisites(prerequisites.getMaven())) .packaging(packaging) .properties(getProperties() == null ? emptyMap() : getProperties()) .licenses(mapLicenses(getLicenses())) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/tree/Pom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/tree/Pom.java index 6bd532cc6c7..e5af8d7bb32 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/tree/Pom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/tree/Pom.java @@ -85,6 +85,9 @@ public static int getModelVersion() { @Nullable String name; + @Nullable + Prerequisites prerequisites; + @Nullable String packaging; diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/tree/Prerequisites.java b/rewrite-maven/src/main/java/org/openrewrite/maven/tree/Prerequisites.java new file mode 100644 index 00000000000..77e0df37125 --- /dev/null +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/tree/Prerequisites.java @@ -0,0 +1,32 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.maven.tree; + +import lombok.Value; +import org.jspecify.annotations.Nullable; + +/** + * Models the prerequisites element of a POM. + */ +@Value +public class Prerequisites { + + /** + * The minimum version of Maven required to build the project. + */ + @Nullable + String maven; +} diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/tree/ResolvedPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/tree/ResolvedPom.java index 2969265eed7..8d3d08db44c 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/tree/ResolvedPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/tree/ResolvedPom.java @@ -301,6 +301,10 @@ public String getPackaging() { case "project.parent.version": case "parent.version": return requested.getParent() != null ? requested.getParent().getVersion() : null; + case "prerequisites.maven": + case "pom.prerequisites.maven": + case "project.prerequisites.maven": + return requested.getPrerequisites() == null ? null : requested.getPrerequisites().getMaven(); } return System.getProperty(property); diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java index 44e1d67106c..895917dd922 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java @@ -55,7 +55,7 @@ void rangeVersion() { com.mycompany.app my-app 1 - + junit @@ -69,6 +69,55 @@ void rangeVersion() { ); } + @Test + void prerequisites() { + rewriteRun( + //language=xml + pomXml( + """ + + org.sample + sample + 1.0.0 + + 3.0 + + + + org.apache.maven.reporting + maven-reporting-api + ${project.prerequisites.maven} + + + + """ + ) + ); + } + + @Test + void jacoco() { + rewriteRun( + //language=xml + pomXml( + """ + + org.sample + sample + 1.0.0 + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + """ + ) + ); + } + @Test void skipDependencyResolution() { rewriteRun( @@ -79,7 +128,7 @@ void skipDependencyResolution() { com.mycompany.app my-app 1 - + foo @@ -128,7 +177,7 @@ void invalidRange() { com.mycompany.app my-app 1 - + junit @@ -517,7 +566,7 @@ void parse() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -527,7 +576,7 @@ void parse() { Trygve Laugstøl - + org.junit.jupiter @@ -583,11 +632,11 @@ void handlesRepositories() { """ 4.0.0 - + org.openrewrite.maven single-project 0.1.0-SNAPSHOT - + com.google.guava @@ -595,7 +644,7 @@ void handlesRepositories() { 29.0-jre - + jcenter @@ -617,15 +666,15 @@ void handlesPropertiesInDependencyScope() { """ 4.0.0 - + org.openrewrite.maven single-project 0.1.0-SNAPSHOT - + compile - + com.google.guava @@ -677,11 +726,11 @@ void selfRecursiveParent() { """ 4.0.0 - + com.mycompany.app my-app 1 - + com.mycompany.app my-app @@ -701,11 +750,11 @@ void selfRecursiveDependency() { """ 4.0.0 - + com.mycompany.app my-app 1 - + com.mycompany.app @@ -893,11 +942,11 @@ public MockResponse dispatch(RecordedRequest request) { resp.setBody(""" 4.0.0 - + com.foo bar 1.0.0 - + """ ); @@ -940,11 +989,11 @@ public MockResponse dispatch(RecordedRequest request) { """ 4.0.0 - + org.openrewrite.test foo 0.1.0-SNAPSHOT - + com.foo @@ -1130,17 +1179,17 @@ void indirectBomImportedFromParent() { """ 4.0.0 - + b org.openrewrite.maven 0.1.0-SNAPSHOT pom - + 1.8 1.8 - + @@ -1161,12 +1210,12 @@ void indirectBomImportedFromParent() { """ 4.0.0 - + c org.openrewrite.maven 0.1.0-SNAPSHOT pom - + @@ -1185,11 +1234,11 @@ void indirectBomImportedFromParent() { """ 4.0.0 - + org.openrewrite.maven d 0.1.0-SNAPSHOT - + 1.8 1.8 @@ -1283,6 +1332,7 @@ void parseEmptyValueActivationTag() { assertThat(activation).isNotNull(); assertThat(activation.getActiveByDefault()).isNull(); assertThat(activation.getJdk()).isNull(); + //noinspection DataFlowIssue assertThat(activation.getProperty()).isNull(); }) ) @@ -1406,7 +1456,7 @@ void dependencyManagementPropagatesToDependencies() { a-parent 0.1.0-SNAPSHOT pom - + @@ -1429,9 +1479,9 @@ void dependencyManagementPropagatesToDependencies() { 0.1.0-SNAPSHOT - + a - + org.openrewrite.maven @@ -1490,9 +1540,9 @@ void dependencyManagementPropagatesToDependencies() { 0.1.0-SNAPSHOT - + b - + org.openrewrite.maven @@ -1606,7 +1656,7 @@ void profileNoJdkActivation() { com.mycompany.app my-app 1 - + old-jdk @@ -1812,11 +1862,11 @@ void ciFriendlyVersionWithParent() { sample ${revision} pom - + sample-rest - + """, spec -> spec.path("pom.xml")), pomXml( @@ -1824,11 +1874,11 @@ void ciFriendlyVersionWithParent() { - + 4.0.0 sample-rest jar - + net.sample sample @@ -1854,14 +1904,14 @@ void canConnectProjectPomsWhenUsingCiFriendlyVersions() { sample ${revision} pom - + sample-parent sample-app sample-rest sample-web - + 0.0.0-SNAPSHOT @@ -1877,11 +1927,11 @@ void canConnectProjectPomsWhenUsingCiFriendlyVersions() { net.sample ${revision} pom - + 0.0.0-SNAPSHOT - + @@ -1903,24 +1953,24 @@ void canConnectProjectPomsWhenUsingCiFriendlyVersions() { - + 4.0.0 sample-app jar - + net.sample sample-parent ${revision} ../parent/pom.xml - + net.sample sample-rest - + net.sample sample-web @@ -1933,11 +1983,11 @@ void canConnectProjectPomsWhenUsingCiFriendlyVersions() { - + 4.0.0 sample-rest jar - + net.sample sample-parent @@ -1951,11 +2001,11 @@ void canConnectProjectPomsWhenUsingCiFriendlyVersions() { - + 4.0.0 sample-web jar - + net.sample sample-parent @@ -1982,14 +2032,14 @@ void ciFriendlyVersionsStillWorkAfterUpdateMavenModel() { sample ${revision} pom - + sample-parent sample-app sample-rest sample-web - + 0.0.0-SNAPSHOT @@ -2005,11 +2055,11 @@ void ciFriendlyVersionsStillWorkAfterUpdateMavenModel() { net.sample ${revision} pom - + 0.0.0-SNAPSHOT - + @@ -2031,29 +2081,29 @@ void ciFriendlyVersionsStillWorkAfterUpdateMavenModel() { - + 4.0.0 sample-app jar - + net.sample sample-parent ${revision} ../parent/pom.xml - + net.sample sample-rest - + net.sample sample-web - + junit junit @@ -2065,29 +2115,29 @@ void ciFriendlyVersionsStillWorkAfterUpdateMavenModel() { - + 4.0.0 sample-app jar - + net.sample sample-parent ${revision} ../parent/pom.xml - + net.sample sample-rest - + net.sample sample-web - + junit junit @@ -2101,11 +2151,11 @@ void ciFriendlyVersionsStillWorkAfterUpdateMavenModel() { - + 4.0.0 sample-rest jar - + net.sample sample-parent @@ -2119,11 +2169,11 @@ void ciFriendlyVersionsStillWorkAfterUpdateMavenModel() { - + 4.0.0 sample-web jar - + net.sample sample-parent @@ -2144,16 +2194,16 @@ void multipleCiFriendlyVersionPlaceholders() { 4.0.0 - + bogus.example parent ${revision}${changelist} pom - + sub - + 99999.0 -SNAPSHOT @@ -2166,13 +2216,13 @@ void multipleCiFriendlyVersionPlaceholders() { 4.0.0 - + bogus.example parent ${revision}${changelist} - + sub """, spec -> spec.path("sub/pom.xml")) @@ -2337,7 +2387,7 @@ void malformedPom() { com.mycompany.app my-app 1 - + junit diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/internal/RawPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/internal/RawPomTest.java index 4eecefea9f0..8f69e5e1bc2 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/internal/RawPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/internal/RawPomTest.java @@ -55,14 +55,15 @@ void profileActivationByAbsenceOfProperty() { @Test void repositoriesSerializationAndDeserialization() { RawPom pom = RawPom.parse( + //language=xml new ByteArrayInputStream(""" `4.0.0 - + com.mycompany.app my-app 1 - + spring-milestones @@ -75,6 +76,7 @@ void repositoriesSerializationAndDeserialization() { null ); + //noinspection DataFlowIssue assertThat(pom.getRepositories()).isNotNull(); assertThat(pom.getRepositories().getRepositories()).hasSize(1); } @@ -82,10 +84,11 @@ void repositoriesSerializationAndDeserialization() { @Test void serializePluginFlags() { RawPom pom = RawPom.parse( + //language=xml new ByteArrayInputStream(""" 4.0.0 - + com.mycompany.app my-app 1 @@ -280,7 +283,7 @@ void deserializePom() throws JsonProcessingException { A business-friendly OSS license - + @@ -299,7 +302,7 @@ void deserializePom() throws JsonProcessingException { default - + java9+ @@ -494,12 +497,12 @@ void deserializePluginConfiguration() throws JsonProcessingException { @Language("xml") String pomString = """ 4.0.0 - + com.mycompany.app my-app 1 jar - +