diff --git a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScope.java b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScope.java index 17a78f4c..d0ac02c6 100644 --- a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScope.java +++ b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScope.java @@ -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}. */ @@ -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 getProjectPaths(); /** - * The build path this scope belongs to. + * The build paths this scope belongs to. */ - BuildPath getBuildPath(); + Set getBuildPaths(); } diff --git a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScopeMatrix.java b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScopeMatrix.java index 65a7e3f2..2c64515a 100644 --- a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScopeMatrix.java +++ b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScopeMatrix.java @@ -20,14 +20,13 @@ public final class BuildScopeMatrix { private final Set buildPaths; private final Map buildScopes; - public BuildScopeMatrix(Collection projectPaths, Collection buildPaths) { + public BuildScopeMatrix( + Collection projectPaths, Collection 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 buildScopes = new HashMap<>(); for (ProjectPath projectPath : projectPaths) { for (BuildPath buildPath : buildPaths) { @@ -39,18 +38,31 @@ public String getId() { } @Override - public ProjectPath getProjectPath() { - return projectPath; + public Set getProjectPaths() { + return Collections.singleton(projectPath); } @Override - public BuildPath getBuildPath() { - return buildPath; + public Set getBuildPaths() { + return Collections.singleton(buildPath); } }); } } + for (BuildScope extra : extras) { + buildScopes.put(extra.getId(), extra); + } this.buildScopes = Collections.unmodifiableMap(buildScopes); + + // now collect all paths + HashSet pp = new HashSet<>(projectPaths); + HashSet 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) { @@ -71,12 +83,12 @@ public Collection allBuildPaths() { public Collection byProjectPath(ProjectPath projectPath) { return all().stream() - .filter(s -> s.getProjectPath().equals(projectPath)) + .filter(s -> s.getProjectPaths().contains(projectPath)) .collect(Collectors.toSet()); } public Collection 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 singleton(ProjectPath projectPath, BuildPath buildPath) { @@ -87,12 +99,13 @@ public Collection singleton(ProjectPath projectPath, BuildPath build return Collections.singleton(result); } - public Collection singletonById(String id) { - BuildScope result = buildScopes.get(id); - if (result == null) { - throw new IllegalArgumentException("no such build scope"); - } - return Collections.singleton(result); + public Collection select(ProjectPath projectPath, BuildPath buildPath) { + HashSet result = new HashSet<>(); + buildScopes.values().stream() + .filter(s -> s.getProjectPaths().contains(projectPath) + && s.getBuildPaths().contains(buildPath)) + .forEach(result::add); + return result; } public Collection union(Collection bs1, Collection bs2) { diff --git a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/CommonBuilds.java b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/CommonBuilds.java index f4d09e45..485f0e0b 100644 --- a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/CommonBuilds.java +++ b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/CommonBuilds.java @@ -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". */ @@ -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 getProjectPaths() { + return Collections.singleton(PROJECT_PATH_TEST); + } + + @Override + public Set getBuildPaths() { + return new HashSet<>(Arrays.asList(BUILD_PATH_COMPILE, BUILD_PATH_RUNTIME)); + } + }; } diff --git a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/java/JavaLanguage.java b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/java/JavaLanguage.java index 2ff215ba..74443e5a 100644 --- a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/java/JavaLanguage.java +++ b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/java/JavaLanguage.java @@ -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))); @@ -146,7 +156,7 @@ private Map 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, @@ -262,7 +272,7 @@ private Map 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( @@ -271,7 +281,7 @@ private Map 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; @@ -337,7 +347,8 @@ private Optional 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); } } @@ -418,8 +429,8 @@ private static int calculateWidth(JavaDependencyScope dependencyScope) { HashSet projectPaths = new HashSet<>(); HashSet 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()) { @@ -540,8 +551,9 @@ private JavaResolutionScope( this.mode = requireNonNull(mode, "mode"); this.wantedPresence = Collections.unmodifiableSet(new HashSet<>(wantedPresence)); Set 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( @@ -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")); }); }