Skip to content

Test for getDependency variations #520

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 3 commits into from
Nov 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -263,35 +263,45 @@ 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))
.map(d -> {
if(d.getType() == null || d.getClassifier() == null || d.getVersion() == null) {

String groupId = evaluate(d.getGroupId());
String artifactId = evaluate(d.getArtifactId());
String version = evaluate(d.getVersion());
// resolve values for properties like ${my.artifactId} or ${dep.version}
String resolvedGroupId = resolve(d.getGroupId());
String resolvedArtifactId = resolve(d.getArtifactId());
String resolvedVersion = resolve(d.getVersion());

List<ResolvedDependency> dependencies = getPom().findDependencies(
groupId,
artifactId,
resolvedGroupId,
resolvedArtifactId,
d.getScope() != null ? Scope.fromName(d.getScope()) : null
);
if (dependencies.isEmpty()) {
// requested dependency from another module in this multi-module project won't be resolvable
return d;
d.setGroupId(resolvedGroupId);
d.setArtifactId(resolvedArtifactId);
d.setVersion(resolvedVersion);
}
else {
ResolvedDependency resolvedDependency = dependencies.get(0);
d.setGroupId(resolvedGroupId);
d.setArtifactId(resolvedArtifactId);
d.setVersion(resolvedDependency.getVersion());
d.setClassifier(resolvedDependency.getClassifier());
d.setType(resolvedDependency.getType());
}
ResolvedDependency resolvedDependency = dependencies.get(0);
d.setVersion(resolvedDependency.getVersion());
d.setClassifier(resolvedDependency.getClassifier());
d.setType(resolvedDependency.getType());
d.setArtifactId(artifactId);
d.setGroupId(groupId);


if(d.getScope() == null ) {
String s = resolveScope(groupId, artifactId, d.getType(), d.getClassifier());
d.setScope(s);
String s = resolveScope(resolvedGroupId, resolvedArtifactId, d.getType(), d.getClassifier());
if(s == null) {
s = "compile";
}
d.setScope(s.toLowerCase());
}
}
return d;
Expand All @@ -302,8 +312,6 @@ public List<Dependency> getRequestedDependencies() {

/**
* {@inheritDoc}
*
* TODO: #497 Test with declared and transitive dependencies
*/
@Override
public Set<Dependency> getEffectiveDependencies(Scope scope) {
Expand All @@ -315,10 +323,28 @@ public Set<Dependency> getEffectiveDependencies(Scope scope) {

@Override
public Set<Dependency> getEffectiveDependencies() {
return getPom().getDependencies().entrySet()
Set<Dependency> collect = getPom()
.getDependencies()
.get(Scope.Compile)
.stream()
.flatMap(e -> e.getValue().stream().map(v -> mapDependency(e.getKey(), v)))
.map(d -> mapDependency(Scope.Compile, d))
.collect(Collectors.toSet());

getPom()
.getDependencies()
.get(Scope.Provided)
.stream()
.map(d -> mapDependency(Scope.Provided, d))
.forEach(d -> collect.add(d));

getPom()
.getDependencies()
.get(Scope.Test)
.stream()
.map(d -> mapDependency(Scope.Test, d))
.forEach(d -> collect.add(d));

return collect;
}

@Override
Expand Down Expand Up @@ -367,7 +393,7 @@ private org.springframework.sbm.build.api.Dependency mapDependency(org.openrewri

private String resolveScope(String groupId, String artifactId, @Nullable String type, @Nullable String classifier) {
Scope managedScope = getPom().getPom().getManagedScope(groupId, artifactId, type, classifier);
return managedScope != null ? managedScope.name().toLowerCase() : null;
return managedScope != null ? managedScope.name().toLowerCase() : "compile";
}

private org.springframework.sbm.build.api.Dependency mapDependency(Scope scope, ResolvedDependency d) {
Expand All @@ -376,12 +402,15 @@ private org.springframework.sbm.build.api.Dependency mapDependency(Scope scope,
d.getArtifactId(),
d.getVersion(),
d.getType(),
scope.name(),
scope.name().toLowerCase(),
d.getClassifier(),
d.getRequested().getExclusions()
.stream()
.map(e -> Dependency.builder().groupId(e.getGroupId()).artifactId(e.getArtifactId()).build())
.collect(Collectors.toList())
d.getRequested().getExclusions() != null ?
d.getRequested().getExclusions()
.stream()
.map(e -> Dependency.builder().groupId(e.getGroupId()).artifactId(e.getArtifactId()).build())
.collect(Collectors.toList())
:
List.of()
);
}

Expand Down Expand Up @@ -623,7 +652,7 @@ public String getArtifactId() {

@Override
public String getVersion() {
return evaluate(getPom().getPom().getVersion());
return resolve(getPom().getPom().getVersion());
}
@Override
public String getCoordinates() {
Expand Down Expand Up @@ -802,7 +831,7 @@ public void removePlugins(String... coordinates) {
}


private String evaluate(String expression) {
private String resolve(String expression) {
return getPom().getPom().getValue(expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static Consumer<Throwable> createErrorHandler() {
}

public Set<Dependency> mapCoordinatesToDependencies(List<String> coordinates) {
Set<Dependency> dependencies = new HashSet<>();
Set<Dependency> dependencies = new LinkedHashSet<>();
coordinates.forEach(c -> {

String[] parts = c.split(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void multiModuleProject() {

String module1Pom = PomBuilder
.buiildPom("com.example:parent:1.0", "module1")
.dependencies("com.example:module2:1.0")
.unscopedDependencies("com.example:module2:1.0")
.build();

String module2Pom = PomBuilder.buiildPom("com.example:parent:1.0", "module2").build();
Expand Down
Loading