Skip to content

Commit

Permalink
AddAnnotationProcessor should compare and update versions using a p…
Browse files Browse the repository at this point in the history
…roperty (#4822)

* Add test to reproduce issue with annotation processors

* Add a second test and looking any variable version

* Update version tag if possible

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
nielsdebruin and timtebeek authored Dec 31, 2024
1 parent 2b8cdd0 commit 000a4f0
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openrewrite.xml.tree.Xml;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import static org.openrewrite.maven.trait.Traits.mavenPlugin;

Expand All @@ -49,7 +50,7 @@ public class AddAnnotationProcessor extends Recipe {

@Option(displayName = "Version",
description = "The third part of a coordinate 'org.projectlombok:lombok-mapstruct-binding:0.2.0' of the processor to add. " +
"Note that an exact version is expected",
"Note that an exact version is expected",
example = "0.2.0")
String version;

Expand All @@ -61,8 +62,8 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "Add an annotation processor to the maven compiler plugin. Will not do anything if it already exists. " +
"Also doesn't add anything when no other annotation processors are defined yet. " +
"(Perhaps `ChangePluginConfiguration` can be used).";
"Also doesn't add anything when no other annotation processors are defined yet. " +
"(Perhaps `ChangePluginConfiguration` can be used).";
}

@Override
Expand All @@ -73,23 +74,32 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag plugins = (Xml.Tag) super.visitTag(tag, ctx);
plugins = (Xml.Tag) mavenPlugin().asVisitor(plugin -> {
if (MAVEN_COMPILER_PLUGIN_GROUP_ID.equals(plugin.getGroupId()) &&
MAVEN_COMPILER_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) {
return new XmlIsoVisitor<ExecutionContext>() {
MAVEN_COMPILER_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) {
AtomicReference<TreeVisitor<?, ExecutionContext>> afterVisitor = new AtomicReference<>();
Xml.Tag modifiedPlugin = new XmlIsoVisitor<ExecutionContext>() {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag tg = super.visitTag(tag, ctx);
if ("annotationProcessorPaths".equals(tg.getName())) {
for (int i = 0; i < tg.getChildren().size(); i++) {
Xml.Tag child = tg.getChildren().get(i);
if (groupId.equals(child.getChildValue("groupId").orElse(null)) &&
artifactId.equals(child.getChildValue("artifactId").orElse(null))) {
artifactId.equals(child.getChildValue("artifactId").orElse(null))) {
if (!version.equals(child.getChildValue("version").orElse(null))) {
String oldVersion = child.getChildValue("version").orElse("");
VersionComparator comparator = Semver.validate(oldVersion, null).getValue();
if (comparator.compare(version, oldVersion) > 0) {
List<Xml.Tag> tags = tg.getChildren();
tags.set(i, child.withChildValue("version", version));
return tg.withContent(tags);
boolean oldVersionUsesProperty = oldVersion.startsWith("${");
String lookupVersion = oldVersionUsesProperty ?
getResolutionResult().getPom().getValue(oldVersion.trim()) :
oldVersion;
VersionComparator comparator = Semver.validate(lookupVersion, null).getValue();
if (comparator.compare(version, lookupVersion) > 0) {
if (oldVersionUsesProperty) {
afterVisitor.set(new ChangePropertyValue(oldVersion, version, null, null).getVisitor());
} else {
List<Xml.Tag> tags = tg.getChildren();
tags.set(i, child.withChildValue("version", version));
return tg.withContent(tags);
}
}
}
return tg;
Expand All @@ -102,6 +112,10 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
return tg;
}
}.visitTag(plugin.getTree(), ctx);
if (afterVisitor.get() != null) {
doAfterVisit(afterVisitor.get());
}
return modifiedPlugin;
}
return plugin.getTree();
}).visitNonNull(plugins, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,111 @@ void shouldUpdateProcessorVersionAlreadyPresent() {
)
);
}

@Test
void addAnnotationWithOlderVersionAsMavenProperty() {
rewriteRun(
spec -> spec.recipe(new AddAnnotationProcessor(
"org.projectlombok",
"lombok-mapstruct-binding",
"0.2.0"
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<properties>
<version.lombok.mapstruct.binding>0.1.0</version.lombok.mapstruct.binding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${version.lombok.mapstruct.binding}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<properties>
<version.lombok.mapstruct.binding>0.2.0</version.lombok.mapstruct.binding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${version.lombok.mapstruct.binding}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
);
}

@Test
void addAnnotationWithSameVersionAsMavenProperty() {
rewriteRun(
spec -> spec.recipe(new AddAnnotationProcessor(
"org.projectlombok",
"lombok-mapstruct-binding",
"0.2.0"
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<properties>
<version.lombok.mapstruct.binding>0.2.0</version.lombok.mapstruct.binding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${version.lombok.mapstruct.binding}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
);
}
}

0 comments on commit 000a4f0

Please sign in to comment.