Skip to content

Remove standard maven-compiler plugin for applications with boot parent - gh-428 #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion components/sbm-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.14.0</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package org.springframework.sbm.build.api;

import org.openrewrite.maven.tree.Scope;

import org.springframework.sbm.project.resource.ProjectResource;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -117,6 +119,8 @@ public interface BuildFile extends ProjectResource {

String getProperty(String key);

void deleteProperty(String key);

String print();

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

List<String> getDeclaredModules();

Optional<Plugin> findPlugin(String groupId, String artifactId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,46 @@
*/
package org.springframework.sbm.build.api;

import lombok.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface Plugin {

String getGroupId();

String getArtifactId();

String getVersion();

String getDependencies();

List<? extends Execution> getExecutions();

Configuration getConfiguration();

interface Configuration {

Optional<String> getDeclaredStringValue(String property);

String getResolvedStringValue(String property);

void setDeclaredStringValue(String property, String value);

Set<String> getPropertyKeys();

}

interface Execution {

String getId();

List<String> getGoals();

String getPhase();

String getConfiguration();

}

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Plugin {

@NotNull
private String groupId;

@NotNull
private String artifactId;

private String version;

@Singular("execution")
private List<Execution> executions;

private String configuration;

private String dependencies;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Setter
public static class Execution {
@Null
private String id;
@Singular("goal")
private List<String> goals;
@Null
private String phase;
@Null
private String configuration;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import org.openrewrite.xml.tree.Xml;
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;
import org.springframework.util.ReflectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
class Refactoring<P> {
class MavenBuildFileRefactoring<P> {

private final RewriteSourceFileHolder<Xml.Document> pom;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.sbm.build.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Parser;
Expand All @@ -24,12 +25,16 @@
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.Markers;
import org.openrewrite.maven.*;
import org.openrewrite.maven.internal.MavenXmlMapper;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Parent;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.ResolvedManagedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.xml.tree.Xml.Tag;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.DependenciesChangedEvent;
Expand Down Expand Up @@ -70,7 +75,8 @@
public class OpenRewriteMavenBuildFile extends RewriteSourceFileHolder<Xml.Document> implements BuildFile {

private final ApplicationEventPublisher eventPublisher;
private PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();
private final PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();
private final MavenBuildFileRefactoring<Xml.Document> refactoring ;

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

public void apply(Recipe recipe) {
// 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
// FIXME: #7 this made it veeery slow
//executionContext.putMessage("org.openrewrite.maven.pomCache", new RocksdbMavenPomCache(this.getAbsoluteProjectDir()));
List<Result> result = recipe.run(List.of(getSourceFile()), executionContext).getResults();
if (!result.isEmpty()) {
replaceWith((Xml.Document) result.get(0).getAfter());
}
refactoring.execute(recipe);
}

public MavenResolutionResult getPom() {
return MavenBuildFileUtil.findMavenResolution(getSourceFile()).get();
}

public RewriteSourceFileHolder<Xml.Document> getResource() {
return this;
}

@Override
public void addDependency(Dependency dependency) {
if (!containsDependency(dependency)) {
Expand Down Expand Up @@ -263,7 +270,6 @@ public List<Dependency> getDeclaredDependencies(Scope... scopes) {
@Override
public List<Dependency> getRequestedDependencies() {
List<org.openrewrite.maven.tree.Dependency> requestedDependencies = getPom().getPom().getRequestedDependencies();

// FIXME: #7 use getPom().getDependencies() instead ?
List<Dependency> declaredDependenciesWithEffectiveVersions = requestedDependencies.stream()
.map(d -> mapDependency(d))
Expand Down Expand Up @@ -312,6 +318,8 @@ public List<Dependency> getRequestedDependencies() {

/**
* {@inheritDoc}
*
* TODO: #497 Test with declared and transitive dependencies
*/
@Override
public Set<Dependency> getEffectiveDependencies(Scope scope) {
Expand Down Expand Up @@ -574,22 +582,22 @@ public boolean hasPlugin(Plugin plugin) {

@Override
public void addPlugin(Plugin plugin) {
apply(new AddMavenPlugin(plugin));
apply(new AddMavenPlugin((OpenRewriteMavenPlugin) plugin));
}

@Override
public List<Path> getSourceFolders() {
return Arrays.asList(getAbsolutePath().getParent().resolve(JAVA_SOURCE_FOLDER));
return List.of(getAbsolutePath().getParent().resolve(JAVA_SOURCE_FOLDER));
}

@Override
public List<Path> getResourceFolders() {
return Arrays.asList(getAbsolutePath().getParent().resolve(RESOURCE_FOLDER));
return List.of(getAbsolutePath().getParent().resolve(RESOURCE_FOLDER));
}

@Override
public List<Path> getTestResourceFolders() {
return Arrays.asList(getAbsolutePath().getParent().resolve(RESOURCE_TEST_FOLDER));
return List.of(getAbsolutePath().getParent().resolve(RESOURCE_TEST_FOLDER));
}

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

@Override
public List<Path> getTestSourceFolders() {
return Arrays.asList(getAbsolutePath().getParent().resolve(JAVA_TEST_SOURCE_FOLDER));
return List.of(getAbsolutePath().getParent().resolve(JAVA_TEST_SOURCE_FOLDER));
}

final public String getProperty(String key) {
return getPom().getPom().getProperties().get(key);
return getPom().getPom().getRequested().getProperties().get(key);
}

@Override
final public void deleteProperty(String key){
apply(new RemoveProperty(key));
apply(new RefreshPomModel());
}

final public void setProperty(String key, String value) {
if (value == null) {
apply(new RemoveProperty(key));
} else {
String current = getProperty(key);
apply(current == null ? new AddProperty(key, value) : new ChangePropertyValue(key, value, false));
}
apply(new RefreshPomModel());
String current = getProperty(key);
apply(current == null ? new ChangePropertyValue(key, value, true) : new ChangePropertyValue(key, value, false));
apply(new RefreshPomModel());
}

@Override
Expand Down Expand Up @@ -654,6 +664,7 @@ public String getArtifactId() {
public String getVersion() {
return resolve(getPom().getPom().getVersion());
}

@Override
public String getCoordinates() {
return getGroupId() + ":" + getArtifactId() + ":" + getVersion();
Expand Down Expand Up @@ -754,45 +765,9 @@ private boolean anyRegexMatchesCoordinate(Plugin p, String... regex) {

@Override
public List<Plugin> getPlugins() {

List<Plugin> plugins = new ArrayList<>();

MavenVisitor mavenVisitor = new MavenVisitor<ExecutionContext>() {

@Override
public Xml.Document visitDocument(Xml.Document maven, ExecutionContext ctx) {
Xml.Tag mavenRoot = maven.getRoot();
Optional<Xml.Tag> build = mavenRoot.getChild("build");
if (build.isPresent()) {
Xml.Tag buildTag = build.get();
Optional<Xml.Tag> pluginTags = buildTag.getChild("plugins");
if (pluginTags.isPresent()) {
List<Xml.Tag> plugin = pluginTags.get().getChildren("plugin");
List<Plugin> pluginList = plugin.stream()
.map(this::mapToPlugin)
.collect(Collectors.toList());
plugins.addAll(pluginList);
}
}
return null;
}

private Plugin mapToPlugin(Xml.Tag tag) {
String groupId = tag.getChild("groupId").get().getValue().get();
String artifactId = tag.getChild("artifactId").get().getValue().get();
Optional<Xml.Tag> versionTag = tag.getChild("version");
String version = null;
if (versionTag.isPresent()) {
version = versionTag.get().getValue().get();
}
Plugin plugin = new Plugin(groupId, artifactId, version, List.of(), "", "");
return plugin;
}
};

mavenVisitor.visitDocument(getSourceFile(), executionContext);

return plugins;
return getPom().getPom().getRequested().getPlugins().stream()
.map(p -> new OpenRewriteMavenPlugin(p, getResource(), refactoring))
.collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -830,6 +805,14 @@ public void removePlugins(String... coordinates) {
}
}

@Override
public Optional<Plugin> findPlugin(String groupId, String artifactId){
return getPlugins()
.stream()
.filter(plugin -> plugin.getGroupId().equals(groupId) &&
plugin.getArtifactId().equals(artifactId))
.findAny();
}

private String resolve(String expression) {
return getPom().getPom().getValue(expression);
Expand Down
Loading