Skip to content

Commit ea888d2

Browse files
author
Mushtaq Ahmed
committed
Convert to actions and cleanup
1 parent 50df561 commit ea888d2

File tree

14 files changed

+968
-299
lines changed

14 files changed

+968
-299
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.nio.file.Path;
2222
import java.util.List;
23+
import java.util.Map;
2324
import java.util.Optional;
2425
import java.util.Set;
2526

@@ -165,4 +166,8 @@ public interface BuildFile extends ProjectResource {
165166
List<RepositoryDefinition> getPluginRepositories();
166167

167168
List<String> getDeclaredModules();
169+
170+
Map<String, Object> getPluginConfiguration(String groupId, String artifactId);
171+
172+
void changeMavenPluginConfiguration(String groupId, String artifactId, Map<String,Object> configurationMap);
168173
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
*/
1616
package org.springframework.sbm.build.api;
1717

18-
import lombok.*;
18+
import java.util.HashMap;
19+
import java.util.List;
1920

2021
import javax.validation.constraints.NotNull;
2122
import javax.validation.constraints.Null;
22-
import java.util.List;
23+
24+
import lombok.AllArgsConstructor;
25+
import lombok.Builder;
26+
import lombok.Getter;
27+
import lombok.NoArgsConstructor;
28+
import lombok.Setter;
29+
import lombok.Singular;
2330

2431
@Getter
2532
@Setter
@@ -39,7 +46,7 @@ public class Plugin {
3946
@Singular("execution")
4047
private List<Execution> executions;
4148

42-
private String configuration;
49+
HashMap<String, Object> configuration;
4350

4451
private String dependencies;
4552

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package org.springframework.sbm.build.impl;
1717

18+
import com.fasterxml.jackson.core.JsonProcessingException;
19+
import com.fasterxml.jackson.core.type.TypeReference;
1820
import lombok.extern.slf4j.Slf4j;
21+
import org.intellij.lang.annotations.Language;
1922
import org.openrewrite.ExecutionContext;
2023
import org.openrewrite.Parser;
2124
import org.openrewrite.Recipe;
@@ -24,11 +27,13 @@
2427
import org.openrewrite.internal.lang.Nullable;
2528
import org.openrewrite.marker.Markers;
2629
import org.openrewrite.maven.*;
30+
import org.openrewrite.maven.internal.MavenXmlMapper;
2731
import org.openrewrite.maven.tree.MavenResolutionResult;
2832
import org.openrewrite.maven.tree.Parent;
2933
import org.openrewrite.maven.tree.ResolvedDependency;
3034
import org.openrewrite.maven.tree.ResolvedManagedDependency;
3135
import org.openrewrite.maven.tree.Scope;
36+
import org.openrewrite.xml.internal.XmlPrinter;
3237
import org.openrewrite.xml.tree.Xml;
3338
import org.springframework.context.ApplicationEventPublisher;
3439
import org.springframework.sbm.build.api.BuildFile;
@@ -42,7 +47,6 @@
4247
import org.springframework.sbm.build.migration.recipe.AddMavenPlugin;
4348
import org.springframework.sbm.build.migration.recipe.RemoveMavenPlugin;
4449
import org.springframework.sbm.build.migration.visitor.AddOrUpdateDependencyManagement;
45-
import org.springframework.sbm.build.migration.visitor.AddProperty;
4650
import org.springframework.sbm.java.impl.ClasspathRegistry;
4751
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
4852
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
@@ -55,6 +59,7 @@
5559
import java.util.ArrayList;
5660
import java.util.Arrays;
5761
import java.util.Collections;
62+
import java.util.HashMap;
5863
import java.util.HashSet;
5964
import java.util.Iterator;
6065
import java.util.List;
@@ -579,7 +584,8 @@ final public void setProperty(String key, String value) {
579584
apply(new RemoveProperty(key));
580585
} else {
581586
String current = getProperty(key);
582-
apply(current == null ? new AddProperty(key, value) : new ChangePropertyValue(key, value, false));
587+
// FIXME: AddProperty does not work, instead used ChangePropertyValue with the missingFlag as true
588+
apply(current == null ? new ChangePropertyValue(key, value, true) : new ChangePropertyValue(key, value, false));
583589
}
584590
apply(new RefreshPomModel());
585591
}
@@ -707,7 +713,7 @@ public List<String> getDeclaredModules() {
707713
.collect(Collectors.toList());
708714
}
709715

710-
@Override
716+
@Override
711717
public List<RepositoryDefinition> getPluginRepositories() {
712718
return pluginRepositoryHandler.getRepositoryDefinitions(getSourceFile());
713719
}
@@ -751,8 +757,19 @@ private Plugin mapToPlugin(Xml.Tag tag) {
751757
if (versionTag.isPresent()) {
752758
version = versionTag.get().getValue().get();
753759
}
754-
Plugin plugin = new Plugin(groupId, artifactId, version, List.of(), "", "");
755-
return plugin;
760+
Optional<Xml.Tag> configurationTag = tag.getChild("configuration");
761+
HashMap<String, Object> configuration = new HashMap<>();
762+
if (configurationTag.isPresent() && configurationTag.get().getChildren().size() > 0){
763+
try {
764+
String configurationXml = configurationTag.get().print(new XmlPrinter<>());
765+
configuration = MavenXmlMapper.readMapper().readValue(configurationXml, new TypeReference<>() {});
766+
}
767+
catch (JsonProcessingException e) {
768+
throw new RuntimeException(e);
769+
}
770+
771+
}
772+
return new Plugin(groupId, artifactId, version, List.of(), configuration, "");
756773
}
757774
};
758775

