Skip to content

Commit 9abac8d

Browse files
authored
Skip update if existing compiler plugin version config is newer (#5644)
1 parent a321036 commit 9abac8d

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

rewrite-maven/src/main/java/org/openrewrite/maven/UseMavenCompilerPluginReleaseConfiguration.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
7777
Optional<String> source = compilerPluginConfig.getChildValue("source");
7878
Optional<String> target = compilerPluginConfig.getChildValue("target");
7979
Optional<String> release = compilerPluginConfig.getChildValue("release");
80-
if (!source.isPresent() &&
81-
!target.isPresent() &&
82-
!release.isPresent() ||
83-
currentNewerThanProposed(release)) {
80+
81+
if (currentNewerThanProposed(source)
82+
|| currentNewerThanProposed(target)
83+
|| currentNewerThanProposed(release)) {
8484
return t;
8585
}
86+
8687
Xml.Tag updated = filterTagChildren(t, compilerPluginConfig,
8788
child -> !("source".equals(child.getName()) || "target".equals(child.getName())));
8889
String releaseVersionValue = hasJavaVersionProperty(getCursor().firstEnclosingOrThrow(Xml.Document.class)) ?
@@ -93,12 +94,12 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
9394
};
9495
}
9596

96-
private boolean currentNewerThanProposed(@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<String> maybeRelease) {
97-
if (!maybeRelease.isPresent()) {
97+
private boolean currentNewerThanProposed(@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<String> config) {
98+
if (!config.isPresent()) {
9899
return false;
99100
}
100101
try {
101-
float currentVersion = Float.parseFloat(maybeRelease.get());
102+
float currentVersion = Float.parseFloat(config.get());
102103
float proposedVersion = Float.parseFloat(releaseVersion.toString());
103104
return proposedVersion < currentVersion;
104105
} catch (NumberFormatException e) {

rewrite-maven/src/test/java/org/openrewrite/maven/UseMavenCompilerPluginReleaseConfigurationTest.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
package org.openrewrite.maven;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.Arguments;
21+
import org.junit.jupiter.params.provider.MethodSource;
1922
import org.openrewrite.DocumentExample;
2023
import org.openrewrite.Issue;
2124
import org.openrewrite.test.RecipeSpec;
2225
import org.openrewrite.test.RewriteTest;
2326

27+
import java.util.stream.Stream;
28+
2429
import static org.openrewrite.java.Assertions.mavenProject;
2530
import static org.openrewrite.maven.Assertions.pomXml;
2631

@@ -621,4 +626,86 @@ void replacesSourceAndTargetConfig_InProfileSection() {
621626
)
622627
);
623628
}
629+
630+
@ParameterizedTest
631+
@MethodSource("mavenCompilerPluginConfig")
632+
void skipsUpdateIfExistingMavenCompilerPluginConfigIsNewerThanRequestedJavaVersion(String mavenCompilerPluginConfig) {
633+
rewriteRun(
634+
//language=xml
635+
pomXml(
636+
"""
637+
<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">
638+
<modelVersion>4.0.0</modelVersion>
639+
<groupId>org.sample</groupId>
640+
<artifactId>sample</artifactId>
641+
<version>1.0.0</version>
642+
""" + mavenCompilerPluginConfig +
643+
"""
644+
</project>
645+
"""
646+
)
647+
);
648+
}
649+
650+
private static Stream<Arguments> mavenCompilerPluginConfig() {
651+
return Stream.of(
652+
Arguments.of("""
653+
<build>
654+
<plugins>
655+
<plugin>
656+
<groupId>org.apache.maven.plugins</groupId>
657+
<artifactId>maven-compiler-plugin</artifactId>
658+
<version>3.8.0</version>
659+
<configuration>
660+
<source>17</source>
661+
</configuration>
662+
</plugin>
663+
</plugins>
664+
</build>
665+
"""),
666+
Arguments.of("""
667+
<build>
668+
<plugins>
669+
<plugin>
670+
<groupId>org.apache.maven.plugins</groupId>
671+
<artifactId>maven-compiler-plugin</artifactId>
672+
<version>3.8.0</version>
673+
<configuration>
674+
<target>17</target>
675+
</configuration>
676+
</plugin>
677+
</plugins>
678+
</build>
679+
"""),
680+
Arguments.of("""
681+
<build>
682+
<plugins>
683+
<plugin>
684+
<groupId>org.apache.maven.plugins</groupId>
685+
<artifactId>maven-compiler-plugin</artifactId>
686+
<version>3.8.0</version>
687+
<configuration>
688+
<source>17</source>
689+
<target>17</target>
690+
</configuration>
691+
</plugin>
692+
</plugins>
693+
</build>
694+
"""),
695+
Arguments.of("""
696+
<build>
697+
<plugins>
698+
<plugin>
699+
<groupId>org.apache.maven.plugins</groupId>
700+
<artifactId>maven-compiler-plugin</artifactId>
701+
<version>3.8.0</version>
702+
<configuration>
703+
<release>17</release>
704+
</configuration>
705+
</plugin>
706+
</plugins>
707+
</build>
708+
""")
709+
);
710+
}
624711
}

0 commit comments

Comments
 (0)