Skip to content
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

Apply add dependency correctly, taking aggregating poms into account #4590

Merged
merged 16 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.maven.tree.*;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.semver.Semver;
import org.openrewrite.xml.tree.Xml;

import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -180,17 +186,17 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
acc.usingType = true;
JavaProject javaProject = sourceFile.getMarkers().findFirst(JavaProject.class).orElse(null);
JavaSourceSet javaSourceSet = sourceFile.getMarkers().findFirst(JavaSourceSet.class).orElse(null);
if(javaProject != null && javaSourceSet != null) {
if (javaProject != null && javaSourceSet != null) {
acc.scopeByProject.compute(javaProject, (jp, scope) -> "compile".equals(scope) ?
scope /* a `compile` scope dependency will also be available in test source set */ :
"test".equals(javaSourceSet.getName()) ? "test" : "compile"
);
}
}
} else if(tree instanceof Xml.Document) {
} else if (tree instanceof Xml.Document) {
Xml.Document doc = (Xml.Document) tree;
MavenResolutionResult mrr = doc.getMarkers().findFirst(MavenResolutionResult.class).orElse(null);
if(mrr == null) {
if (mrr == null) {
return sourceFile;
}
acc.pomsDefinedInCurrentRepository.add(mrr.getPom().getGav());
Expand Down Expand Up @@ -235,14 +241,29 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
}
}

if(onlyIfUsing == null && getResolutionResult().getParent() != null && acc.pomsDefinedInCurrentRepository.contains(getResolutionResult().getParent().getPom().getGav())) {
if (onlyIfUsing == null && getResolutionResult().getParent() != null && acc.pomsDefinedInCurrentRepository.contains(getResolutionResult().getParent().getPom().getGav())) {
return maven;
}

if (!getResolutionResult().getPom().getSubprojects().isEmpty() &&
(getResolutionResult().getModules().isEmpty() ||
!anyChildIsSubProject())) {
return maven;
}

return new AddDependencyVisitor(
groupId, artifactId, version, versionPattern, resolvedScope, releasesOnly,
type, classifier, optional, familyPatternCompiled, metadataFailures).visitNonNull(document, ctx);
}

