Skip to content

Commit 7f5d757

Browse files
committed
Support for ban-commons-lang-2.skip
1 parent 624a641 commit 7f5d757

5 files changed

Lines changed: 252 additions & 2 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.jenkins.tools.pluginmodernizer.core.recipes;
2+
3+
import io.jenkins.tools.pluginmodernizer.core.visitors.AddLastPropertyVisitor;
4+
import org.openrewrite.ExecutionContext;
5+
import org.openrewrite.Option;
6+
import org.openrewrite.Recipe;
7+
import org.openrewrite.TreeVisitor;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
/**
12+
* Add a Maven property at the end of the properties section
13+
*/
14+
public class AddProperty extends Recipe {
15+
16+
@Option(
17+
displayName = "Property name",
18+
description = "Key name of the property to remove.",
19+
example = "configuration-as-code.version")
20+
String key;
21+
22+
@Option(displayName = "Property value", description = "Value of the property to add.", example = "1.0.0")
23+
String value;
24+
25+
/**
26+
* Logger
27+
*/
28+
private static final Logger LOG = LoggerFactory.getLogger(RemoveProperty.class);
29+
30+
public AddProperty(String key, String value) {
31+
this.key = key;
32+
this.value = value;
33+
}
34+
35+
@Override
36+
public TreeVisitor<?, ExecutionContext> getVisitor() {
37+
return new AddLastPropertyVisitor(key, value);
38+
}
39+
40+
@Override
41+
public String getDisplayName() {
42+
return "Add a Maven property at the end of the properties section";
43+
}
44+
45+
@Override
46+
public String getDescription() {
47+
return "Add a Maven property at the end of the properties section.";
48+
}
49+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.jenkins.tools.pluginmodernizer.core.visitors;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import org.openrewrite.ExecutionContext;
6+
import org.openrewrite.maven.MavenIsoVisitor;
7+
import org.openrewrite.xml.tree.Content;
8+
import org.openrewrite.xml.tree.Xml;
9+
10+
/**
11+
* A visitor that add a maven property at the end of the properties section
12+
*/
13+
public class AddLastPropertyVisitor extends MavenIsoVisitor<ExecutionContext> {
14+
15+
/**
16+
* The property name to add.
17+
*/
18+
private final String propertyName;
19+
20+
/**
21+
* The property value to add.
22+
*/
23+
private final String propertyValue;
24+
25+
/**
26+
* Constructor of the visitor.
27+
* @param propertyName The property name to add.
28+
* @param propertyValue The property value to add.
29+
*/
30+
public AddLastPropertyVisitor(String propertyName, String propertyValue) {
31+
this.propertyName = propertyName;
32+
this.propertyValue = propertyValue;
33+
}
34+
35+
@Override
36+
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) {
37+
tag = super.visitTag(tag, executionContext);
38+
if (tag.getName().equals("properties")) {
39+
40+
// Ensure value if exists
41+
Xml.Tag existingPropertyTag = tag.getChild(propertyName).orElse(null);
42+
if (existingPropertyTag != null && existingPropertyTag.getValue().isPresent()) {
43+
return tag;
44+
}
45+
46+
// Ensure previous
47+
Xml.Tag previousPropertyTag = tag.getChildren().getFirst();
48+
49+
// Add new property at the end
50+
List<Content> contents = new ArrayList<>();
51+
if (tag.getContent() != null) {
52+
contents.addAll(tag.getContent());
53+
}
54+
Xml.Tag newPropertyTag =
55+
Xml.Tag.build("<" + propertyName + ">" + propertyValue + "</" + propertyName + ">");
56+
newPropertyTag = newPropertyTag.withPrefix(previousPropertyTag.getPrefix());
57+
contents.add(newPropertyTag);
58+
59+
tag = tag.withContent(contents);
60+
}
61+
62+
return tag;
63+
}
64+
}

