Skip to content

Commit f059392

Browse files
committed
Fix #169, Java.version downgraded from 17 to 11
1 parent 7176632 commit f059392

File tree

7 files changed

+166
-36
lines changed

7 files changed

+166
-36
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import lombok.EqualsAndHashCode;
19+
import lombok.Value;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Option;
22+
import org.openrewrite.Recipe;
23+
import org.openrewrite.TreeVisitor;
24+
import org.openrewrite.maven.MavenVisitor;
25+
import org.openrewrite.xml.tree.Xml;
26+
27+
@Value
28+
@EqualsAndHashCode(callSuper = true)
29+
public class UpgradeJavaVersion extends Recipe {
30+
@Override
31+
public String getDisplayName() {
32+
return "Upgrade Java version";
33+
}
34+
35+
@Override
36+
public String getDescription() {
37+
return "Upgrade build plugin configuration to use the specified Java version. " +
38+
"This recipe changes maven-compiler-plugin target version and related settings. " +
39+
"Will not downgrade if the version is newer than the specified version.";
40+
}
41+
42+
@Option(displayName = "Java version",
43+
description = "The Java version to upgrade to.",
44+
example = "11")
45+
Integer version;
46+
47+
@Override
48+
protected TreeVisitor<?, ExecutionContext> getVisitor() {
49+
return new MavenVisitor<ExecutionContext>() {
50+
@Override
51+
public Xml visitTag(Xml.Tag tag, ExecutionContext executionContext) {
52+
Xml.Tag t = (Xml.Tag) super.visitTag(tag, executionContext);
53+
if(!isPropertyTag()) {
54+
return t;
55+
}
56+
if(!"java.version".equals(t.getName()) && !"maven.compiler.source".equals(t.getName()) && !"maven.compiler.target".equals(t.getName())) {
57+
return t;
58+
}
59+
float value = tag.getValue().map(Float::parseFloat).orElse(0f);
60+
if(value >= version) {
61+
return t;
62+
}
63+
return t.withValue(String.valueOf(version));
64+
}
65+
};
66+
}
67+
}