private boolean anyChildIsSubProject() {
for (MavenResolutionResult child : getResolutionResult().getModules()) {
if (getResolutionResult().getPom().getSubprojects().contains(child.getPom().getGav().getArtifactId())) {
return true;
}
}
return false;
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import lombok.experimental.NonFinal;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.maven.tree.*;

Expand Down Expand Up @@ -118,6 +119,12 @@ public class RawPom {
@Nullable
Profiles profiles;

@Nullable
Modules modules;

@Nullable
SubProjects subprojects;

public static RawPom parse(InputStream inputStream, @Nullable String snapshotVersion) {
try {
RawPom pom = MavenXmlMapper.readMapper().readValue(inputStream, RawPom.class);
Expand Down Expand Up @@ -217,6 +224,32 @@ public Profiles(@JacksonXmlProperty(localName = "profile") List<Profile> profile
}
}

@Getter
public static class Modules {
private final List<String> modules;

public Modules() {
this.modules = emptyList();
}

public Modules(@JacksonXmlProperty(localName = "module") List<String> modules) {
this.modules = modules;
}
}

@Getter
public static class SubProjects {
private final List<String> subprojects;

public SubProjects() {
this.subprojects = emptyList();
}

public SubProjects(@JacksonXmlProperty(localName = "subproject") List<String> subprojects) {
this.subprojects = subprojects;
}
}

@FieldDefaults(level = AccessLevel.PRIVATE)
@Data
public static class Build {
Expand Down Expand Up @@ -346,9 +379,9 @@ public static class Profile {
}

public @Nullable String getVersion() {
if(version == null) {
if(currentVersion == null) {
if(parent == null) {
if (version == null) {
if (currentVersion == null) {
if (parent == null) {
return null;
} else {
return parent.getVersion();
Expand Down Expand Up @@ -382,8 +415,9 @@ public Pom toPom(@Nullable Path inputPath, @Nullable MavenRepository repo) {
.packaging(packaging)
.properties(getProperties() == null ? emptyMap() : getProperties())
.licenses(mapLicenses(getLicenses()))
.profiles(mapProfiles(getProfiles()));
if(StringUtils.isBlank(pomVersion)) {
.profiles(mapProfiles(getProfiles()))
.subprojects(mapSubProjects(getModules(), getSubprojects()));
if (StringUtils.isBlank(pomVersion)) {
builder.dependencies(mapRequestedDependencies(getDependencies()))
.dependencyManagement(mapDependencyManagement(getDependencyManagement()))
.repositories(mapRepositories(getRepositories()))
Expand Down Expand Up @@ -547,4 +581,17 @@ private List<org.openrewrite.maven.tree.Plugin.Execution> mapPluginExecutions(@N
return executions;
}

private List<String> mapSubProjects(@Nullable Modules modules, @Nullable SubProjects subprojects) {
if (modules == null && subprojects != null) {
return subprojects.getSubprojects();
}
if (subprojects == null && modules != null) {
return modules.getModules();
}
if (modules != null && subprojects != null) {
return ListUtils.concatAll(modules.getModules(), subprojects.getSubprojects());
}
return emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public boolean parentPomIsProjectPom() {
.anyMatch(gav -> gav.equals(parentGav));
}

public boolean isAggregatingPom() {
return !getPom().getSubprojects().isEmpty();
}
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

private Map<Path, Pom> getProjectPomsRecursive(Map<Path, Pom> projectPoms) {
projectPoms.put(requireNonNull(pom.getRequested().getSourcePath()), pom.getRequested());
if (parent != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public static int getModelVersion() {
@Builder.Default
List<Plugin> pluginManagement = emptyList();

@Builder.Default
List<String> subprojects = emptyList();

public String getGroupId() {
return gav.getGroupId();
}
Expand Down Expand Up @@ -184,7 +187,8 @@ public ResolvedPom resolve(Iterable<String> activeProfiles,
repositories,
dependencies,
plugins,
pluginManagement)
pluginManagement,
subprojects)
.resolve(ctx, downloader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public class ResolvedPom {
Iterable<String> activeProfiles;

public ResolvedPom(Pom requested, Iterable<String> activeProfiles) {
this(requested, activeProfiles, emptyMap(), emptyList(), null, emptyList(), emptyList(), emptyList(), emptyList());
this(requested, activeProfiles, emptyMap(), emptyList(), null, emptyList(), emptyList(), emptyList(), emptyList(), emptyList());
}

@JsonCreator
ResolvedPom(Pom requested, Iterable<String> activeProfiles, Map<String, String> properties, List<ResolvedManagedDependency> dependencyManagement, @Nullable List<MavenRepository> initialRepositories, List<MavenRepository> repositories, List<Dependency> requestedDependencies, List<Plugin> plugins, List<Plugin> pluginManagement) {
ResolvedPom(Pom requested, Iterable<String> activeProfiles, Map<String, String> properties, List<ResolvedManagedDependency> dependencyManagement, @Nullable List<MavenRepository> initialRepositories, List<MavenRepository> repositories, List<Dependency> requestedDependencies, List<Plugin> plugins, List<Plugin> pluginManagement, List<String> subprojects) {
this.requested = requested;
this.activeProfiles = activeProfiles;
this.properties = properties;
Expand All @@ -83,6 +83,7 @@ public ResolvedPom(Pom requested, Iterable<String> activeProfiles) {
this.requestedDependencies = requestedDependencies;
this.plugins = plugins;
this.pluginManagement = pluginManagement;
this.subprojects = subprojects;
}

@NonFinal
Expand Down Expand Up @@ -113,6 +114,10 @@ public ResolvedPom(Pom requested, Iterable<String> activeProfiles) {
@Builder.Default
List<Plugin> pluginManagement = emptyList();

@NonFinal
@Builder.Default
List<String> subprojects = emptyList();


/**
* Deduplicate dependencies and dependency management dependencies
Expand Down Expand Up @@ -172,6 +177,7 @@ public ResolvedPom resolve(ExecutionContext ctx, MavenPomDownloader downloader)
emptyList(),
emptyList(),
emptyList(),
emptyList(),
emptyList()
).resolver(ctx, downloader).resolve();

Expand Down Expand Up @@ -926,7 +932,7 @@ public List<ResolvedDependency> resolveDependencies(Scope scope, Map<GroupArtifa
ResolvedPom resolvedPom = cache.getResolvedDependencyPom(dPom.getGav());
if (resolvedPom == null) {
resolvedPom = new ResolvedPom(dPom, getActiveProfiles(), emptyMap(),
emptyList(), initialRepositories, emptyList(), emptyList(), emptyList(), emptyList());
emptyList(), initialRepositories, emptyList(), emptyList(), emptyList(), emptyList(), emptyList());
resolvedPom.resolver(ctx, downloader).resolveParentsRecursively(dPom);
cache.putResolvedDependencyPom(dPom.getGav(), resolvedPom);
}
Expand Down
Loading