Skip to content

Commit

Permalink
Add explicit Maven plugin groupId for clarity (#3591)
Browse files Browse the repository at this point in the history
* Add explicit Maven plugin groupId for clarity

Fixes #3529

* Rename to ExplicitPluginGroupId
  • Loading branch information
timtebeek authored Oct 1, 2023
1 parent b87bc10 commit 4a6a157
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 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.cleanup;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenTagInsertionComparator;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.tree.Xml;

@Value
@EqualsAndHashCode(callSuper = true)
public class ExplicitPluginGroupId extends Recipe {
@Override
public String getDisplayName() {
return "Add explicit `groupId` to Maven plugins";
}

@Override
public String getDescription() {
return "Add the default `<groupId>org.apache.maven.plugins</groupId>` to plugins for clarity.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenVisitor<ExecutionContext>() {
@Override
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = (Xml.Tag) super.visitTag(tag, ctx);
if (isPluginTag() && !t.getChild("groupId").isPresent()) {
Xml.Tag groupIdTag = Xml.Tag.build("<groupId>org.apache.maven.plugins</groupId>");
t = (Xml.Tag) new AddToTagVisitor<>(t, groupIdTag, new MavenTagInsertionComparator(t.getChildren()))
.visitNonNull(t, ctx, getCursor().getParentOrThrow());
maybeUpdateModel();
}
return t;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ name: org.openrewrite.maven.BestPractices
displayName: Apache Maven best practices
description: Applies best practices to Maven POMs.
recipeList:
- org.openrewrite.maven.cleanup.ExplicitPluginGroupId
- org.openrewrite.maven.cleanup.PrefixlessExpressions

---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2023 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.cleanup;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.maven.Assertions.pomXml;

class ExplicitPluginGroupIdTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ExplicitPluginGroupId());
}

private static final String BEFORE = """
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
""";

private static final String AFTER = """
<project>
<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>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
""";

@Test
@DocumentExample
@Issue("https://github.com/openrewrite/rewrite/issues/3529")
void addGroupId() {
rewriteRun(pomXml(BEFORE, AFTER));
}

@Test
void noopIfPresent() {
rewriteRun(pomXml(AFTER));
}

@Test
void reportingPlugin() {
rewriteRun(
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</reporting>
</project>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</reporting>
</project>
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.openrewrite.maven.Assertions.pomXml;

class ManagedDependencyRequiresVersionTest implements RewriteTest {

@Issue("https://github.com/openrewrite/rewrite/issues/1084")
@Test
void dependencyManagementDependencyRequiresVersion() {
Expand Down

0 comments on commit 4a6a157

Please sign in to comment.