Skip to content

Commit 08f07f1

Browse files
author
Mushtaq Ahmed
committed
Add New recipe and condition
1 parent ea888d2 commit 08f07f1

File tree

22 files changed

+512
-178
lines changed

22 files changed

+512
-178
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/build/api/BuildFile.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,6 @@ public interface BuildFile extends ProjectResource {
170170
Map<String, Object> getPluginConfiguration(String groupId, String artifactId);
171171

172172
void changeMavenPluginConfiguration(String groupId, String artifactId, Map<String,Object> configurationMap);
173+
174+
void removePropertyAndReplaceAllOccurrences(String propertyKey, String newValue);
173175
}

components/sbm-core/src/main/java/org/springframework/sbm/build/impl/OpenRewriteMavenBuildFile.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
import org.openrewrite.maven.tree.ResolvedDependency;
3434
import org.openrewrite.maven.tree.ResolvedManagedDependency;
3535
import org.openrewrite.maven.tree.Scope;
36+
import org.openrewrite.xml.ChangeTagValueVisitor;
3637
import org.openrewrite.xml.internal.XmlPrinter;
3738
import org.openrewrite.xml.tree.Xml;
39+
import org.openrewrite.xml.tree.Xml.Tag;
40+
3841
import org.springframework.context.ApplicationEventPublisher;
3942
import org.springframework.sbm.build.api.BuildFile;
4043
import org.springframework.sbm.build.api.DependenciesChangedEvent;
@@ -75,7 +78,7 @@
7578
public class OpenRewriteMavenBuildFile extends RewriteSourceFileHolder<Xml.Document> implements BuildFile {
7679

7780
private final ApplicationEventPublisher eventPublisher;
78-
private PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();
81+
private final PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();
7982

8083
// TODO: #7 clarify if RefreshPomModel is still required?
8184
// Execute separately since RefreshPomModel caches the refreshed maven files after the first visit
@@ -110,7 +113,7 @@ protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx)
110113
for (int i = 0; i < newMavenFiles.size(); i++) {
111114
Optional<MavenResolutionResult> mavenModels = MavenBuildFileUtil.findMavenResolution(mavenFiles.get(i));
112115
Optional<MavenResolutionResult> newMavenModels = MavenBuildFileUtil.findMavenResolution(newMavenFiles.get(i));
113-
mavenFiles.get(i).withMarkers(Markers.build(Arrays.asList(newMavenModels.get())));
116+
mavenFiles.get(i).withMarkers(Markers.build(List.of(newMavenModels.get())));
114117
// FIXME: 497 verify correctness
115118
mavenFiles.set(i, newMavenFiles.get(i));
116119
}
@@ -549,17 +552,17 @@ public void addPlugin(Plugin plugin) {
549552

550553
@Override
551554
public List<Path> getSourceFolders() {
552-
return Arrays.asList(getAbsolutePath().getParent().resolve(JAVA_SOURCE_FOLDER));
555+
return List.of(getAbsolutePath().getParent().resolve(JAVA_SOURCE_FOLDER));
553556
}
554557

555558
@Override
556559
public List<Path> getResourceFolders() {
557-
return Arrays.asList(getAbsolutePath().getParent().resolve(RESOURCE_FOLDER));
560+
return List.of(getAbsolutePath().getParent().resolve(RESOURCE_FOLDER));
558561
}
559562

560563
@Override
561564
public List<Path> getTestResourceFolders() {
562-
return Arrays.asList(getAbsolutePath().getParent().resolve(RESOURCE_TEST_FOLDER));
565+
return List.of(getAbsolutePath().getParent().resolve(RESOURCE_TEST_FOLDER));
563566
}
564567

565568
@Override
@@ -572,11 +575,12 @@ public List<Path> getClasspath() {
572575

573576
@Override
574577
public List<Path> getTestSourceFolders() {
575-
return Arrays.asList(getAbsolutePath().getParent().resolve(JAVA_TEST_SOURCE_FOLDER));
578+
return List.of(getAbsolutePath().getParent().resolve(JAVA_TEST_SOURCE_FOLDER));
576579
}
577580

578581
final public String getProperty(String key) {
579-
return getPom().getPom().getProperties().get(key);
582+
//return getPom().getPom().getProperties().get(key);
583+
return getPom().getPom().getRequested().getProperties().get(key);
580584
}
581585

582586
final public void setProperty(String key, String value) {
@@ -850,4 +854,36 @@ public void changeMavenPluginConfiguration(String groupId, String artifactId,
850854

851855
}
852856

857+
@Override
858+
public void removePropertyAndReplaceAllOccurrences(String propertyKey, String newValue){
859+
MavenIsoVisitor mavenVisitor = new MavenIsoVisitor<ExecutionContext>() {
860+
@Override
861+
public Xml.Document visitDocument(Xml.Document maven, ExecutionContext ctx) {
862+
new MavenIsoVisitor<ExecutionContext>() {
863+
864+
String propertyName = propertyKey.startsWith("${") ?
865+
propertyKey.replace("${", "").replace("}", "") : propertyKey;
866+
867+
@Override
868+
public Tag visitTag(Tag tag, ExecutionContext context) {
869+
if (isPropertyTag() && propertyName.equals(tag.getName())) {
870+
apply(new RemoveProperty(propertyName));
871+
}
872+
Optional<String> value = tag.getValue();
873+
if (tag.getContent() != null && value.isPresent() && value.get().contains("${")) {
874+
if (propertyKey.equals(value.get())) {
875+
apply(new GenericOpenRewriteRecipe<>(()-> new ChangeTagValueVisitor<>(tag, newValue)));
876+
877+
}
878+
}
879+
return super.visitTag(tag, context);
880+
}
881+
}.visit(maven, executionContext);
882+
883+
return super.visitDocument(maven, executionContext);
884+
}
885+
};
886+
mavenVisitor.visitDocument(getSourceFile(), executionContext);
887+
888+
}
853889
}

components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@ void deserializePluginConfiguration() {
17651765
" <artifactId>maven-compiler-plugin</artifactId>\n" +
17661766
" <configuration>\n" +
17671767
" <source>${source}</source>\n" +
1768+
" <release>${source}</release>\n" +
17681769
" <target>17</target>\n" +
17691770
" <fork>false</fork>\n" +
17701771
" <showWarnings>true</showWarnings>\n" +
@@ -1859,6 +1860,88 @@ void serializePluginConfiguration() {
18591860

18601861
}
18611862

1863+
@Test
1864+
void removeAndReplaceAllOccurrences() {
1865+
1866+
String pomXml =
1867+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
1868+
"<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 http://maven.apache.org/maven-v4_0_0.xsd\">\n" +
1869+
"\n" +
1870+
" <modelVersion>4.0.0</modelVersion>\n" +
1871+
" <groupId>org.springframework.boot</groupId>\n" +
1872+
" <artifactId>spring-boot-starter-parent</artifactId>\n" +
1873+
" <version>2.7.3</version>\n" +
1874+
" <packaging>jar</packaging>\n" +
1875+
" <name>hello-world</name>\n" +
1876+
" <properties>\n" +
1877+
" <java.version>17</java.version>\n" +
1878+
" <source>17</source>\n" +
1879+
" <target>17</target>\n" +
1880+
" </properties>\n" +
1881+
" <build>\n" +
1882+
" <plugins>\n" +
1883+
" <plugin>\n" +
1884+
" <groupId>org.apache.maven.plugins</groupId>\n" +
1885+
" <artifactId>maven-compiler-plugin</artifactId>\n" +
1886+
" <configuration>\n" +
1887+
" <source>${source}</source>\n" +
1888+
" <release>${target}</release>\n" +
1889+
" <target>${target}</target>\n" +
1890+
" <fork>false</fork>\n" +
1891+
" <showWarnings>true</showWarnings>\n" +
1892+
" <showDeprecation>true</showDeprecation>\n"+
1893+
" <compilerArgs>\n" +
1894+
" <compilerArg>-J-Duser.language=en_us</compilerArg>\n" +
1895+
" </compilerArgs>\n" +
1896+
" </configuration>\n" +
1897+
" </plugin>\n" +
1898+
" </plugins>\n" +
1899+
" </build>\n" +
1900+
"</project>";
1901+
1902+
String expected =
1903+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
1904+
"<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 http://maven.apache.org/maven-v4_0_0.xsd\">\n" +
1905+
"\n" +
1906+
" <modelVersion>4.0.0</modelVersion>\n" +
1907+
" <groupId>org.springframework.boot</groupId>\n" +
1908+
" <artifactId>spring-boot-starter-parent</artifactId>\n" +
1909+
" <version>2.7.3</version>\n" +
1910+
" <packaging>jar</packaging>\n" +
1911+
" <name>hello-world</name>\n" +
1912+
" <properties>\n" +
1913+
" <java.version>17</java.version>\n" +
1914+
" </properties>\n" +
1915+
" <build>\n" +
1916+
" <plugins>\n" +
1917+
" <plugin>\n" +
1918+
" <groupId>org.apache.maven.plugins</groupId>\n" +
1919+
" <artifactId>maven-compiler-plugin</artifactId>\n" +
1920+
" <configuration>\n" +
1921+
" <source>${java.version}</source>\n" +
1922+
" <release>${java.version}</release>\n" +
1923+
" <target>${java.version}</target>\n" +
1924+
" <fork>false</fork>\n" +
1925+
" <showWarnings>true</showWarnings>\n" +
1926+
" <showDeprecation>true</showDeprecation>\n"+
1927+
" <compilerArgs>\n" +
1928+
" <compilerArg>-J-Duser.language=en_us</compilerArg>\n" +
1929+
" </compilerArgs>\n" +
1930+
" </configuration>\n" +
1931+
" </plugin>\n" +
1932+
" </plugins>\n" +
1933+
" </build>\n" +
1934+
"</project>";
1935+
1936+
BuildFile openRewriteMavenBuildFile = TestProjectContext.buildProjectContext().withMavenRootBuildFileSource(pomXml).build().getBuildFile();
1937+
openRewriteMavenBuildFile
1938+
.removePropertyAndReplaceAllOccurrences("${source}", "${java.version}");
1939+
openRewriteMavenBuildFile
1940+
.removePropertyAndReplaceAllOccurrences("${target}", "${java.version}");
1941+
1942+
assertThat(openRewriteMavenBuildFile.print()).isEqualTo(expected);
1943+
}
1944+
18621945
@Test
18631946
void removePluginsMatchingRegex() {
18641947
String pomXml =

components/sbm-support-boot/src/main/java/org/springframework/sbm/boot/cleanup/RemovePropertyAndAllOccurrence.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.springframework.sbm.boot.cleanup;
1+
package org.springframework.sbm.boot.cleanup.actions;
22

33
import java.util.Map;
44

@@ -14,19 +14,20 @@ public class RemoveRedundantMavenCompilerPlugin extends AbstractAction {
1414
@Override
1515
public void apply(ProjectContext context) {
1616

17-
Map<String, Object> configurationMap = context.getBuildFile().getPluginConfiguration(GROUP_ID, ARTIFACT_ID);
17+
Map<String, Object> configurationMap = context.getBuildFile()
18+
.getPluginConfiguration(GROUP_ID, ARTIFACT_ID);
1819

1920
if (configurationMap == null) {
2021
return;
2122
}
2223

2324
long otherConfiguration = configurationMap.entrySet().stream()
24-
.filter(config -> !config.getKey().equals("source")).filter(config -> !config.getKey().equals("target"))
25+
.filter(config -> !config.getKey().equals("source"))
26+
.filter(config -> !config.getKey().equals("target"))
2527
.count();
2628

2729
if (otherConfiguration == 0) {
2830
context.getBuildFile().removePlugins(GROUP_ID + ":" + ARTIFACT_ID);
29-
3031
}
3132

3233
}
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.springframework.sbm.boot.cleanup;
1+
package org.springframework.sbm.boot.cleanup.actions;
22

33
import java.util.Map;
44

@@ -23,7 +23,8 @@ public class RemoveRedundantMavenCompilerPluginProperties extends AbstractAction
2323
@Override
2424
public void apply(ProjectContext context) {
2525

26-
Map<String, Object> configurationMap = context.getBuildFile().getPluginConfiguration(GROUP_ID, ARTIFACT_ID);
26+
Map<String, Object> configurationMap = context.getBuildFile()
27+
.getPluginConfiguration(GROUP_ID, ARTIFACT_ID);
2728

2829
if (configurationMap == null) {
2930
return;
@@ -33,22 +34,29 @@ public void apply(ProjectContext context) {
3334
String targetValue = (String) configurationMap.get("target");
3435

3536
if (sourceValue != null && targetValue != null) {
36-
String sourceLookupValue = sourceValue, targetLookupValue = targetValue;
37-
if (sourceValue.startsWith("${")) {
38-
String sourceProperty = sourceValue.replace("${", "").replace("}", "");
39-
sourceLookupValue = context.getBuildFile().getProperty(sourceProperty);
40-
context.getBuildFile().setProperty(sourceProperty, null);
41-
}
42-
if (targetValue.startsWith("${")) {
43-
String targetProperty = targetValue.replace("${", "").replace("}", "");
44-
targetLookupValue = context.getBuildFile().getProperty(targetProperty);
45-
context.getBuildFile().setProperty(targetProperty, null);
46-
}
37+
38+
String sourceLookupValue = sourceValue.startsWith("${") ?
39+
context.getBuildFile().getProperty(
40+
sourceValue.replace("${", "").replace("}", "")
41+
) : sourceValue;
42+
43+
String targetLookupValue = targetValue.startsWith("${") ?
44+
context.getBuildFile().getProperty(
45+
targetValue.replace("${", "").replace("}", "")
46+
) : targetValue;
4747

4848
if (sourceLookupValue != null && sourceLookupValue.equals(targetLookupValue)) {
4949
context.getBuildFile().setProperty(JAVA_VERSION, sourceLookupValue);
5050
configurationMap.put("source", MAVEN_COMPILER_SOURCE);
5151
configurationMap.put("target", MAVEN_COMPILER_TARGET);
52+
if(sourceValue.startsWith("${")){
53+
context.getBuildFile()
54+
.removePropertyAndReplaceAllOccurrences(sourceValue, JAVA_VERSION);
55+
}
56+
if (targetValue.startsWith("${")){
57+
context.getBuildFile()
58+
.removePropertyAndReplaceAllOccurrences(targetValue, JAVA_VERSION);
59+
}
5260
context.getBuildFile().changeMavenPluginConfiguration(GROUP_ID, ARTIFACT_ID, configurationMap);
5361
}
5462
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.springframework.sbm.boot.cleanup.conditions;
2+
3+
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
4+
import org.springframework.sbm.build.api.Plugin;
5+
import org.springframework.sbm.build.migration.conditions.MavenPluginDoesNotExist;
6+
import org.springframework.sbm.common.migration.conditions.FileMatchingPatternExist;
7+
import org.springframework.sbm.engine.context.ProjectContext;
8+
import org.springframework.sbm.engine.recipe.Condition;
9+
10+
public class SpringBootMavenCompilerExists implements Condition {
11+
12+
@Override
13+
public String getDescription() {
14+
return "Checks if it's a spring boot project pom has a maven compiler plugin exists";
15+
}
16+
17+
@Override
18+
public boolean evaluate(ProjectContext context) {
19+
20+
FileMatchingPatternExist fileMatchingPatternExist = new FileMatchingPatternExist();
21+
fileMatchingPatternExist.setPattern("/**/pom.xml");
22+
23+
IsSpringBootProject isSpringBootProject = new IsSpringBootProject();
24+
isSpringBootProject.setVersionPattern(".*");
25+
26+
MavenPluginDoesNotExist mavenPluginDoesNotExist = new MavenPluginDoesNotExist();
27+
Plugin plugin = Plugin.builder().groupId("org.apache.maven.plugins").artifactId("maven-compiler-plugin")
28+
.build();
29+
mavenPluginDoesNotExist.setPlugin(plugin);
30+
31+
return fileMatchingPatternExist.evaluate(context) && !mavenPluginDoesNotExist.evaluate(context)
32+
&& isSpringBootProject.evaluate(context);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)