Skip to content

Commit c17f866

Browse files
authored
Add maven-compiler-plugin version when missing during Java migration (#989)
When maven-compiler-plugin has no <version> tag and uses <source>/<target>, the migration converts to <release> but the default plugin version (3.1) doesn't support it. Set addVersionIfMissing on UpgradePluginVersion so a compatible version is always present.
1 parent 56b280c commit c17f866

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ recipeList:
110110
groupId: org.apache.maven.plugins
111111
artifactId: maven-compiler-plugin
112112
newVersion: 3.6.2
113+
addVersionIfMissing: true
113114
- org.openrewrite.github.SetupJavaUpgradeJavaVersion:
114115
minimumJavaMajorVersion: 11
115116
- org.openrewrite.maven.UpgradePluginVersion:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ recipeList:
110110
groupId: org.apache.maven.plugins
111111
artifactId: maven-compiler-plugin
112112
newVersion: 3.x
113+
addVersionIfMissing: true
113114
- org.openrewrite.maven.UpgradePluginVersion:
114115
groupId: org.apache.maven.plugins
115116
artifactId: maven-war-plugin

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ recipeList:
207207
groupId: org.apache.maven.plugins
208208
artifactId: maven-compiler-plugin
209209
newVersion: 3.15.x
210+
addVersionIfMissing: true
210211
- org.openrewrite.maven.UpgradePluginVersion:
211212
groupId: org.apache.maven.plugins
212213
artifactId: maven-surefire-plugin

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,86 @@ void needToJaxb2MavenPlugin() {
7070
);
7171
}
7272

73+
@Test
74+
void compilerPluginVersionAddedWhenMissing() {
75+
rewriteRun(
76+
version(
77+
mavenProject("project",
78+
//language=xml
79+
pomXml(
80+
"""
81+
<project>
82+
<groupId>com.mycompany.app</groupId>
83+
<artifactId>my-app</artifactId>
84+
<version>1</version>
85+
<build>
86+
<plugins>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-compiler-plugin</artifactId>
90+
<configuration>
91+
<source>1.5</source>
92+
<target>1.5</target>
93+
</configuration>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
</project>
98+
""",
99+
spec -> spec.after(pomXml -> {
100+
assertThat(pomXml).contains("<version>3.");
101+
return pomXml;
102+
})
103+
)
104+
),
105+
8)
106+
);
107+
}
108+
109+
@Test
110+
void compilerPluginVersionAddedAndReleaseSetForFullMigration() {
111+
rewriteRun(
112+
spec -> spec.recipe(Environment.builder()
113+
.scanRuntimeClasspath("org.openrewrite.java.migrate")
114+
.build()
115+
.activateRecipes("org.openrewrite.java.migrate.Java8toJava11")),
116+
version(
117+
mavenProject("project",
118+
//language=xml
119+
pomXml(
120+
"""
121+
<project>
122+
<groupId>com.mycompany.app</groupId>
123+
<artifactId>my-app</artifactId>
124+
<version>1</version>
125+
<build>
126+
<plugins>
127+
<plugin>
128+
<groupId>org.apache.maven.plugins</groupId>
129+
<artifactId>maven-compiler-plugin</artifactId>
130+
<configuration>
131+
<source>1.5</source>
132+
<target>1.5</target>
133+
</configuration>
134+
</plugin>
135+
</plugins>
136+
</build>
137+
</project>
138+
""",
139+
spec -> spec.after(pomXml -> {
140+
assertThat(pomXml)
141+
.contains("<release>11</release>")
142+
.contains("<version>3.")
143+
.doesNotContain("<source>")
144+
.doesNotContain("<target>");
145+
return pomXml;
146+
})
147+
)
148+
),
149+
8)
150+
);
151+
}
152+
73153
@Test
74154
void noChangeOnCorrectJaxb2MavenPluginVersion() {
75155
rewriteRun(

0 commit comments

Comments
 (0)