Skip to content

Commit

Permalink
Sort out maven3 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 12, 2024
1 parent 2d3d7cb commit 690b5dd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package eu.maveniverse.maven.toolbox.shared;

import java.util.Set;

/**
* Build scope is certain combination of {@link ProjectPath} and {@link BuildPath}.
*/
Expand All @@ -17,12 +19,12 @@ public interface BuildScope {
String getId();

/**
* The project path this scope belongs to.
* The project paths this scope belongs to.
*/
ProjectPath getProjectPath();
Set<ProjectPath> getProjectPaths();

/**
* The build path this scope belongs to.
* The build paths this scope belongs to.
*/
BuildPath getBuildPath();
Set<BuildPath> getBuildPaths();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public final class BuildScopeMatrix {
private final Set<BuildPath> buildPaths;
private final Map<String, BuildScope> buildScopes;

public BuildScopeMatrix(Collection<ProjectPath> projectPaths, Collection<BuildPath> buildPaths) {
public BuildScopeMatrix(
Collection<ProjectPath> projectPaths, Collection<BuildPath> buildPaths, BuildScope... extras) {
requireNonNull(projectPaths, "projectPath");
requireNonNull(buildPaths, "buildPaths");
if (projectPaths.isEmpty() || buildPaths.isEmpty()) {
throw new IllegalArgumentException("empty matrix");
}
this.projectPaths = Collections.unmodifiableSet(new HashSet<>(projectPaths));
this.buildPaths = Collections.unmodifiableSet(new HashSet<>(buildPaths));
HashMap<String, BuildScope> buildScopes = new HashMap<>();
for (ProjectPath projectPath : projectPaths) {
for (BuildPath buildPath : buildPaths) {
Expand All @@ -39,18 +38,31 @@ public String getId() {
}

@Override
public ProjectPath getProjectPath() {
return projectPath;
public Set<ProjectPath> getProjectPaths() {
return Collections.singleton(projectPath);
}

@Override
public BuildPath getBuildPath() {
return buildPath;
public Set<BuildPath> getBuildPaths() {
return Collections.singleton(buildPath);
}
});
}
}
for (BuildScope extra : extras) {
buildScopes.put(extra.getId(), extra);
}
this.buildScopes = Collections.unmodifiableMap(buildScopes);

// now collect all paths
HashSet<ProjectPath> pp = new HashSet<>(projectPaths);
HashSet<BuildPath> bp = new HashSet<>(buildPaths);
buildScopes.values().forEach(s -> {
pp.addAll(s.getProjectPaths());
bp.addAll(s.getBuildPaths());
});
this.projectPaths = Collections.unmodifiableSet(pp);
this.buildPaths = Collections.unmodifiableSet(bp);
}

private String createId(ProjectPath projectPath, BuildPath buildPath) {
Expand All @@ -71,12 +83,12 @@ public Collection<BuildPath> allBuildPaths() {

public Collection<BuildScope> byProjectPath(ProjectPath projectPath) {
return all().stream()
.filter(s -> s.getProjectPath().equals(projectPath))
.filter(s -> s.getProjectPaths().contains(projectPath))
.collect(Collectors.toSet());
}

public Collection<BuildScope> byBuildPath(BuildPath buildPath) {
return all().stream().filter(s -> s.getBuildPath().equals(buildPath)).collect(Collectors.toSet());
return all().stream().filter(s -> s.getBuildPaths().contains(buildPath)).collect(Collectors.toSet());
}

public Collection<BuildScope> singleton(ProjectPath projectPath, BuildPath buildPath) {
Expand All @@ -87,12 +99,13 @@ public Collection<BuildScope> singleton(ProjectPath projectPath, BuildPath build
return Collections.singleton(result);
}

public Collection<BuildScope> singletonById(String id) {
BuildScope result = buildScopes.get(id);
if (result == null) {
throw new IllegalArgumentException("no such build scope");
}
return Collections.singleton(result);
public Collection<BuildScope> select(ProjectPath projectPath, BuildPath buildPath) {
HashSet<BuildScope> result = new HashSet<>();
buildScopes.values().stream()
.filter(s -> s.getProjectPaths().contains(projectPath)
&& s.getBuildPaths().contains(buildPath))
.forEach(result::add);
return result;
}

public Collection<BuildScope> union(Collection<BuildScope> bs1, Collection<BuildScope> bs2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
package eu.maveniverse.maven.toolbox.shared;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
* Set of constants meant to be used in "common builds".
*/
Expand Down Expand Up @@ -72,4 +77,21 @@ public int order() {
return 2;
}
};

public static final BuildScope MAVEN_TEST_BUILD_SCOPE = new BuildScope() {
@Override
public String getId() {
return "test";
}

@Override
public Set<ProjectPath> getProjectPaths() {
return Collections.singleton(PROJECT_PATH_TEST);
}

@Override
public Set<BuildPath> getBuildPaths() {
return new HashSet<>(Arrays.asList(BUILD_PATH_COMPILE, BUILD_PATH_RUNTIME));
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,32 @@ public enum MavenLevel {
true,
false,
new BuildScopeMatrix(
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME))),
Collections.singletonList(CommonBuilds.PROJECT_PATH_MAIN),
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
CommonBuilds.MAVEN_TEST_BUILD_SCOPE)),
Maven3(
true,
false,
true,
false,
new BuildScopeMatrix(
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME))),
Maven4(
Collections.singletonList(CommonBuilds.PROJECT_PATH_MAIN),
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
CommonBuilds.MAVEN_TEST_BUILD_SCOPE)),
Maven4WithoutSystem(
false,
false,
false,
true,
new BuildScopeMatrix(
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME))),
Maven4WithSystem(
true,
false,
false,
true,
new BuildScopeMatrix(
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME)));

Expand Down Expand Up @@ -146,7 +156,7 @@ private Map<String, JavaDependencyScope> buildDependencyScopes() {
false,
buildScopeMatrix.union(
buildScopeMatrix.byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
buildScopeMatrix.singleton(
buildScopeMatrix.select(
CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
result.put(
DS_TEST,
Expand Down Expand Up @@ -262,7 +272,7 @@ private Map<String, JavaResolutionScope> buildResolutionScopes() {
RS_TEST_COMPILE,
this,
ResolutionScope.Mode.ELIMINATE,
buildScopeMatrix.singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
buildScopeMatrix.select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
Collections.singletonList(dependencyScopes.get(DS_SYSTEM)),
nonTransitiveScopes));
result.put(
Expand All @@ -271,7 +281,7 @@ private Map<String, JavaResolutionScope> buildResolutionScopes() {
RS_TEST_RUNTIME,
this,
ResolutionScope.Mode.ELIMINATE,
buildScopeMatrix.singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
buildScopeMatrix.select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
Collections.singletonList(dependencyScopes.get(DS_SYSTEM)),
nonTransitiveScopes));
return result;
Expand Down Expand Up @@ -337,7 +347,8 @@ private Optional<BuildScope> getMainProjectBuildScope(JavaDependencyScope javaDe
.sorted(Comparator.comparing(BuildPath::order))
.collect(Collectors.toList())) {
for (BuildScope buildScope : javaDependencyScope.getPresence()) {
if (buildScope.getProjectPath() == projectPath && buildScope.getBuildPath() == buildPath) {
if (buildScope.getProjectPaths().contains(projectPath)
&& buildScope.getBuildPaths().contains(buildPath)) {
return Optional.of(buildScope);
}
}
Expand Down Expand Up @@ -418,8 +429,8 @@ private static int calculateWidth(JavaDependencyScope dependencyScope) {
HashSet<ProjectPath> projectPaths = new HashSet<>();
HashSet<BuildPath> buildPaths = new HashSet<>();
dependencyScope.getPresence().forEach(s -> {
projectPaths.add(s.getProjectPath());
buildPaths.add(s.getBuildPath());
projectPaths.addAll(s.getProjectPaths());
buildPaths.addAll(s.getBuildPaths());
});
int result = 0;
if (dependencyScope.isTransitive()) {
Expand Down Expand Up @@ -540,8 +551,9 @@ private JavaResolutionScope(
this.mode = requireNonNull(mode, "mode");
this.wantedPresence = Collections.unmodifiableSet(new HashSet<>(wantedPresence));
Set<JavaDependencyScope> included = collectScopes(wantedPresence);
if (explicitlyIncluded != null) {
included.addAll(explicitlyIncluded);
// here we may have null elements, based on existence of system scope
if (explicitlyIncluded != null && !explicitlyIncluded.isEmpty()) {
explicitlyIncluded.stream().filter(Objects::nonNull).forEach(included::add);
}
this.directlyIncluded = Collections.unmodifiableSet(included);
this.transitivelyExcluded = Collections.unmodifiableSet(
Expand Down Expand Up @@ -638,7 +650,7 @@ private void dumpDependencyScopes() {
System.out.println(" Presence: "
+ s.getPresence().stream().map(BuildScope::getId).collect(Collectors.toSet()));
System.out.println(" Main project scope: "
+ s.getMainProjectBuildScope().get().getId());
+ s.getMainProjectBuildScope().map(BuildScope::getId).orElse("null"));
});
}

Expand Down

0 comments on commit 690b5dd

Please sign in to comment.