@@ -796,4 +813,41 @@ public void removePlugins(String... coordinates) {
796813
}
797814
}
798815

816+
@Override
817+
public Map<String, Object> getPluginConfiguration(String groupId, String artifactId){
818+
819+
Optional<Plugin> maybeCompilerPlugin = getPlugins()
820+
.stream()
821+
.filter(plugin -> plugin.getGroupId().equals(groupId) &&
822+
plugin.getArtifactId().equals(artifactId))
823+
.findAny();
824+
825+
if (maybeCompilerPlugin.isEmpty()) {
826+
return null;
827+
}
828+
829+
return maybeCompilerPlugin.get().getConfiguration();
830+
}
831+
832+
@Override
833+
public void changeMavenPluginConfiguration(String groupId, String artifactId,
834+
Map<String, Object> configurationMap) {
835+
if (configurationMap != null && !configurationMap.isEmpty()) {
836+
try {
837+
String configurationXml = MavenXmlMapper.writeMapper().writerWithDefaultPrettyPrinter()
838+
.writeValueAsString(configurationMap);
839+
@Language("xml")
840+
String configurationXmlWithoutRoot = configurationXml.replaceFirst("<HashMap>", "").replace("</HashMap>", "")
841+
.trim();
842+
843+
apply(new ChangePluginConfiguration(groupId, artifactId, configurationXmlWithoutRoot));
844+
845+
}
846+
catch (JsonProcessingException e) {
847+
throw new RuntimeException(e);
848+
}
849+
}
850+
851+
}
852+
799853
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.sbm.build.migration.actions;
1717

18+
import lombok.NoArgsConstructor;
1819
import lombok.Setter;
1920
import lombok.experimental.SuperBuilder;
2021

@@ -23,6 +24,7 @@
2324

