Skip to content

Commit f13464e

Browse files
Wrapping the adding of the dependency for org.projectlombok:lombok-mapstruct-binding in a check to make sure you do not already have said dependency (potentially in another scope) (#944)
* Wrapping the adding of the dependency for `org.projectlombok:lombok-mapstruct-binding` in a check to make sure you do not already have said dependency (potentially in another scope) * Create a separate test class with separate tests for Maven and Gradle --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent a272a5a commit f13464e

File tree

2 files changed

+269
-4
lines changed

2 files changed

+269
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,25 @@ recipeList:
327327
version: 0.2.0
328328
configuration: annotationProcessor
329329
acceptTransitive: false
330-
- org.openrewrite.maven.AddDependency:
330+
- org.openrewrite.java.migrate.AddLombokMapstructBindingMavenDependencyOnly
331+
- org.openrewrite.maven.AddAnnotationProcessor:
331332
groupId: org.projectlombok
332333
artifactId: lombok-mapstruct-binding
333334
version: 0.2.0
334-
acceptTransitive: false
335-
- org.openrewrite.maven.AddAnnotationProcessor:
335+
---
336+
type: specs.openrewrite.org/v1beta/recipe
337+
name: org.openrewrite.java.migrate.AddLombokMapstructBindingMavenDependencyOnly
338+
displayName: Add `lombok-mapstruct-binding` dependency for Maven when both MapStruct and Lombok are used
339+
description: >-
340+
Add the `lombok-mapstruct-binding` when both MapStruct and Lombok are used, and the dependency does not already exist.
341+
Only to be called from `org.openrewrite.java.migrate.AddLombokMapstructBinding` to reduce redundant checks
342+
preconditions:
343+
- org.openrewrite.java.dependencies.search.DoesNotIncludeDependency:
344+
groupId: org.projectlombok
345+
artifactId: lombok-mapstruct-binding
346+
recipeList:
347+
- org.openrewrite.maven.AddDependency:
336348
groupId: org.projectlombok
337349
artifactId: lombok-mapstruct-binding
338350
version: 0.2.0
339-
351+
acceptTransitive: false
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
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 org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.gradle.Assertions.buildGradle;
24+
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
25+
import static org.openrewrite.java.Assertions.mavenProject;
26+
import static org.openrewrite.maven.Assertions.pomXml;
27+
28+
class AddLombokMapstructBindingTest implements RewriteTest {
29+
@Override
30+
public void defaults(RecipeSpec spec) {
31+
spec.recipeFromResources("org.openrewrite.java.migrate.AddLombokMapstructBinding");
32+
}
33+
34+
@DocumentExample
35+
@Test
36+
void addForGradle() {
37+
rewriteRun(
38+
spec -> spec.beforeRecipe(withToolingApi()),
39+
mavenProject("project",
40+
//language=groovy
41+
buildGradle(
42+
"""
43+
plugins { id 'java' }
44+
repositories { mavenCentral() }
45+
dependencies {
46+
implementation "org.projectlombok:lombok:1.18.42"
47+
implementation "org.mapstruct:mapstruct:1.6.3"
48+
}
49+
""",
50+
"""
51+
plugins { id 'java' }
52+
repositories { mavenCentral() }
53+
dependencies {
54+
annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.2.0"
55+
56+
implementation "org.projectlombok:lombok:1.18.42"
57+
implementation "org.mapstruct:mapstruct:1.6.3"
58+
}
59+
"""
60+
)
61+
)
62+
);
63+
}
64+
65+
@Test
66+
void doesNotDuplicateGradle() {
67+
rewriteRun(
68+
spec -> spec.beforeRecipe(withToolingApi()),
69+
mavenProject("project",
70+
//language=groovy
71+
buildGradle(
72+
"""
73+
plugins { id 'java' }
74+
repositories { mavenCentral() }
75+
dependencies {
76+
annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.2.0"
77+
78+
implementation "org.projectlombok:lombok:1.18.42"
79+
implementation "org.mapstruct:mapstruct:1.6.3"
80+
}
81+
"""
82+
)
83+
)
84+
);
85+
}
86+
87+
@Test
88+
void addForMaven() {
89+
rewriteRun(
90+
mavenProject("project",
91+
//language=xml
92+
pomXml(
93+
"""
94+
<project>
95+
<groupId>com.mycompany.app</groupId>
96+
<artifactId>my-app</artifactId>
97+
<version>1</version>
98+
<dependencies>
99+
<dependency>
100+
<groupId>org.projectlombok</groupId>
101+
<artifactId>lombok</artifactId>
102+
<version>1.18.42</version>
103+
</dependency>
104+
<dependency>
105+
<groupId>org.mapstruct</groupId>
106+
<artifactId>mapstruct</artifactId>
107+
<version>1.6.3</version>
108+
</dependency>
109+
</dependencies>
110+
<plugins>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-compiler-plugin</artifactId>
114+
<configuration>
115+
<annotationProcessorPaths>
116+
<path>
117+
<groupId>org.projectlombok</groupId>
118+
<artifactId>lombok</artifactId>
119+
</path>
120+
</annotationProcessorPaths>
121+
</configuration>
122+
<version>3.14.1</version>
123+
</plugin>
124+
</plugins>
125+
</project>
126+
""",
127+
"""
128+
<project>
129+
<groupId>com.mycompany.app</groupId>
130+
<artifactId>my-app</artifactId>
131+
<version>1</version>
132+
<dependencies>
133+
<dependency>
134+
<groupId>org.projectlombok</groupId>
135+
<artifactId>lombok</artifactId>
136+
<version>1.18.42</version>
137+
</dependency>
138+
<dependency>
139+
<groupId>org.projectlombok</groupId>
140+
<artifactId>lombok-mapstruct-binding</artifactId>
141+
<version>0.2.0</version>
142+
</dependency>
143+
<dependency>
144+
<groupId>org.mapstruct</groupId>
145+
<artifactId>mapstruct</artifactId>
146+
<version>1.6.3</version>
147+
</dependency>
148+
</dependencies>
149+
<plugins>
150+
<plugin>
151+
<groupId>org.apache.maven.plugins</groupId>
152+
<artifactId>maven-compiler-plugin</artifactId>
153+
<configuration>
154+
<annotationProcessorPaths>
155+
<path>
156+
<groupId>org.projectlombok</groupId>
157+
<artifactId>lombok</artifactId>
158+
</path>
159+
<path>
160+
<groupId>org.projectlombok</groupId>
161+
<artifactId>lombok-mapstruct-binding</artifactId>
162+
<version>0.2.0</version>
163+
</path>
164+
</annotationProcessorPaths>
165+
</configuration>
166+
<version>3.14.1</version>
167+
</plugin>
168+
</plugins>
169+
</project>
170+
"""
171+
),
172+
mavenProject("anotherProject",
173+
//language=xml
174+
pomXml(
175+
"""
176+
<project>
177+
<groupId>org.someother</groupId>
178+
<artifactId>anotherproject</artifactId>
179+
<version>1.0.0</version>
180+
<properties>
181+
<maven.compiler.release>17</maven.compiler.release>
182+
</properties>
183+
<dependencies>
184+
</dependencies>
185+
</project>
186+
"""
187+
)
188+
)
189+
)
190+
);
191+
}
192+
193+
@Test
194+
void doesNotDuplicateMaven() {
195+
rewriteRun(
196+
mavenProject("project",
197+
//language=xml
198+
pomXml(
199+
"""
200+
<project>
201+
<groupId>com.mycompany.app</groupId>
202+
<artifactId>my-app</artifactId>
203+
<version>1</version>
204+
<properties>
205+
<maven.compiler.release>17</maven.compiler.release>
206+
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
207+
</properties>
208+
<dependencies>
209+
<dependency>
210+
<groupId>org.projectlombok</groupId>
211+
<artifactId>lombok</artifactId>
212+
<version>1.18.42</version>
213+
<scope>provided</scope>
214+
</dependency>
215+
<dependency>
216+
<groupId>org.projectlombok</groupId>
217+
<artifactId>lombok-mapstruct-binding</artifactId>
218+
<version>${lombok.mapstruct.binding.version}</version>
219+
<scope>provided</scope>
220+
</dependency>
221+
<dependency>
222+
<groupId>org.mapstruct</groupId>
223+
<artifactId>mapstruct</artifactId>
224+
<version>1.6.3</version>
225+
</dependency>
226+
</dependencies>
227+
<plugins>
228+
<plugin>
229+
<groupId>org.apache.maven.plugins</groupId>
230+
<artifactId>maven-compiler-plugin</artifactId>
231+
<configuration>
232+
<annotationProcessorPaths>
233+
<path>
234+
<groupId>org.projectlombok</groupId>
235+
<artifactId>lombok</artifactId>
236+
</path>
237+
<path>
238+
<groupId>org.projectlombok</groupId>
239+
<artifactId>lombok-mapstruct-binding</artifactId>
240+
<version>0.2.0</version>
241+
</path>
242+
</annotationProcessorPaths>
243+
</configuration>
244+
<version>3.14.1</version>
245+
</plugin>
246+
</plugins>
247+
</project>
248+
"""
249+
)
250+
)
251+
);
252+
}
253+
}

0 commit comments

Comments
 (0)