Skip to content

Commit 722e646

Browse files
author
Mushtaq Ahmed
committed
WIP - Initial changes
1 parent bc08523 commit 722e646

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/build/migration/actions/SetProperty.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package org.springframework.sbm.build.migration.actions;
1717

1818
import lombok.Setter;
19+
import lombok.experimental.SuperBuilder;
20+
1921
import org.springframework.sbm.engine.context.ProjectContext;
2022
import org.springframework.sbm.engine.recipe.AbstractAction;
2123

2224
@Setter
25+
@SuperBuilder
2326
public class SetProperty extends AbstractAction {
2427

2528
private String propertyName;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.springframework.sbm.boot;
2+
3+
import java.util.Optional;
4+
5+
import org.openrewrite.ExecutionContext;
6+
import org.openrewrite.maven.AddProperty;
7+
import org.openrewrite.maven.ChangePropertyValue;
8+
import org.openrewrite.maven.MavenVisitor;
9+
import org.openrewrite.maven.RemoveProperty;
10+
import org.openrewrite.xml.AddToTagVisitor;
11+
import org.openrewrite.xml.ChangeTagValueVisitor;
12+
import org.openrewrite.xml.XPathMatcher;
13+
import org.openrewrite.xml.format.AutoFormat;
14+
import org.openrewrite.xml.tree.Xml;
15+
16+
import static org.openrewrite.xml.AddToTagVisitor.addToTag;
17+
18+
public class ChangeMavenCompilerPluginConfiguration extends MavenVisitor<ExecutionContext> {
19+
20+
private static final String GROUP_ID = "org.apache.maven.plugins";
21+
22+
private static final String ARTIFACT_ID = "maven-compiler-plugin";
23+
24+
private static final XPathMatcher PLUGINS_MATCHER = new XPathMatcher("/project/build/plugins");
25+
26+
private static final String MAVEN_COMPILER_SOURCE = "${maven.compiler.source}";
27+
28+
private static final String MAVEN_COMPILER_TARGET = "${maven.compiler.target}";
29+
30+
private static final String JAVA_VERSION = "java.version";
31+
32+
@Override
33+
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
34+
Xml.Tag plugins = (Xml.Tag) super.visitTag(tag, ctx);
35+
if (!PLUGINS_MATCHER.matches(getCursor())) {
36+
return plugins;
37+
}
38+
Optional<Xml.Tag> maybePlugin = plugins.getChildren().stream()
39+
.filter(plugin -> "plugin".equals(plugin.getName())
40+
&& GROUP_ID.equals(plugin.getChildValue("groupId").orElse(null))
41+
&& ARTIFACT_ID.equals(plugin.getChildValue("artifactId").orElse(null)))
42+
.findAny();
43+
if (maybePlugin.isEmpty()) {
44+
return plugins;
45+
}
46+
47+
Xml.Tag plugin = maybePlugin.get();
48+
Optional<Xml.Tag> maybeConfiguration = maybePlugin.get().getChild("configuration");
49+
if (maybeConfiguration.isPresent()) {
50+
Xml.Tag configuration = maybeConfiguration.get();
51+
52+
String sourceValue = configuration.getChildValue("source").orElse(null);
53+
String targetValue = configuration.getChildValue("target").orElse(null);
54+
55+
if (sourceValue != null && targetValue != null) {
56+
String sourceLookupValue = sourceValue.startsWith("${")
57+
? super.getResolutionResult().getPom().getValue(sourceValue.trim()) : sourceValue;
58+
59+
String targetLookupValue = targetValue.startsWith("${")
60+
? super.getResolutionResult().getPom().getValue(targetValue.trim()) : targetValue;
61+
62+
// If source and target version are same
63+
if (sourceLookupValue != null && sourceLookupValue.equals(targetLookupValue)){
64+
doAfterVisit(new ChangePropertyValue(JAVA_VERSION, sourceLookupValue, true));
65+
doAfterVisit(new ChangeTagValueVisitor<>(configuration.getChild("source").get(), MAVEN_COMPILER_SOURCE));
66+
doAfterVisit(new ChangeTagValueVisitor<>(configuration.getChild("target").get(), MAVEN_COMPILER_TARGET));
67+
68+
if (sourceValue.startsWith("${")) {
69+
doAfterVisit(new RemoveProperty(sourceValue.substring(2, sourceValue.length() - 1)));
70+
}
71+
if (targetValue.startsWith("${")){
72+
doAfterVisit(new RemoveProperty(targetValue.substring(2, targetValue.length() - 1)));
73+
}
74+
}
75+
}
76+
77+
if (sourceValue == null ){
78+
addToTag(configuration,
79+
Xml.Tag.build("<source>" + MAVEN_COMPILER_SOURCE + "</source>\n")
80+
.withPrefix("\n"),
81+
getCursor());
82+
}
83+
84+
if (targetValue == null){
85+
addToTag(configuration,
86+
Xml.Tag.build("<target>" + MAVEN_COMPILER_TARGET + "</target>\n")
87+
.withPrefix("\n"),
88+
getCursor());
89+
}
90+
}
91+
else {
92+
Xml.Tag configurations = Xml.Tag.build("<configuration>\n<source>" + MAVEN_COMPILER_SOURCE
93+
+ "</source>\n<target>" + MAVEN_COMPILER_TARGET + "</target>\n</configuration>\n");
94+
doAfterVisit(new AddToTagVisitor<>(plugin, configurations));
95+
}
96+
return plugins;
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package org.springframework.sbm.boot;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import org.springframework.sbm.openrewrite.MavenRefactoringTestHelper;
6+
7+
class ChangeMavenPluginConfigurationTest {
8+
9+
@Test
10+
void happyPath() {
11+
String given = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
12+
+ " <project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n"
13+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
14+
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
15+
+ " <modelVersion>4.0.0</modelVersion>\n"
16+
+ " <parent>\n"
17+
+ " <groupId>org.springframework.boot</groupId>\n"
18+
+ " <artifactId>spring-boot-starter-parent</artifactId>\n"
19+
+ " <version>2.7.3</version>\n"
20+
+ " </parent>\n"
21+
+ " <artifactId>module1</artifactId>\n"
22+
+ " <properties>\n"
23+
+ " <test1>17</test1>\n"
24+
+ " <test2>17</test2>\n"
25+
+ " </properties>\n"
26+
+ " <build>\n"
27+
+ " <plugins>\n"
28+
+ " <plugin>\n"
29+
+ " <groupId>org.apache.maven.plugins</groupId>\n"
30+
+ " <artifactId>maven-compiler-plugin</artifactId>\n"
31+
+ " <configuration>\n"
32+
+ " <source>${test1}</source>\n"
33+
+ " <target>${test2}</target>\n"
34+
+ " </configuration>\n"
35+
+ " </plugin>\n"
36+
+ " </plugins>\n"
37+
+ " </build>\n"
38+
+ " </project>";
39+
40+
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
41+
+ " <project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n"
42+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
43+
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
44+
+ " <modelVersion>4.0.0</modelVersion>\n"
45+
+ " <parent>\n"
46+
+ " <groupId>org.springframework.boot</groupId>\n"
47+
+ " <artifactId>spring-boot-starter-parent</artifactId>\n"
48+
+ " <version>2.7.3</version>\n"
49+
+ " </parent>\n"
50+
+ " <artifactId>module1</artifactId>\n"
51+
+ " <properties>\n"
52+
+ " <java.version>17</java.version>\n"
53+
+ " </properties>\n"
54+
+ " <build>\n"
55+
+ " <plugins>\n"
56+
+ " <plugin>\n"
57+
+ " <groupId>org.apache.maven.plugins</groupId>\n"
58+
+ " <artifactId>maven-compiler-plugin</artifactId>\n"
59+
+ " <configuration>\n"
60+
+ " <source>${maven.compiler.source}</source>\n"
61+
+ " <target>${maven.compiler.target}</target>\n"
62+
+ " </configuration>\n"
63+
+ " </plugin>\n"
64+
+ " </plugins>\n"
65+
+ " </build>\n"
66+
+ " </project>";
67+
68+
ChangeMavenCompilerPluginConfiguration sut = new ChangeMavenCompilerPluginConfiguration();
69+
MavenRefactoringTestHelper.verifyChange(given, expected, sut);
70+
}
71+
72+
@Test
73+
void emptyMavenCompilerConfiguration() {
74+
String given = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
75+
+ " <project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n"
76+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
77+
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
78+
+ " <modelVersion>4.0.0</modelVersion>\n"
79+
+ " <parent>\n"
80+
+ " <groupId>org.springframework.boot</groupId>\n"
81+
+ " <artifactId>spring-boot-starter-parent</artifactId>\n"
82+
+ " <version>2.7.3</version>\n"
83+
+ " </parent>\n"
84+
+ " <artifactId>module1</artifactId>\n"
85+
+ " <properties>\n"
86+
+ " </properties>\n"
87+
+ " <build>\n"
88+
+ " <plugins>\n"
89+
+ " <plugin>\n"
90+
+ " <groupId>org.apache.maven.plugins</groupId>\n"
91+
+ " <artifactId>maven-compiler-plugin</artifactId>\n"
92+
+ " </plugin>\n"
93+
+ " </plugins>\n"
94+
+ " </build>\n"
95+
+ " </project>";
96+
97+
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
98+
+ " <project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n"
99+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
100+
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
101+
+ " <modelVersion>4.0.0</modelVersion>\n"
102+
+ " <parent>\n"
103+
+ " <groupId>org.springframework.boot</groupId>\n"
104+
+ " <artifactId>spring-boot-starter-parent</artifactId>\n"
105+
+ " <version>2.7.3</version>\n"
106+
+ " </parent>\n"
107+
+ " <artifactId>module1</artifactId>\n"
108+
+ " <properties>\n"
109+
+ " </properties>\n"
110+
+ " <build>\n"
111+
+ " <plugins>\n"
112+
+ " <plugin>\n"
113+
+ " <groupId>org.apache.maven.plugins</groupId>\n"
114+
+ " <artifactId>maven-compiler-plugin</artifactId>\n"
115+
+ " <configuration>\n"
116+
+ " <source>${maven.compiler.source}</source>\n"
117+
+ " <target>${maven.compiler.target}</target>\n"
118+
+ " </configuration>\n"
119+
+ " </plugin>\n"
120+
+ " </plugins>\n"
121+
+ " </build>\n"
122+
+ " </project>";
123+
124+
ChangeMavenCompilerPluginConfiguration sut = new ChangeMavenCompilerPluginConfiguration();
125+
MavenRefactoringTestHelper.verifyChange(given, expected, sut);
126+
}
127+
128+
}

0 commit comments

Comments
 (0)