-
Notifications
You must be signed in to change notification settings - Fork 500
Add AddAnnotationProcessor recipe
#4694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cd4d47a
Add `AddAnnotationProcessor` recipe
Laurens-W 616ed0d
Use `MavenPlugin` trait
Laurens-W 0c79452
Add more test coverage and clearer test names
Laurens-W 56e2c16
Lil cleanup
Laurens-W 4c98e85
Lil cleanup
Laurens-W b6cc859
Merge branch 'main' into add-maven-annotation-processor
timtebeek 3a6bace
Show handling of missing groupId and mismatched processor version
timtebeek c9946d1
Minor polish
timtebeek a8e9349
Apply suggestions from code review
timtebeek 79cfe61
Replace existing version if newer version is higher
Laurens-W File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
rewrite-maven/src/main/java/org/openrewrite/maven/AddAnnotationProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * Copyright 2024 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.maven; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Value; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Option; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.semver.Semver; | ||
| import org.openrewrite.semver.VersionComparator; | ||
| import org.openrewrite.xml.XmlIsoVisitor; | ||
| import org.openrewrite.xml.tree.Xml; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.openrewrite.maven.trait.Traits.mavenPlugin; | ||
|
|
||
| @Value | ||
| @EqualsAndHashCode(callSuper = false) | ||
| public class AddAnnotationProcessor extends Recipe { | ||
| private static final String MAVEN_COMPILER_PLUGIN_GROUP_ID = "org.apache.maven.plugins"; | ||
| private static final String MAVEN_COMPILER_PLUGIN_ARTIFACT_ID = "maven-compiler-plugin"; | ||
|
|
||
| @Option(displayName = "Group", | ||
| description = "The first part of the coordinate 'org.projectlombok:lombok-mapstruct-binding:0.2.0' of the processor to add.", | ||
| example = "org.projectlombok") | ||
| String groupId; | ||
|
|
||
| @Option(displayName = "Artifact", | ||
| description = "The second part of a coordinate 'org.projectlombok:lombok-mapstruct-binding:0.2.0' of the processor to add.", | ||
| example = "lombok-mapstruct-binding") | ||
| String artifactId; | ||
|
|
||
| @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", | ||
| example = "0.2.0") | ||
| String version; | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Add an annotation processor to `maven-compiler-plugin`"; | ||
| } | ||
|
|
||
| @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)."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new MavenVisitor<ExecutionContext>() { | ||
| @Override | ||
| 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>() { | ||
| @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))) { | ||
| 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); | ||
| } | ||
| } | ||
| return tg; | ||
| } | ||
| } | ||
| return tg.withContent(ListUtils.concat(tg.getChildren(), Xml.Tag.build(String.format( | ||
| "<path>\n<groupId>%s</groupId>\n<artifactId>%s</artifactId>\n<version>%s</version>\n</path>", | ||
| groupId, artifactId, version)))); | ||
| } | ||
| return tg; | ||
| } | ||
| }.visitTag(plugin.getTree(), ctx); | ||
| } | ||
| return plugin.getTree(); | ||
| }).visitNonNull(plugins, 0); | ||
| if (plugins != tag) { | ||
| plugins = autoFormat(plugins, ctx); | ||
| } | ||
| return plugins; | ||
| } | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
181 changes: 181 additions & 0 deletions
181
rewrite-maven/src/test/java/org/openrewrite/maven/AddAnnotationProcessorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Copyright 2024 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.maven; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.maven.Assertions.pomXml; | ||
|
|
||
| class AddAnnotationProcessorTest implements RewriteTest { | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
| void addAnnotationProcessor() { | ||
| 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> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <annotationProcessorPaths> | ||
| <path> | ||
| <groupId>org.mapstruct</groupId> | ||
| <artifactId>mapstruct-processor</artifactId> | ||
| </path> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| </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> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <annotationProcessorPaths> | ||
| <path> | ||
| <groupId>org.mapstruct</groupId> | ||
| <artifactId>mapstruct-processor</artifactId> | ||
| </path> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| </path> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok-mapstruct-binding</artifactId> | ||
| <version>0.2.0</version> | ||
| </path> | ||
| </annotationProcessorPaths> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldUpdateProcessorVersionAlreadyPresent() { | ||
| 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> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <annotationProcessorPaths> | ||
| <path> | ||
| <groupId>org.mapstruct</groupId> | ||
| <artifactId>mapstruct-processor</artifactId> | ||
| </path> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| </path> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok-mapstruct-binding</artifactId> | ||
| <version>0.1.0</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> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <annotationProcessorPaths> | ||
| <path> | ||
| <groupId>org.mapstruct</groupId> | ||
| <artifactId>mapstruct-processor</artifactId> | ||
| </path> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| </path> | ||
| <path> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok-mapstruct-binding</artifactId> | ||
| <version>0.2.0</version> | ||
| </path> | ||
| </annotationProcessorPaths> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd intentionally changed this to use a mismatched version; should we perhaps override any existing version here? Or only when lower than the version we're intended to add?