plugin-modernizer-core/src/main/resources/META-INF/rewrite/recipes.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ recipeList:
117117
- io.jenkins.tools.pluginmodernizer.core.recipes.UpdateParent:
118118
majorVersionFilter: 5
119119
- io.jenkins.tools.pluginmodernizer.core.recipes.EnsureRelativePath
120-
- org.openrewrite.maven.AddProperty:
120+
- io.jenkins.tools.pluginmodernizer.core.recipes.AddProperty:
121121
key: ban-junit4-imports.skip
122122
value: false
123123
- io.jenkins.tools.pluginmodernizer.core.recipes.AnnotateWithJenkins
@@ -190,6 +190,9 @@ displayName: Migrate Commons Lang from 2 to 3 and StringEscapeUtils to Commons T
190190
description: Migrate Commons Lang from 2 to 3 and StringEscapeUtils to Commons Text.
191191
tags: ['dependencies', 'migration']
192192
recipeList:
193+
- io.jenkins.tools.pluginmodernizer.core.recipes.AddProperty:
194+
key: ban-commons-lang-2.skip
195+
value: false
193196
# upgrade Commons Lang from 2 to 3
194197
- org.openrewrite.maven.AddDependency:
195198
groupId: io.jenkins.plugins

plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/DeclarativeRecipesTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ void upgradeToRecommendCoreVersionTest() {
729729
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
730730
<jenkins.baseline>%s</jenkins.baseline>
731731
<jenkins.version>${jenkins.baseline}.%s</jenkins.version>
732+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
732733
</properties>
733734
<dependencyManagement>
734735
<dependencies>
@@ -898,6 +899,7 @@ void upgradeToRecommendCoreVersionTestWithoutPluginDependencies() {
898899
<maven.compiler.source>17</maven.compiler.source>
899900
<maven.compiler.release>17</maven.compiler.release>
900901
<maven.compiler.target>17</maven.compiler.target>
902+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
901903
</properties>
902904
<repositories>
903905
<repository>
@@ -933,6 +935,7 @@ void upgradeToRecommendCoreVersionTestWithoutPluginDependencies() {
933935
<properties>
934936
<jenkins.version>%s</jenkins.version>
935937
<jenkins-test-harness.version>%s</jenkins-test-harness.version>
938+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
936939
</properties>
937940
<repositories>
938941
<repository>
@@ -995,6 +998,7 @@ void upgradeToRecommendCoreVersionTestWithBaseline() {
995998
<jenkins-test-harness.version>2.41.1</jenkins-test-harness.version>
996999
<jenkins.baseline>2.440</jenkins.baseline>
9971000
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
1001+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
9981002
</properties>
9991003
<dependencyManagement>
10001004
<dependencies>
@@ -1047,6 +1051,7 @@ void upgradeToRecommendCoreVersionTestWithBaseline() {
10471051
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
10481052
<jenkins.baseline>%s</jenkins.baseline>
10491053
<jenkins.version>${jenkins.baseline}.%s</jenkins.version>
1054+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
10501055
</properties>
10511056
<dependencyManagement>
10521057
<dependencies>
@@ -1132,6 +1137,7 @@ void upgradeToRecommendCoreVersionTestWithMultipleBom() {
11321137
<revision>2.17.0</revision>
11331138
<changelist>999999-SNAPSHOT</changelist>
11341139
<jenkins.version>2.516.1</jenkins.version>
1140+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
11351141
</properties>
11361142
<repositories>
11371143
<repository>
@@ -1206,6 +1212,7 @@ void upgradeToRecommendCoreVersionTestWithMultipleBom() {
12061212
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
12071213
<jenkins.baseline>%s</jenkins.baseline>
12081214
<jenkins.version>${jenkins.baseline}.%s</jenkins.version>
1215+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
12091216
</properties>
12101217
<repositories>
12111218
<repository>
@@ -1318,6 +1325,7 @@ void upgradeToUpgradeToLatestJava11CoreVersion() {
13181325
<properties>
13191326
<jenkins-test-harness.version>2.41.1</jenkins-test-harness.version>
13201327
<jenkins.version>2.440.3</jenkins.version>
1328+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
13211329
</properties>
13221330
<dependencyManagement>
13231331
<dependencies>
@@ -1379,6 +1387,7 @@ void upgradeToUpgradeToLatestJava11CoreVersion() {
13791387
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
13801388
<jenkins.baseline>2.462</jenkins.baseline>
13811389
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
1390+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
13821391
</properties>
13831392
<dependencyManagement>
13841393
<dependencies>
@@ -1521,6 +1530,7 @@ void upgradeToUpgradeToLatestJava8CoreVersion() {
15211530
<properties>
15221531
<jenkins-test-harness.version>2.41.1</jenkins-test-harness.version>
15231532
<jenkins.version>2.303.3</jenkins.version>
1533+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
15241534
</properties>
15251535
<dependencyManagement>
15261536
<dependencies>
@@ -1576,6 +1586,7 @@ void upgradeToUpgradeToLatestJava8CoreVersion() {
15761586
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
15771587
<jenkins.baseline>2.346</jenkins.baseline>
15781588
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
1589+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
15791590
</properties>
15801591
<dependencyManagement>
15811592
<dependencies>
@@ -2037,6 +2048,7 @@ void upgradeNextMajorParentVersionTest() {
20372048
<maven.compiler.source>11</maven.compiler.source>
20382049
<maven.compiler.target>11</maven.compiler.target>
20392050
<maven.compiler.release>11</maven.compiler.release>
2051+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
20402052
</properties>
20412053
<dependencies>
20422054
<dependency>
@@ -2080,6 +2092,7 @@ void upgradeNextMajorParentVersionTest() {
20802092
<properties>
20812093
<jenkins-test-harness.version>%s</jenkins-test-harness.version>
20822094
<jenkins.version>%s</jenkins.version>
2095+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
20832096
</properties>
20842097
<dependencies>
20852098
<dependency>
@@ -2277,6 +2290,7 @@ void upgradeNextMajorParentVersionTestWithBom() {
22772290
<maven.compiler.source>17</maven.compiler.source>
22782291
<maven.compiler.release>17</maven.compiler.release>
22792292
<maven.compiler.target>17</maven.compiler.target>
2293+
22802294
</properties>
22812295
<dependencyManagement>
22822296
<dependencies>
@@ -2497,6 +2511,7 @@ void upgradeNextMajorParentVersionTestWithBaseline() {
24972511
<jenkins-test-harness.version>2.41.1</jenkins-test-harness.version>
24982512
<jenkins.baseline>2.440</jenkins.baseline>
24992513
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
2514+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
25002515
</properties>
25012516
<dependencyManagement>
25022517
<dependencies>
@@ -2549,6 +2564,7 @@ void upgradeNextMajorParentVersionTestWithBaseline() {
25492564
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
25502565
<jenkins.baseline>2.516</jenkins.baseline>
25512566
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
2567+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
25522568
</properties>
25532569
<dependencyManagement>
25542570
<dependencies>
@@ -3367,6 +3383,33 @@ void migrateCommonsLang2ToLang3AndCommonText() {
33673383
spec -> spec.recipeFromResource(
33683384
"/META-INF/rewrite/recipes.yml",
33693385
"io.jenkins.tools.pluginmodernizer.MigrateCommonsLang2ToLang3AndCommonText"),
3386+
// language=xml
3387+
pomXml("""
3388+
<?xml version="1.0" encoding="UTF-8"?>
3389+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3390+
<modelVersion>4.0.0</modelVersion>
3391+
<groupId>io.jenkins.plugins</groupId>
3392+
<artifactId>empty</artifactId>
3393+
<version>1.0.0-SNAPSHOT</version>
3394+
<name>Empty pom</name>
3395+
<properties>
3396+
<jenkins.version>2.492.1</jenkins.version>
3397+
</properties>
3398+
</project>
3399+
""", """
3400+
<?xml version="1.0" encoding="UTF-8"?>
3401+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3402+
<modelVersion>4.0.0</modelVersion>
3403+
<groupId>io.jenkins.plugins</groupId>
3404+
<artifactId>empty</artifactId>
3405+
<version>1.0.0-SNAPSHOT</version>
3406+
<name>Empty pom</name>
3407+
<properties>
3408+
<jenkins.version>2.492.1</jenkins.version>
3409+
<ban-commons-lang-2.skip>false</ban-commons-lang-2.skip>
3410+
</properties>
3411+
</project>
3412+
"""),
33703413
// language=java
33713414
java("""
33723415
package org.apache.commons.lang;
@@ -3640,8 +3683,8 @@ public void myTestMethodChild(JenkinsRule j) {
36403683
<packaging>hpi</packaging>
36413684
<name>Empty Plugin</name>
36423685
<properties>
3643-
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
36443686
<jenkins.version>2.440.3</jenkins.version>
3687+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
36453688
</properties>
36463689
<dependencyManagement>
36473690
<dependencies>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package io.jenkins.tools.pluginmodernizer.core.visitors;
2+
3+
import static org.openrewrite.maven.Assertions.pomXml;
4+
import static org.openrewrite.test.RewriteTest.toRecipe;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.parallel.Execution;
8+
import org.junit.jupiter.api.parallel.ExecutionMode;
9+
import org.openrewrite.ExecutionContext;
10+
import org.openrewrite.maven.MavenIsoVisitor;
11+
import org.openrewrite.test.RewriteTest;
12+
import org.openrewrite.xml.tree.Xml;
13+
14+
/**
15+
* Test for {@link AddLastPropertyVisitor}
16+
*/
17+
@Execution(ExecutionMode.CONCURRENT)
18+
public class AddLastPropertyTest implements RewriteTest {
19+
20+
@Test
21+
void addPropertyAtEnd() {
22+
rewriteRun(
23+
spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() {
24+
@Override
25+
public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) {
26+
doAfterVisit(new AddLastPropertyVisitor("jenkins.baseline", "2.440"));
27+
return super.visitDocument(x, ctx);
28+
}
29+
})),
30+
// language=xml
31+
pomXml("""
32+
<?xml version="1.0" encoding="UTF-8"?>
33+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
34+
<modelVersion>4.0.0</modelVersion>
35+
<groupId>io.jenkins.plugins</groupId>
36+
<artifactId>empty</artifactId>
37+
<version>1.0.0-SNAPSHOT</version>
38+
<packaging>hpi</packaging>
39+
<name>Empty Plugin</name>
40+
<properties>
41+
<!-- Existing properties -->
42+
<jenkins.version>2.440</jenkins.version>
43+
</properties>
44+
</project>
45+
""", """
46+
<?xml version="1.0" encoding="UTF-8"?>
47+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
48+
<modelVersion>4.0.0</modelVersion>
49+
<groupId>io.jenkins.plugins</groupId>
50+
<artifactId>empty</artifactId>
51+
<version>1.0.0-SNAPSHOT</version>
52+
<packaging>hpi</packaging>
53+
<name>Empty Plugin</name>
54+
<properties>
55+
<!-- Existing properties -->
56+
<jenkins.version>2.440</jenkins.version>
57+
<jenkins.baseline>2.440</jenkins.baseline>
58+
</properties>
59+
</project>
60+
"""));
61+
}
62+
63+
@Test
64+
void shouldIgnoreDuplicateTags() {
65+
rewriteRun(
66+
spec -> spec.recipe(toRecipe(() -> new MavenIsoVisitor<>() {
67+
@Override
68+
public Xml.Document visitDocument(Xml.Document x, ExecutionContext ctx) {
69+
doAfterVisit(new AddLastPropertyVisitor("jenkins.baseline", "2.440"));
70+
return super.visitDocument(x, ctx);
71+
}
72+
})),
73+
// language=xml
74+
pomXml("""
75+
<?xml version="1.0" encoding="UTF-8"?>
76+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
77+
<modelVersion>4.0.0</modelVersion>
78+
<groupId>io.jenkins.plugins</groupId>
79+
<artifactId>empty</artifactId>
80+
<version>1.0.0-SNAPSHOT</version>
81+
<packaging>hpi</packaging>
82+
<name>Empty Plugin</name>
83+
<properties>
84+
<!-- Existing properties -->
85+
<jenkins.version>2.440</jenkins.version>
86+
<jenkins.baseline>2.440</jenkins.baseline>
87+
</properties>
88+
</project>
89+
"""));
90+
}
91+
}

0 commit comments

Comments
 (0)