Skip to content

Commit 48dcd43

Browse files
jkschneiderJonathan Schneider
andauthored
Honor Maven compiler processor paths when adding Lombok binding (#1361)
* 1319: reproduce binding already configured as a Maven processor * Clean regression fixture whitespace * 1319: respect module compiler paths before adding Lombok binding * 1319: retain compiler configuration after its null check * 1319: honor child overrides of managed processor paths * 1319: recognize locally declared non-inherited processors --------- Co-authored-by: Jonathan Schneider <jon@kandinsky.taila458f.ts.net>
1 parent a9b4ef9 commit 48dcd43

4 files changed

Lines changed: 626 additions & 17 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2026 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 com.fasterxml.jackson.databind.JsonNode;
19+
import lombok.EqualsAndHashCode;
20+
import lombok.Value;
21+
import org.openrewrite.ExecutionContext;
22+
import org.openrewrite.Recipe;
23+
import org.openrewrite.TreeVisitor;
24+
import org.openrewrite.internal.ListUtils;
25+
import org.openrewrite.maven.AddDependencyVisitor;
26+
import org.openrewrite.maven.MavenIsoVisitor;
27+
import org.openrewrite.maven.tree.Plugin;
28+
import org.openrewrite.maven.tree.ResolvedPom;
29+
import org.openrewrite.xml.tree.Xml;
30+
31+
import java.util.List;
32+
33+
@Value
34+
@EqualsAndHashCode(callSuper = false)
35+
public class AddLombokMapstructBindingMavenDependencyOnly extends Recipe {
36+
String displayName = "Add `lombok-mapstruct-binding` dependency for Maven when both MapStruct and Lombok are used";
37+
38+
String description = "Add the `lombok-mapstruct-binding` Maven dependency when this module uses both Lombok and MapStruct " +
39+
"and the binding is not already a dependency or configured on the compiler annotation processor path.";
40+
41+
@Override
42+
public TreeVisitor<?, ExecutionContext> getVisitor() {
43+
return new MavenIsoVisitor<ExecutionContext>() {
44+
@Override
45+
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
46+
if (getResolutionResult().findDependencies("org.projectlombok", "lombok", null).isEmpty() ||
47+
getResolutionResult().findDependencies("org.mapstruct", "mapstruct", null).isEmpty() ||
48+
!getResolutionResult().findDependencies("org.projectlombok", "lombok-mapstruct-binding", null).isEmpty()) {
49+
return document;
50+
}
51+
ResolvedPom pom = getResolutionResult().getPom();
52+
// The resolved model can omit even locally declared plugins with inherited=false.
53+
List<Plugin> plugins = ListUtils.concatAll(pom.getPlugins(), nonInheritedPlugins(pom.getRequested().getPlugins()));
54+
List<Plugin> managedPlugins = ListUtils.concatAll(pom.getPluginManagement(), nonInheritedPlugins(pom.getRequested().getPluginManagement()));
55+
if (hasBindingProcessor(pom, plugins) ||
56+
!overridesManagedProcessorPaths(plugins) && hasBindingProcessor(pom, managedPlugins)) {
57+
return document;
58+
}
59+
return (Xml.Document) new AddDependencyVisitor("org.projectlombok", "lombok-mapstruct-binding", "0.2.0",
60+
null, null, null, null, null, null, null).visitNonNull(document, ctx);
61+
}
62+
63+
private List<Plugin> nonInheritedPlugins(List<Plugin> plugins) {
64+
return ListUtils.map(plugins, plugin -> "false".equals(plugin.getInherited()) ? plugin : null);
65+
}
66+
67+
private boolean overridesManagedProcessorPaths(List<Plugin> plugins) {
68+
for (Plugin plugin : plugins) {
69+
if (!"org.apache.maven.plugins".equals(plugin.getGroupId()) ||
70+
!"maven-compiler-plugin".equals(plugin.getArtifactId())) {
71+
continue;
72+
}
73+
JsonNode configuration = plugin.getConfiguration();
74+
if (configuration != null &&
75+
("override".equals(configuration.path("combine.self").asText()) ||
76+
"override".equals(configuration.path("annotationProcessorPaths").path("combine.self").asText()))) {
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+
83+
private boolean hasBindingProcessor(ResolvedPom pom, List<Plugin> plugins) {
84+
for (Plugin plugin : plugins) {
85+
if (!"org.apache.maven.plugins".equals(plugin.getGroupId()) ||
86+
!"maven-compiler-plugin".equals(plugin.getArtifactId())) {
87+
continue;
88+
}
89+
JsonNode configuration = plugin.getConfiguration();
90+
if (configuration == null) {
91+
continue;
92+
}
93+
JsonNode paths = configuration.path("annotationProcessorPaths").path("path");
94+
if (paths.isArray()) {
95+
for (JsonNode path : paths) {
96+
if (isBinding(pom, path)) {
97+
return true;
98+
}
99+
}
100+
} else if (isBinding(pom, paths)) {
101+
return true;
102+
}
103+
}
104+
return false;
105+
}
106+
107+
private boolean isBinding(ResolvedPom pom, JsonNode path) {
108+
return "org.projectlombok".equals(pom.getValue(path.path("groupId").asText(""))) &&
109+
"lombok-mapstruct-binding".equals(pom.getValue(path.path("artifactId").asText("")));
110+
}
111+
};
112+
}
113+
}

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -379,20 +379,3 @@ recipeList:
379379
artifactId: lombok-mapstruct-binding
380380
version: 0.2.0
381381
- org.openrewrite.java.migrate.AddMapstructAnnotationProcessorPath
382-
---
383-
type: specs.openrewrite.org/v1beta/recipe
384-
name: org.openrewrite.java.migrate.AddLombokMapstructBindingMavenDependencyOnly
385-
displayName: Add `lombok-mapstruct-binding` dependency for Maven when both MapStruct and Lombok are used
386-
description: >-
387-
Add the `lombok-mapstruct-binding` Maven dependency when it does not already exist. Expects the caller to have checked
388-
that both MapStruct and Lombok are used.
389-
preconditions:
390-
- org.openrewrite.java.dependencies.search.DoesNotIncludeDependency:
391-
groupId: org.projectlombok
392-
artifactId: lombok-mapstruct-binding
393-
- org.openrewrite.Singleton
394-
recipeList:
395-
- org.openrewrite.maven.AddDependency:
396-
groupId: org.projectlombok
397-
artifactId: lombok-mapstruct-binding
398-
version: 0.2.0

0 commit comments

Comments
 (0)