2425
@Setter
2526
@SuperBuilder
27+
@NoArgsConstructor
2628
public class SetProperty extends AbstractAction {
2729

2830
private String propertyName;

components/sbm-core/src/main/java/org/springframework/sbm/build/migration/recipe/AddMavenPlugin.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package org.springframework.sbm.build.migration.recipe;
1717

18+
import com.fasterxml.jackson.core.JsonProcessingException;
1819
import lombok.Data;
1920
import lombok.EqualsAndHashCode;
2021
import org.openrewrite.ExecutionContext;
2122
import org.openrewrite.Recipe;
2223
import org.openrewrite.TreeVisitor;
2324
import org.openrewrite.maven.MavenVisitor;
25+
import org.openrewrite.maven.internal.MavenXmlMapper;
2426
import org.openrewrite.xml.AddToTagVisitor;
2527
import org.openrewrite.xml.ChangeTagValueVisitor;
2628
import org.openrewrite.xml.XPathMatcher;
@@ -108,7 +110,7 @@ private String createPluginTagString() {
108110
sb.append("</artifactId>\n");
109111
sb.append(renderVersion());
110112
sb.append(renderExecutions());
111-
sb.append(plugin.getConfiguration() != null ? plugin.getConfiguration().trim() + "\n" : "");
113+
sb.append(renderConfiguration());
112114
sb.append(plugin.getDependencies() != null ? plugin.getDependencies().trim() + "\n" : "");
113115
sb.append("</plugin>\n");
114116
return sb.toString();
@@ -122,6 +124,21 @@ private String renderVersion() {
122124
return plugin.getVersion() != null ? "<version>" + plugin.getVersion() + "</version>\n" : "";
123125
}
124126

127+
private String renderConfiguration(){
128+
if (plugin.getConfiguration() != null) {
129+
try {
130+
String configurationXml = MavenXmlMapper.writeMapper().writerWithDefaultPrettyPrinter()
131+
.writeValueAsString(plugin.getConfiguration());
132+
return configurationXml.replaceFirst("<HashMap>", "").replace("</HashMap>", "")
133+
.trim();
134+
}
135+
catch (JsonProcessingException e) {
136+
throw new RuntimeException(e);
137+
}
138+
}
139+
return "";
140+
}
141+
125142
private String renderExecutions() {
126143
if (plugin.getExecutions() == null || plugin.getExecutions().isEmpty())
127144
return "";
@@ -132,10 +149,10 @@ private String renderExecutions() {
132149

133150
private String renderExecution(Plugin.Execution execution) {
134151
return "<execution>\n" + renderId(execution) + renderGoals(execution) + renderPhase(execution)
135-
+ renderConfiguration(execution) + "</execution>";
152+
+ renderExecutionConfiguration(execution) + "</execution>";
136153
}
137154

138-
private String renderConfiguration(Plugin.Execution execution) {
155+
private String renderExecutionConfiguration(Plugin.Execution execution) {
139156
return execution.getConfiguration() == null ? "" : execution.getConfiguration().trim();
140157
}
141158

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

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.nio.file.Path;
3737
import java.util.List;
38+
import java.util.Map;
3839
import java.util.Objects;
3940
import java.util.Optional;
4041
import java.util.stream.Collectors;
@@ -1731,16 +1732,134 @@ void getPlugins() {
17311732
assertThat(plugins.get(0).getGroupId()).isEqualTo("org.mule.tools.maven");
17321733
assertThat(plugins.get(0).getArtifactId()).isEqualTo("mule-maven-plugin");
17331734
assertThat(plugins.get(0).getVersion()).isEqualTo("${mule.maven.plugin.version}");
1734-
assertThat(plugins.get(0).getConfiguration()).isEmpty();
1735+
assertThat(plugins.get(0).getConfiguration()).isNotEmpty();
17351736

1736-
assertThat(plugins.get(1).getGroupId()).isEqualTo("com.mulesoft.munit.tools");
1737+
assertThat(plugins.get(1).getGroupId()).isEqualTo("com.mulesoft.munit.tools");
17371738
assertThat(plugins.get(1).getArtifactId()).isEqualTo("munit-maven-plugin");
17381739
assertThat(plugins.get(1).getVersion()).isEqualTo("${munit.version}");
1739-
assertThat(plugins.get(1).getConfiguration()).isEmpty();
1740+
assertThat(plugins.get(1).getConfiguration()).isNotEmpty();
17401741
assertThat(plugins.get(1).getExecutions()).isEmpty();
17411742
}
17421743

1743-
@Test
1744+
@Test
1745+
void deserializePluginConfiguration() {
1746+
1747+
String pomXml =
1748+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
1749+
"<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" +
1750+
"\n" +
1751+
" <modelVersion>4.0.0</modelVersion>\n" +
1752+
" <groupId>org.springframework.boot</groupId>\n" +
1753+
" <artifactId>spring-boot-starter-parent</artifactId>\n" +
1754+
" <version>2.7.3</version>\n" +
1755+
" <packaging>jar</packaging>\n" +
1756+
" <name>hello-world</name>" +
1757+
" <properties>\n" +
1758+
" <source>17</source>\n" +
1759+
" <target>17</target>\n" +
1760+
" </properties>\n" +
1761+
" <build>\n" +
1762+
" <plugins>\n" +
1763+
" <plugin>\n" +
1764+
" <groupId>org.apache.maven.plugins</groupId>\n" +
1765+
" <artifactId>maven-compiler-plugin</artifactId>\n" +
1766+
" <configuration>\n" +
1767+
" <source>${source}</source>\n" +
1768+
" <target>17</target>\n" +
1769+
" <fork>false</fork>\n" +
1770+
" <showWarnings>true</showWarnings>\n" +
1771+
" <showDeprecation>true</showDeprecation>\n"+
1772+
" <compilerArgs>\n" +
1773+
" <compilerArg>-J-Duser.language=en_us</compilerArg>\n" +
1774+
" </compilerArgs>\n" +
1775+
" </configuration>\n" +
1776+
" </plugin>\n" +
1777+
" </plugins>\n" +
1778+
" </build>\n" +
1779+
"</project>";
1780+
1781+
BuildFile openRewriteMavenBuildFile = TestProjectContext.buildProjectContext().withMavenRootBuildFileSource(pomXml).build().getBuildFile();
1782+
1783+
Plugin compilerPlugin = openRewriteMavenBuildFile.getPlugins()
1784+
.stream()
1785+
.filter(plugin -> plugin.getGroupId().equals("org.apache.maven.plugins") &&
1786+
plugin.getArtifactId().equals("maven-compiler-plugin"))
1787+
.findAny().orElseThrow();
1788+
1789+
Map<String, Object> configurationMap = compilerPlugin.getConfiguration();
1790+
1791+
assertThat(configurationMap.get("source")).isEqualTo("${source}");
1792+
assertThat(configurationMap.get("target")).isEqualTo("17");
1793+
assertThat(configurationMap.get("fork")).isEqualTo("false");
1794+
}
1795+
1796+
@Test
1797+
void serializePluginConfiguration() {
1798+
1799+
String pomXml =
1800+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
1801+
"<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" +
1802+
"\n" +
1803+
" <modelVersion>4.0.0</modelVersion>\n" +
1804+
" <groupId>org.springframework.boot</groupId>\n" +
1805+
" <artifactId>spring-boot-starter-parent</artifactId>\n" +
1806+
" <version>2.7.3</version>\n" +
1807+
" <packaging>jar</packaging>\n" +
1808+
" <name>hello-world</name>" +
1809+
" <build>\n" +
1810+
" <plugins>\n" +
1811+
" <plugin>\n" +
1812+
" <groupId>org.apache.maven.plugins</groupId>\n" +
1813+
" <artifactId>maven-compiler-plugin</artifactId>\n" +
1814+
" </plugin>\n" +
1815+
" </plugins>\n" +
1816+
" </build>\n" +
1817+
"</project>";
1818+
1819+
String expected =
1820+
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
1821+
"<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" +
1822+
"\n" +
1823+
" <modelVersion>4.0.0</modelVersion>\n" +
1824+
" <groupId>org.springframework.boot</groupId>\n" +
1825+
" <artifactId>spring-boot-starter-parent</artifactId>\n" +
1826+
" <version>2.7.3</version>\n" +
1827+
" <packaging>jar</packaging>\n" +
1828+
" <name>hello-world</name>" +
1829+
" <build>\n" +
1830+
" <plugins>\n" +
1831+
" <plugin>\n" +
1832+
" <groupId>org.apache.maven.plugins</groupId>\n" +
1833+
" <artifactId>maven-compiler-plugin</artifactId>\n" +
1834+
" <configuration>\n" +
1835+
" <source>17</source>\n" +
1836+
" <target>17</target>\n" +
1837+
" </configuration>\n" +
1838+
" </plugin>\n" +
1839+
" </plugins>\n" +
1840+
" </build>\n" +
1841+
"</project>";
1842+
1843+
BuildFile openRewriteMavenBuildFile = TestProjectContext.buildProjectContext().withMavenRootBuildFileSource(pomXml).build().getBuildFile();
1844+
1845+
Plugin compilerPlugin = openRewriteMavenBuildFile.getPlugins()
1846+
.stream()
1847+
.filter(plugin -> plugin.getGroupId().equals("org.apache.maven.plugins") &&
1848+
plugin.getArtifactId().equals("maven-compiler-plugin"))
1849+
.findAny().orElseThrow();
1850+
1851+
Map<String, Object> configurationMap = compilerPlugin.getConfiguration();
1852+
1853+
configurationMap.put("source", 17);
1854+
configurationMap.put("target", 17);
1855+
1856+
openRewriteMavenBuildFile.changeMavenPluginConfiguration("org.apache.maven.plugins", "maven-compiler-plugin", configurationMap);
1857+
1858+
assertThat(openRewriteMavenBuildFile.print()).isEqualTo(expected);
1859+
1860+
}
1861+
1862+
@Test
17441863
void removePluginsMatchingRegex() {
17451864
String pomXml =
17461865
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +

0 commit comments

Comments
 (0)