Skip to content

Commit 151eec7

Browse files
authored
Update UpgradeJavaVersion to support more case of maven java version upgrade (#213)
1 parent 90dc103 commit 151eec7

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

src/main/java/org/openrewrite/java/migrate/UpgradeJavaVersion.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
import org.openrewrite.java.tree.J;
2727
import org.openrewrite.marker.SearchResult;
2828
import org.openrewrite.maven.MavenVisitor;
29+
import org.openrewrite.xml.XPathMatcher;
30+
import org.openrewrite.xml.search.FindTags;
2931
import org.openrewrite.xml.tree.Xml;
3032

33+
import java.util.Arrays;
3134
import java.util.Collections;
3235
import java.util.List;
3336
import java.util.Optional;
37+
import java.util.stream.Collectors;
3438

3539

3640
@Value
@@ -117,22 +121,49 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method,
117121
}
118122
}
119123

124+
private static final List<String> JAVA_VERSION_XPATHS = Arrays.asList(
125+
"/project/properties/java.version",
126+
"/project/properties/jdk.version",
127+
"/project/properties/javaVersion",
128+
"/project/properties/jdkVersion",
129+
"/project/properties/maven.compiler.source",
130+
"/project/properties/maven.compiler.target",
131+
"/project/properties/maven.compiler.release",
132+
"/project/build/plugins/plugin[artifactId='maven-compiler-plugin']/configuration/source",
133+
"/project/build/plugins/plugin[artifactId='maven-compiler-plugin']/configuration/target",
134+
"/project/build/plugins/plugin[artifactId='maven-compiler-plugin']/configuration/release");
135+
136+
private static final List<XPathMatcher> JAVA_VERSION_XPATH_MATCHERS =
137+
JAVA_VERSION_XPATHS.stream().map(XPathMatcher::new).collect(Collectors.toList());
138+
139+
120140
private class MavenUpdateJavaVersionVisitor extends MavenVisitor<ExecutionContext> {
121141
@Override
122142
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
123-
Xml.Tag t = (Xml.Tag) super.visitTag(tag, ctx);
124-
if (!isPropertyTag()) {
125-
return t;
126-
}
127-
if (!"java.version".equals(t.getName()) && !"maven.compiler.source".equals(t.getName()) && !"maven.compiler.target".equals(t.getName()) ||
128-
(tag.getValue().isPresent() && tag.getValue().get().startsWith("${"))) {
129-
return t;
130-
}
131-
float value = tag.getValue().map(Float::parseFloat).orElse(0f);
132-
if (value >= version) {
133-
return t;
143+
tag = (Xml.Tag) super.visitTag(tag, ctx);
144+
145+
if (JAVA_VERSION_XPATH_MATCHERS.stream().anyMatch(matcher -> matcher.matches(getCursor()))) {
146+
Optional<Float> maybeVersion = tag.getValue().flatMap(
147+
value -> {
148+
try {
149+
return Optional.of(Float.parseFloat(value));
150+
} catch (NumberFormatException e) {
151+
return Optional.empty();
152+
}
153+
}
154+
);
155+
156+
if (!maybeVersion.isPresent()) {
157+
return tag;
158+
}
159+
float currentVersion = maybeVersion.get();
160+
if (currentVersion >= version) {
161+
return tag;
162+
}
163+
return tag.withValue(String.valueOf(version));
134164
}
135-
return t.withValue(String.valueOf(version));
165+
166+
return tag;
136167
}
137168
}
138169
}

src/test/java/org/openrewrite/java/migrate/UpgradeJavaVersionTest.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
class UpgradeJavaVersionTest implements RewriteTest {
3030

3131
@Test
32-
void mavenUpgradeFromJava8ToJava17() {
32+
void mavenUpgradeFromJava8ToJava17ViaProperties() {
3333
rewriteRun(
3434
spec -> spec.recipe(new UpgradeJavaVersion(17)),
3535
//language=xml
@@ -68,6 +68,56 @@ void mavenUpgradeFromJava8ToJava17() {
6868
);
6969
}
7070

71+
@Test
72+
void mavenUpgradeFromJava8ToJava17ViaConfiguration() {
73+
//language=xml
74+
rewriteRun(
75+
spec -> spec.recipe(new UpgradeJavaVersion(17)),
76+
pomXml(
77+
"""
78+
<project>
79+
<groupId>com.mycompany.app</groupId>
80+
<artifactId>my-app</artifactId>
81+
<version>1</version>
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<groupId>org.apache.maven.plugins</groupId>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<version>3.8.0</version>
88+
<configuration>
89+
<source>1.8</source>
90+
<target>1.8</target>
91+
</configuration>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
</project>
96+
""",
97+
"""
98+
<project>
99+
<groupId>com.mycompany.app</groupId>
100+
<artifactId>my-app</artifactId>
101+
<version>1</version>
102+
<build>
103+
<plugins>
104+
<plugin>
105+
<groupId>org.apache.maven.plugins</groupId>
106+
<artifactId>maven-compiler-plugin</artifactId>
107+
<version>3.8.0</version>
108+
<configuration>
109+
<source>17</source>
110+
<target>17</target>
111+
</configuration>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</project>
116+
"""
117+
)
118+
);
119+
}
120+
71121
@Test
72122
void gradleUpgradeFromJava11ToJava17() {
73123
rewriteRun(

0 commit comments

Comments
 (0)