src/main/java/org/openrewrite/java/migrate/maven/UseMavenCompilerPluginReleaseConfiguration.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,42 @@ public String getDisplayName() {
5050

5151
@Override
5252
public String getDescription() {
53-
return "Replaces any explicit `source` or `target` configuration (if present) on the maven-compiler-plugin with `release`, and updates the `release` value if needed.";
53+
return "Replaces any explicit `source` or `target` configuration (if present) on the maven-compiler-plugin with " +
54+
"`release`, and updates the `release` value if needed. Will not downgrade the java version if the current version is higher.";
5455
}
5556

5657
@Override
5758
public TreeVisitor<?, ExecutionContext> getVisitor() {
5859
return new MavenIsoVisitor<ExecutionContext>() {
5960
@Override
6061
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
61-
final Xml.Tag superVisit = super.visitTag(tag, ctx);
62+
final Xml.Tag t = super.visitTag(tag, ctx);
6263
if (!PLUGINS_MATCHER.matches(getCursor())) {
63-
return superVisit;
64+
return t;
6465
}
65-
Optional<Xml.Tag> maybeCompilerPlugin = superVisit.getChildren().stream()
66+
Optional<Xml.Tag> maybeCompilerPlugin = t.getChildren().stream()
6667
.filter(plugin -> "plugin".equals(plugin.getName())
6768
&& "org.apache.maven.plugins".equals(plugin.getChildValue("groupId").orElse(null))
6869
&& "maven-compiler-plugin".equals(plugin.getChildValue("artifactId").orElse(null)))
6970
.findAny();
7071
Optional<Xml.Tag> maybeCompilerPluginConfig = maybeCompilerPlugin
7172
.flatMap(it -> it.getChild("configuration"));
7273
if (!maybeCompilerPluginConfig.isPresent()) {
73-
return superVisit;
74+
return t;
7475
}
7576
Xml.Tag compilerPluginConfig = maybeCompilerPluginConfig.get();
7677
Optional<String> source = compilerPluginConfig.getChildValue("source");
7778
Optional<String> target = compilerPluginConfig.getChildValue("target");
7879
Optional<String> release = compilerPluginConfig.getChildValue("release");
7980
if (!source.isPresent()
8081
&& !target.isPresent()
81-
&& !release.isPresent()) {
82-
return superVisit;
82+
&& !release.isPresent()
83+
|| currentNewerThanProposed(release)) {
84+
return t;
8385
}
84-
Xml.Tag updated = filterTagChildren(superVisit, compilerPluginConfig,
86+
Xml.Tag updated = filterTagChildren(t, compilerPluginConfig,
8587
child -> !("source".equals(child.getName()) || "target".equals(child.getName())));
86-
if (updated != superVisit
88+
if (updated != t
8789
&& source.map("${maven.compiler.source}"::equals).orElse(true)
8890
&& target.map("${maven.compiler.target}"::equals).orElse(true)) {
8991
return filterTagChildren(updated, maybeCompilerPlugin.get(),
@@ -99,6 +101,19 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
99101
};
100102
}
101103

104+
private boolean currentNewerThanProposed(@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<String> maybeRelease) {
105+
if(!maybeRelease.isPresent()) {
106+
return false;
107+
}
108+
try {
109+
float currentVersion = Float.parseFloat(maybeRelease.get());
110+
float proposedVersion = Float.parseFloat(releaseVersion);
111+
return proposedVersion < currentVersion;
112+
} catch (NumberFormatException e) {
113+
return false;
114+
}
115+
}
116+
102117
private boolean hasJavaVersionProperty(Xml.Document xml) {
103118
return !FindProperties.find(xml, "java.version").isEmpty();
104119
}

src/main/resources/META-INF/rewrite/jakarta-ee-9.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ tags:
218218
- jakarta
219219

220220
recipeList:
221+
- org.openrewrite.maven.UpgradeDependencyVersion:
222+
groupId: jakarta.authorization
223+
artifactId: jakarta.authorization-api
224+
newVersion: 2.x
221225
- org.openrewrite.maven.AddDependency:
222226
groupId: jakarta.authentication
223227
artifactId: jakarta.authentication-api

src/main/resources/META-INF/rewrite/java-version-11.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,8 @@ name: org.openrewrite.java.migrate.JavaVersion11
6060
displayName: Change Maven Java version property values to 11
6161
description: Change maven.compiler.source and maven.compiler.target values to 11.
6262
recipeList:
63-
- org.openrewrite.maven.ChangePropertyValue:
64-
key: java.version
65-
newValue: 11
66-
addIfMissing: false
67-
- org.openrewrite.maven.ChangePropertyValue:
68-
key: maven.compiler.source
69-
newValue: 11
70-
addIfMissing: false
71-
- org.openrewrite.maven.ChangePropertyValue:
72-
key: maven.compiler.target
73-
newValue: 11
74-
addIfMissing: false
63+
- org.openrewrite.java.migrate.UpgradeJavaVersion:
64+
version: 11
7565
- org.openrewrite.java.migrate.maven.UseMavenCompilerPluginReleaseConfiguration:
7666
releaseVersion: 11
7767
---

src/main/resources/META-INF/rewrite/java-version-17.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ tags:
4141
- java17
4242
- compiler
4343
recipeList:
44-
- org.openrewrite.maven.ChangePropertyValue:
45-
key: java.version
46-
newValue: 17
47-
addIfMissing: false
48-
- org.openrewrite.maven.ChangePropertyValue:
49-
key: maven.compiler.source
50-
newValue: 17
51-
addIfMissing: false
52-
- org.openrewrite.maven.ChangePropertyValue:
53-
key: maven.compiler.target
54-
newValue: 17
55-
addIfMissing: false
44+
- org.openrewrite.java.migrate.UpgradeJavaVersion:
45+
version: 17
5646
- org.openrewrite.java.migrate.maven.UseMavenCompilerPluginReleaseConfiguration:
5747
releaseVersion: 17

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.migrate;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.Issue;
1920
import org.openrewrite.config.Environment;
2021
import org.openrewrite.test.RecipeSpec;
2122
import org.openrewrite.test.RewriteTest;
@@ -102,4 +103,27 @@ void changeMavenCompiler() {
102103
)
103104
);
104105
}
106+
107+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/169")
108+
@Test
109+
void noDowngrade() {
110+
rewriteRun(
111+
pomXml(
112+
"""
113+
<project>
114+
<modelVersion>4.0.0</modelVersion>
115+
116+
<properties>
117+
<maven.compiler.source>17</maven.compiler.source>
118+
<maven.compiler.target>17</maven.compiler.target>
119+
</properties>
120+
121+
<groupId>com.mycompany.app</groupId>
122+
<artifactId>my-app</artifactId>
123+
<version>1</version>
124+
</project>
125+
"""
126+
)
127+
);
128+
}
105129
}

src/test/java/org/openrewrite/java/migrate/maven/UseMavenCompilerPluginReleaseConfigurationTest.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.migrate.maven;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.Issue;
1920
import org.openrewrite.test.RecipeSpec;
2021
import org.openrewrite.test.RewriteTest;
2122

@@ -30,6 +31,7 @@ public void defaults(final RecipeSpec spec) {
3031
@Test
3132
void replacesSourceAndTargetConfig() {
3233
rewriteRun(
34+
//language=xml
3335
pomXml("""
3436
<?xml version="1.0" encoding="UTF-8"?>
3537
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -84,6 +86,7 @@ void replacesSourceAndTargetConfig() {
8486
@Test
8587
void deletesSourceAndTargetConfigWithNoReplacementIfDefaultAndCleansUpConfiguration() {
8688
rewriteRun(
89+
//language=xml
8790
pomXml("""
8891
<?xml version="1.0" encoding="UTF-8"?>
8992
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -135,6 +138,7 @@ void deletesSourceAndTargetConfigWithNoReplacementIfDefaultAndCleansUpConfigurat
135138
@Test
136139
void deletesSourceAndTargetConfigWithNoReplacementIfDefault() {
137140
rewriteRun(
141+
//language=xml
138142
pomXml("""
139143
<?xml version="1.0" encoding="UTF-8"?>
140144
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -190,6 +194,7 @@ void deletesSourceAndTargetConfigWithNoReplacementIfDefault() {
190194
@Test
191195
void reusesJavaVersionVariableIfAvailable() {
192196
rewriteRun(
197+
//language=xml
193198
pomXml("""
194199
<?xml version="1.0" encoding="UTF-8"?>
195200
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -252,6 +257,7 @@ void reusesJavaVersionVariableIfAvailable() {
252257
@Test
253258
void upgradesExistingReleaseConfig() {
254259
rewriteRun(
260+
//language=xml
255261
pomXml("""
256262
<?xml version="1.0" encoding="UTF-8"?>
257263
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -305,6 +311,7 @@ void upgradesExistingReleaseConfig() {
305311
@Test
306312
void prefersJavaVersionIfAvailable() {
307313
rewriteRun(
314+
//language=xml
308315
pomXml("""
309316
<?xml version="1.0" encoding="UTF-8"?>
310317
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -366,6 +373,7 @@ void prefersJavaVersionIfAvailable() {
366373
@Test
367374
void notMisledByUnrelatedProperty() {
368375
rewriteRun(
376+
//language=xml
369377
pomXml("""
370378
<?xml version="1.0" encoding="UTF-8"?>
371379
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -386,7 +394,7 @@ void notMisledByUnrelatedProperty() {
386394
<version>3.8.0</version>
387395
<configuration>
388396
<release>10</release>
389-
<barfoo>${foobar}</barfoo>
397+
<basedir>${foobar}</basedir>
390398
</configuration>
391399
</plugin>
392400
</plugins>
@@ -414,7 +422,7 @@ void notMisledByUnrelatedProperty() {
414422
<version>3.8.0</version>
415423
<configuration>
416424
<release>11</release>
417-
<barfoo>${foobar}</barfoo>
425+
<basedir>${foobar}</basedir>
418426
</configuration>
419427
</plugin>
420428
</plugins>
@@ -429,6 +437,7 @@ void notMisledByUnrelatedProperty() {
429437
@Test
430438
void doesNotChangeIfNoSourceOrTargetConfig() {
431439
rewriteRun(
440+
//language=xml
432441
pomXml("""
433442
<?xml version="1.0" encoding="UTF-8"?>
434443
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -455,4 +464,35 @@ void doesNotChangeIfNoSourceOrTargetConfig() {
455464
)
456465
);
457466
}
458-
}
467+
468+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/169")
469+
@Test
470+
void noVersionDowngrade() {
471+
rewriteRun(
472+
//language=xml
473+
pomXml("""
474+
<?xml version="1.0" encoding="UTF-8"?>
475+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
476+
<modelVersion>4.0.0</modelVersion>
477+
<groupId>org.sample</groupId>
478+
<artifactId>sample</artifactId>
479+
<version>1.0.0</version>
480+
481+
<build>
482+
<plugins>
483+
<plugin>
484+
<groupId>org.apache.maven.plugins</groupId>
485+
<artifactId>maven-compiler-plugin</artifactId>
486+
<version>3.8.0</version>
487+
<configuration>
488+
<release>17</release>
489+
</configuration>
490+
</plugin>
491+
</plugins>
492+
</build>
493+
494+
</project>
495+
""")
496+
);
497+
}
498+
}

0 commit comments

Comments
 (0)