Skip to content

Commit 8674d6b

Browse files
ahmedmqfabapp2
authored andcommitted
Remove standard maven-compiler plugin for applications with boot parent - gh-428 (#508)
- Add New recipe and condition - Enhance PomBuilder.java - Use MavenBuildFileRefactoring to apply recipe, similar to JavaRefactoring - Refactor getPlugins() method in OpenRewriteMavenBuildFile - Extract Plugin interface and refactor Maven Plugin implementation - Create new interface `Plugin#Configuration` and implementation - Refactor `Plugin#Execution` into `OpenRewriteMavenPlugin#OpenRewriteMavenPluginExecution` - Add deleteProperty() API to BuildFile - setProperty() API no longer removes the maven property if the value supplied is null. - Add maven plugin configuration to PomBuilder - Use Jackson XML pretty print to generate XML string for plugin configuration - Add findPlugins API to BuildFile - Use Plugin interface instead of OpenRewriteMavenPlugin implementation in dealing with objects - Refactor Maven compiler plugin actions - Clean up BuildFile API
1 parent 0fd292a commit 8674d6b

File tree

33 files changed

+2239
-185
lines changed

33 files changed

+2239
-185
lines changed

components/sbm-core/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@
154154
<classifier>tests</classifier>
155155
<scope>test</scope>
156156
</dependency>
157-
</dependencies>
157+
<dependency>
158+
<groupId>com.fasterxml.jackson.dataformat</groupId>
159+
<artifactId>jackson-dataformat-xml</artifactId>
160+
<version>2.14.0</version>
161+
</dependency>
162+
163+
</dependencies>
158164
<build>
159165
<plugins>
160166
<plugin>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
package org.springframework.sbm.build.api;
1717

1818
import org.openrewrite.maven.tree.Scope;
19+
1920
import org.springframework.sbm.project.resource.ProjectResource;
2021

2122
import java.nio.file.Path;
2223
import java.util.List;
24+
import java.util.Map;
2325
import java.util.Optional;
2426
import java.util.Set;
2527

@@ -117,6 +119,8 @@ public interface BuildFile extends ProjectResource {
117119

118120
String getProperty(String key);
119121

122+
void deleteProperty(String key);
123+
120124
String print();
121125

122126
/**
@@ -165,4 +169,6 @@ public interface BuildFile extends ProjectResource {
165169
List<RepositoryDefinition> getPluginRepositories();
166170

167171
List<String> getDeclaredModules();
172+
173+
Optional<Plugin> findPlugin(String groupId, String artifactId);
168174
}

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

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,46 @@
1515
*/
1616
package org.springframework.sbm.build.api;
1717

18-
import lombok.*;
19-
20-
import javax.validation.constraints.NotNull;
21-
import javax.validation.constraints.Null;
2218
import java.util.List;
19+
import java.util.Optional;
20+
import java.util.Set;
21+
22+
public interface Plugin {
23+
24+
String getGroupId();
25+
26+
String getArtifactId();
27+
28+
String getVersion();
29+
30+
String getDependencies();
31+
32+
List<? extends Execution> getExecutions();
33+
34+
Configuration getConfiguration();
35+
36+
interface Configuration {
37+
38+
Optional<String> getDeclaredStringValue(String property);
39+
40+
String getResolvedStringValue(String property);
41+
42+
void setDeclaredStringValue(String property, String value);
43+
44+
Set<String> getPropertyKeys();
45+
46+
}
47+
48+
interface Execution {
49+
50+
String getId();
51+
52+
List<String> getGoals();
53+
54+
String getPhase();
55+
56+
String getConfiguration();
57+
58+
}
2359

24-
@Getter
25-
@Setter
26-
@Builder
27-
@NoArgsConstructor
28-
@AllArgsConstructor
29-
public class Plugin {
30-
31-
@NotNull
32-
private String groupId;
33-
34-
@NotNull
35-
private String artifactId;
36-
37-
private String version;
38-
39-
@Singular("execution")
40-
private List<Execution> executions;
41-
42-
private String configuration;
43-
44-
private String dependencies;
45-
46-
@Builder
47-
@Getter
48-
@NoArgsConstructor
49-
@AllArgsConstructor
50-
@Setter
51-
public static class Execution {
52-
@Null
53-
private String id;
54-
@Singular("goal")
55-
private List<String> goals;
56-
@Null
57-
private String phase;
58-
@Null
59-
private String configuration;
60-
}
61-
}
60+
}

components/sbm-core/src/main/java/org/springframework/sbm/build/impl/Refactoring.java renamed to components/sbm-core/src/main/java/org/springframework/sbm/build/impl/MavenBuildFileRefactoring.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
import org.openrewrite.xml.tree.Xml;
2424
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
2525
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;
26-
import org.springframework.util.ReflectionUtils;
2726

2827
import java.util.Arrays;
2928
import java.util.List;
3029
import java.util.stream.Collectors;
3130

3231
@RequiredArgsConstructor
33-
class Refactoring<P> {
32+
class MavenBuildFileRefactoring<P> {
3433

3534
private final RewriteSourceFileHolder<Xml.Document> pom;
3635

@@ -57,7 +56,9 @@ private List<Result> executeRecipe(Recipe recipe) {
5756

5857
private void processResults(List<Result> results) {
5958
if (!results.isEmpty()) {
60-
results.forEach(c -> processResult(c));
59+
// FIXME: Works only on a single POM and does not apply to all other resources
60+
pom.replaceWith((Xml.Document) results.get(0).getAfter());
61+
// results.forEach(c -> processResult(c));
6162
}
6263
}
6364

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

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

18+
import com.fasterxml.jackson.core.JsonProcessingException;
1819
import lombok.extern.slf4j.Slf4j;
1920
import org.openrewrite.ExecutionContext;
2021
import org.openrewrite.Parser;
@@ -24,12 +25,16 @@
2425
import org.openrewrite.internal.lang.Nullable;
2526
import org.openrewrite.marker.Markers;
2627
import org.openrewrite.maven.*;
28+
import org.openrewrite.maven.internal.MavenXmlMapper;
2729
import org.openrewrite.maven.tree.MavenResolutionResult;
2830
import org.openrewrite.maven.tree.Parent;
2931
import org.openrewrite.maven.tree.ResolvedDependency;
3032
import org.openrewrite.maven.tree.ResolvedManagedDependency;
3133
import org.openrewrite.maven.tree.Scope;
34+
import org.openrewrite.xml.ChangeTagValueVisitor;
3235
import org.openrewrite.xml.tree.Xml;
36+
import org.openrewrite.xml.tree.Xml.Tag;
37+
3338
import org.springframework.context.ApplicationEventPublisher;
3439
import org.springframework.sbm.build.api.BuildFile;
3540
import org.springframework.sbm.build.api.DependenciesChangedEvent;
@@ -70,7 +75,8 @@
7075
public class OpenRewriteMavenBuildFile extends RewriteSourceFileHolder<Xml.Document> implements BuildFile {
7176

7277
private final ApplicationEventPublisher eventPublisher;
73-
private PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();
78+
private final PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();
79+
private final MavenBuildFileRefactoring<Xml.Document> refactoring ;
7480

7581
// TODO: #7 clarify if RefreshPomModel is still required?
7682
// Execute separately since RefreshPomModel caches the refreshed maven files after the first visit
@@ -105,7 +111,7 @@ protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx)
105111
for (int i = 0; i < newMavenFiles.size(); i++) {
106112
Optional<MavenResolutionResult> mavenModels = MavenBuildFileUtil.findMavenResolution(mavenFiles.get(i));
107113
Optional<MavenResolutionResult> newMavenModels = MavenBuildFileUtil.findMavenResolution(newMavenFiles.get(i));
108-
mavenFiles.get(i).withMarkers(Markers.build(Arrays.asList(newMavenModels.get())));
114+
mavenFiles.get(i).withMarkers(Markers.build(List.of(newMavenModels.get())));
109115
// FIXME: 497 verify correctness
110116
mavenFiles.set(i, newMavenFiles.get(i));
111117
}
@@ -143,22 +149,23 @@ public OpenRewriteMavenBuildFile(Path absoluteProjectPath,
143149
super(absoluteProjectPath, sourceFile);
144150
this.eventPublisher = eventPublisher;
145151
this.executionContext = executionContext;
152+
this.refactoring = new MavenBuildFileRefactoring<>(this.getResource());
146153
}
147154

148155
public void apply(Recipe recipe) {
149156
// FIXME: #7 Make ExecutionContext a Spring Bean and caching configurable, also if the project root is used as workdir it must be added to .gitignore
150-
// FIXME: #7 this made it veeery slow
151157
//executionContext.putMessage("org.openrewrite.maven.pomCache", new RocksdbMavenPomCache(this.getAbsoluteProjectDir()));
152-
List<Result> result = recipe.run(List.of(getSourceFile()), executionContext).getResults();
153-
if (!result.isEmpty()) {
154-
replaceWith((Xml.Document) result.get(0).getAfter());
155-
}
158+
refactoring.execute(recipe);
156159
}
157160

158161
public MavenResolutionResult getPom() {
159162
return MavenBuildFileUtil.findMavenResolution(getSourceFile()).get();
160163
}
161164

165+
public RewriteSourceFileHolder<Xml.Document> getResource() {
166+
return this;
167+
}
168+
162169
@Override
163170
public void addDependency(Dependency dependency) {
164171
if (!containsDependency(dependency)) {
@@ -263,7 +270,6 @@ public List<Dependency> getDeclaredDependencies(Scope... scopes) {
263270
@Override
264271
public List<Dependency> getRequestedDependencies() {
265272
List<org.openrewrite.maven.tree.Dependency> requestedDependencies = getPom().getPom().getRequestedDependencies();
266-
267273
// FIXME: #7 use getPom().getDependencies() instead ?
268274
List<Dependency> declaredDependenciesWithEffectiveVersions = requestedDependencies.stream()
269275
.map(d -> mapDependency(d))
@@ -312,6 +318,8 @@ public List<Dependency> getRequestedDependencies() {
312318

313319
/**
314320
* {@inheritDoc}
321+
*
322+
* TODO: #497 Test with declared and transitive dependencies
315323
*/
316324
@Override
317325
public Set<Dependency> getEffectiveDependencies(Scope scope) {
@@ -574,22 +582,22 @@ public boolean hasPlugin(Plugin plugin) {
574582

575583
@Override
576584
public void addPlugin(Plugin plugin) {
577-
apply(new AddMavenPlugin(plugin));
585+
apply(new AddMavenPlugin((OpenRewriteMavenPlugin) plugin));
578586
}
579587

580588
@Override
581589
public List<Path> getSourceFolders() {
582-
return Arrays.asList(getAbsolutePath().getParent().resolve(JAVA_SOURCE_FOLDER));
590+
return List.of(getAbsolutePath().getParent().resolve(JAVA_SOURCE_FOLDER));
583591
}
584592

585593
@Override
586594
public List<Path> getResourceFolders() {
587-
return Arrays.asList(getAbsolutePath().getParent().resolve(RESOURCE_FOLDER));
595+
return List.of(getAbsolutePath().getParent().resolve(RESOURCE_FOLDER));
588596
}
589597

590598
@Override
591599
public List<Path> getTestResourceFolders() {
592-
return Arrays.asList(getAbsolutePath().getParent().resolve(RESOURCE_TEST_FOLDER));
600+
return List.of(getAbsolutePath().getParent().resolve(RESOURCE_TEST_FOLDER));
593601
}
594602

595603
@Override
@@ -602,21 +610,23 @@ public List<Path> getClasspath() {
602610

603611
@Override
604612
public List<Path> getTestSourceFolders() {
605-
return Arrays.asList(getAbsolutePath().getParent().resolve(JAVA_TEST_SOURCE_FOLDER));
613+
return List.of(getAbsolutePath().getParent().resolve(JAVA_TEST_SOURCE_FOLDER));
606614
}
607615

608616
final public String getProperty(String key) {
609-
return getPom().getPom().getProperties().get(key);
617+
return getPom().getPom().getRequested().getProperties().get(key);
610618
}
611619

620+
@Override
621+
final public void deleteProperty(String key){
622+
apply(new RemoveProperty(key));
623+
apply(new RefreshPomModel());
624+
}
625+
612626
final public void setProperty(String key, String value) {
613-
if (value == null) {
614-
apply(new RemoveProperty(key));
615-
} else {
616-
String current = getProperty(key);
617-
apply(current == null ? new AddProperty(key, value) : new ChangePropertyValue(key, value, false));
618-
}
619-
apply(new RefreshPomModel());
627+
String current = getProperty(key);
628+
apply(current == null ? new ChangePropertyValue(key, value, true) : new ChangePropertyValue(key, value, false));
629+
apply(new RefreshPomModel());
620630
}
621631

622632
@Override
@@ -654,6 +664,7 @@ public String getArtifactId() {
654664
public String getVersion() {
655665
return resolve(getPom().getPom().getVersion());
656666
}
667+
657668
@Override
658669
public String getCoordinates() {
659670
return getGroupId() + ":" + getArtifactId() + ":" + getVersion();
@@ -754,45 +765,9 @@ private boolean anyRegexMatchesCoordinate(Plugin p, String... regex) {
754765

755766
@Override
756767
public List<Plugin> getPlugins() {
757-
758-
List<Plugin> plugins = new ArrayList<>();
759-
760-
MavenVisitor mavenVisitor = new MavenVisitor<ExecutionContext>() {
761-
762-
@Override
763-
public Xml.Document visitDocument(Xml.Document maven, ExecutionContext ctx) {
764-
Xml.Tag mavenRoot = maven.getRoot();
765-
Optional<Xml.Tag> build = mavenRoot.getChild("build");
766-
if (build.isPresent()) {
767-
Xml.Tag buildTag = build.get();
768-
Optional<Xml.Tag> pluginTags = buildTag.getChild("plugins");
769-
if (pluginTags.isPresent()) {
770-
List<Xml.Tag> plugin = pluginTags.get().getChildren("plugin");
771-
List<Plugin> pluginList = plugin.stream()
772-
.map(this::mapToPlugin)
773-
.collect(Collectors.toList());
774-
plugins.addAll(pluginList);
775-
}
776-
}
777-
return null;
778-
}
779-
780-
private Plugin mapToPlugin(Xml.Tag tag) {
781-
String groupId = tag.getChild("groupId").get().getValue().get();
782-
String artifactId = tag.getChild("artifactId").get().getValue().get();
783-
Optional<Xml.Tag> versionTag = tag.getChild("version");
784-
String version = null;
785-
if (versionTag.isPresent()) {
786-
version = versionTag.get().getValue().get();
787-
}
788-
Plugin plugin = new Plugin(groupId, artifactId, version, List.of(), "", "");
789-
return plugin;
790-
}
791-
};
792-
793-
mavenVisitor.visitDocument(getSourceFile(), executionContext);
794-
795-
return plugins;
768+
return getPom().getPom().getRequested().getPlugins().stream()
769+
.map(p -> new OpenRewriteMavenPlugin(p, getResource(), refactoring))
770+
.collect(Collectors.toList());
796771
}
797772

798773
/**
@@ -830,6 +805,14 @@ public void removePlugins(String... coordinates) {
830805
}
831806
}
832807

808+
@Override
809+
public Optional<Plugin> findPlugin(String groupId, String artifactId){
810+
return getPlugins()
811+
.stream()
812+
.filter(plugin -> plugin.getGroupId().equals(groupId) &&
813+
plugin.getArtifactId().equals(artifactId))
814+
.findAny();
815+
}
833816

834817
private String resolve(String expression) {
835818
return getPom().getPom().getValue(expression);

0 commit comments

Comments
 (0)