Skip to content

Commit e318692

Browse files
committed
add comments and advance tests
1 parent 557ba1d commit e318692

2 files changed

Lines changed: 128 additions & 56 deletions

File tree

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

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6969
return tree;
7070
}
7171

72+
// if we find more than one project pom, it has to be multi-module
7273
boolean isMultiModule = tree.getMarkers()
7374
.findFirst(MavenResolutionResult.class)
7475
.map(MavenResolutionResult::getProjectPoms)
@@ -79,70 +80,72 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
7980
// maybe add the plugin to //build/pluginManagement/plugins or //build/plugins is not present yet
8081
tree = new AddPluginVisitor(isMultiModule, MAVEN_COMPILER_PLUGIN_GROUP_ID, MAVEN_COMPILER_PLUGIN_ARTIFACT_ID, null, "<configuration><annotationProcessorPaths/></configuration>", null, null, null).visit(tree, ctx);
8182

82-
return new AddAnnotationProcessorPath(isMultiModule).visit(tree, ctx);
83-
}
84-
85-
class AddAnnotationProcessorPath extends MavenIsoVisitor<ExecutionContext> {
86-
private final boolean addToManaged;
83+
// configure the plugin adding process
84+
return new MavenIsoVisitor<ExecutionContext>() {
85+
@Override
86+
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
87+
Xml.Tag plugins = super.visitTag(tag, ctx);
88+
plugins = (Xml.Tag) new MavenPlugin.Matcher(isMultiModule, MAVEN_COMPILER_PLUGIN_GROUP_ID, MAVEN_COMPILER_PLUGIN_ARTIFACT_ID).asVisitor(plugin -> {
89+
90+
MavenResolutionResult mrr = getResolutionResult();
91+
AtomicReference<TreeVisitor<?, ExecutionContext>> maybePropertyUpdate = new AtomicReference<>();
92+
Xml.Tag modifiedPlugin = new XmlIsoVisitor<ExecutionContext>() {
93+
@Override
94+
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
95+
Xml.Tag tg = super.visitTag(tag, ctx);
96+
97+
if (!"annotationProcessorPaths".equals(tg.getName())) {
98+
return tg;
99+
}
87100

88-
AddAnnotationProcessorPath(boolean addToManaged) {
89-
this.addToManaged = addToManaged;
90-
}
101+
// iterate the children (annotation processor paths) and try to update the version
102+
for (int i = 0; i < tg.getChildren().size(); i++) {
103+
Xml.Tag child = tg.getChildren().get(i);
104+
if (!groupId.equals(child.getChildValue("groupId").orElse(null)) ||
105+
!artifactId.equals(child.getChildValue("artifactId").orElse(null))) {
106+
continue;
107+
}
91108

92-
@Override
93-
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
94-
Xml.Tag plugins = super.visitTag(tag, ctx);
95-
plugins = (Xml.Tag) new MavenPlugin.Matcher(addToManaged,MAVEN_COMPILER_PLUGIN_GROUP_ID, MAVEN_COMPILER_PLUGIN_ARTIFACT_ID).asVisitor(plugin -> {
96-
97-
MavenResolutionResult mrr = getResolutionResult();
98-
AtomicReference<TreeVisitor<?, ExecutionContext>> afterVisitor = new AtomicReference<>();
99-
Xml.Tag modifiedPlugin = new XmlIsoVisitor<ExecutionContext>() {
100-
@Override
101-
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
102-
Xml.Tag tg = super.visitTag(tag, ctx);
103-
if (!"annotationProcessorPaths".equals(tg.getName())) {
104-
return tg;
105-
}
106-
for (int i = 0; i < tg.getChildren().size(); i++) {
107-
Xml.Tag child = tg.getChildren().get(i);
108-
if (!groupId.equals(child.getChildValue("groupId").orElse(null)) ||
109-
!artifactId.equals(child.getChildValue("artifactId").orElse(null))) {
110-
continue;
111-
}
112-
if (!version.equals(child.getChildValue("version").orElse(null))) {
113-
String oldVersion = child.getChildValue("version").orElse("");
114-
boolean oldVersionUsesProperty = oldVersion.startsWith("${");
115-
String lookupVersion = oldVersionUsesProperty ?
116-
mrr.getPom().getValue(oldVersion.trim()) :
117-
oldVersion;
118-
VersionComparator comparator = Semver.validate(lookupVersion, null).getValue();
119-
if (comparator.compare(version, lookupVersion) > 0) {
120-
if (oldVersionUsesProperty) {
121-
afterVisitor.set(new ChangePropertyValue(oldVersion, version, null, null).getVisitor());
122-
} else {
123-
List<Xml.Tag> tags = tg.getChildren();
124-
tags.set(i, child.withChildValue("version", version));
125-
return tg.withContent(tags);
109+
if (!version.equals(child.getChildValue("version").orElse(null))) {
110+
String oldVersion = child.getChildValue("version").orElse("");
111+
boolean oldVersionUsesProperty = oldVersion.startsWith("${");
112+
String lookupVersion = oldVersionUsesProperty ? mrr.getPom().getValue(oldVersion.trim()) : oldVersion;
113+
VersionComparator comparator = Semver.validate(lookupVersion, null).getValue();
114+
if (comparator.compare(version, lookupVersion) > 0) {
115+
if (oldVersionUsesProperty) {
116+
// a maven property is used here, update in properties section later
117+
maybePropertyUpdate.set(new ChangePropertyValue(oldVersion, version, null, null).getVisitor());
118+
} else {
119+
// update the paths version directly
120+
List<Xml.Tag> tags = tg.getChildren();
121+
tags.set(i, child.withChildValue("version", version));
122+
return tg.withContent(tags);
123+
}
126124
}
127125
}
126+
127+
return tg;
128128
}
129-
return tg;
129+
130+
// not found so we add it
131+
return tg.withContent(ListUtils.concat(tg.getChildren(), Xml.Tag.build(String.format(
132+
"<path>\n<groupId>%s</groupId>\n<artifactId>%s</artifactId>\n<version>%s</version>\n</path>",
133+
groupId, artifactId, version))));
130134
}
131-
return tg.withContent(ListUtils.concat(tg.getChildren(), Xml.Tag.build(String.format(
132-
"<path>\n<groupId>%s</groupId>\n<artifactId>%s</artifactId>\n<version>%s</version>\n</path>",
133-
groupId, artifactId, version))));
135+
}.visitTag(plugin.getTree(), ctx);
136+
137+
if (maybePropertyUpdate.get() != null) {
138+
doAfterVisit(maybePropertyUpdate.get());
134139
}
135-
}.visitTag(plugin.getTree(), ctx);
136-
if (afterVisitor.get() != null) {
137-
doAfterVisit(afterVisitor.get());
140+
141+
return modifiedPlugin;
142+
}).visitNonNull(plugins, 0);
143+
if (plugins != tag) {
144+
plugins = autoFormat(plugins, ctx);
138145
}
139-
return modifiedPlugin;
140-
}).visitNonNull(plugins, 0);
141-
if (plugins != tag) {
142-
plugins = autoFormat(plugins, ctx);
146+
return plugins;
143147
}
144-
return plugins;
145-
}
148+
}.visit(tree, ctx);
146149
}
147150
};
148151
}

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ void createPluginManagementWhenMissing() {
849849
}
850850

851851
@Test
852-
void childModuleUnchanged() {
852+
void leaveChildPluginsUntouched() {
853853
// Multi-module: child module has its own maven-compiler-plugin
854854
// Expected: child remains untouched, only root gets pluginManagement entry
855855
rewriteRun(
@@ -933,6 +933,75 @@ void childModuleUnchanged() {
933933
);
934934
}
935935

936+
@Test
937+
void childIsUntouched() {
938+
// Multi-module: child module has no maven-compiler-plugin
939+
// Expected: child remains untouched, only root gets pluginManagement entry
940+
rewriteRun(
941+
pomXml(
942+
"""
943+
<project>
944+
<modelVersion>4.0.0</modelVersion>
945+
<groupId>com.mycompany.app</groupId>
946+
<artifactId>parent</artifactId>
947+
<version>1</version>
948+
<packaging>pom</packaging>
949+
<modules>
950+
<module>child</module>
951+
</modules>
952+
</project>
953+
""",
954+
"""
955+
<project>
956+
<modelVersion>4.0.0</modelVersion>
957+
<groupId>com.mycompany.app</groupId>
958+
<artifactId>parent</artifactId>
959+
<version>1</version>
960+
<packaging>pom</packaging>
961+
<modules>
962+
<module>child</module>
963+
</modules>
964+
<build>
965+
<pluginManagement>
966+
<plugins>
967+
<plugin>
968+
<groupId>org.apache.maven.plugins</groupId>
969+
<artifactId>maven-compiler-plugin</artifactId>
970+
<configuration>
971+
<annotationProcessorPaths>
972+
<path>
973+
<groupId>org.projectlombok</groupId>
974+
<artifactId>lombok-mapstruct-binding</artifactId>
975+
<version>0.2.0</version>
976+
</path>
977+
</annotationProcessorPaths>
978+
</configuration>
979+
</plugin>
980+
</plugins>
981+
</pluginManagement>
982+
</build>
983+
</project>
984+
"""
985+
),
986+
mavenProject("child",
987+
pomXml(
988+
// Child has nothing - should NOT be modified
989+
"""
990+
<project>
991+
<modelVersion>4.0.0</modelVersion>
992+
<parent>
993+
<groupId>com.mycompany.app</groupId>
994+
<artifactId>parent</artifactId>
995+
<version>1</version>
996+
</parent>
997+
<artifactId>child</artifactId>
998+
</project>
999+
"""
1000+
)
1001+
)
1002+
);
1003+
}
1004+
9361005
@Test
9371006
void addOnlyToPluginManagementWhenInBothLocations() {
9381007
// Multi-module: root has plugin in both build/plugins and pluginManagement

0 commit comments

Comments
 (0)