Skip to content

Commit 690b5dd

Browse files
committed
Sort out maven3 and 2
1 parent 2d3d7cb commit 690b5dd

File tree

4 files changed

+82
-33
lines changed

4 files changed

+82
-33
lines changed

shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScope.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
package eu.maveniverse.maven.toolbox.shared;
99

10+
import java.util.Set;
11+
1012
/**
1113
* Build scope is certain combination of {@link ProjectPath} and {@link BuildPath}.
1214
*/
@@ -17,12 +19,12 @@ public interface BuildScope {
1719
String getId();
1820

1921
/**
20-
* The project path this scope belongs to.
22+
* The project paths this scope belongs to.
2123
*/
22-
ProjectPath getProjectPath();
24+
Set<ProjectPath> getProjectPaths();
2325

2426
/**
25-
* The build path this scope belongs to.
27+
* The build paths this scope belongs to.
2628
*/
27-
BuildPath getBuildPath();
29+
Set<BuildPath> getBuildPaths();
2830
}

shared/src/main/java/eu/maveniverse/maven/toolbox/shared/BuildScopeMatrix.java

+28-15
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ public final class BuildScopeMatrix {
2020
private final Set<BuildPath> buildPaths;
2121
private final Map<String, BuildScope> buildScopes;
2222

23-
public BuildScopeMatrix(Collection<ProjectPath> projectPaths, Collection<BuildPath> buildPaths) {
23+
public BuildScopeMatrix(
24+
Collection<ProjectPath> projectPaths, Collection<BuildPath> buildPaths, BuildScope... extras) {
2425
requireNonNull(projectPaths, "projectPath");
2526
requireNonNull(buildPaths, "buildPaths");
2627
if (projectPaths.isEmpty() || buildPaths.isEmpty()) {
2728
throw new IllegalArgumentException("empty matrix");
2829
}
29-
this.projectPaths = Collections.unmodifiableSet(new HashSet<>(projectPaths));
30-
this.buildPaths = Collections.unmodifiableSet(new HashSet<>(buildPaths));
3130
HashMap<String, BuildScope> buildScopes = new HashMap<>();
3231
for (ProjectPath projectPath : projectPaths) {
3332
for (BuildPath buildPath : buildPaths) {
@@ -39,18 +38,31 @@ public String getId() {
3938
}
4039

4140
@Override
42-
public ProjectPath getProjectPath() {
43-
return projectPath;
41+
public Set<ProjectPath> getProjectPaths() {
42+
return Collections.singleton(projectPath);
4443
}
4544

4645
@Override
47-
public BuildPath getBuildPath() {
48-
return buildPath;
46+
public Set<BuildPath> getBuildPaths() {
47+
return Collections.singleton(buildPath);
4948
}
5049
});
5150
}
5251
}
52+
for (BuildScope extra : extras) {
53+
buildScopes.put(extra.getId(), extra);
54+
}
5355
this.buildScopes = Collections.unmodifiableMap(buildScopes);
56+
57+
// now collect all paths
58+
HashSet<ProjectPath> pp = new HashSet<>(projectPaths);
59+
HashSet<BuildPath> bp = new HashSet<>(buildPaths);
60+
buildScopes.values().forEach(s -> {
61+
pp.addAll(s.getProjectPaths());
62+
bp.addAll(s.getBuildPaths());
63+
});
64+
this.projectPaths = Collections.unmodifiableSet(pp);
65+
this.buildPaths = Collections.unmodifiableSet(bp);
5466
}
5567

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

7284
public Collection<BuildScope> byProjectPath(ProjectPath projectPath) {
7385
return all().stream()
74-
.filter(s -> s.getProjectPath().equals(projectPath))
86+
.filter(s -> s.getProjectPaths().contains(projectPath))
7587
.collect(Collectors.toSet());
7688
}
7789

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

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

90-
public Collection<BuildScope> singletonById(String id) {
91-
BuildScope result = buildScopes.get(id);
92-
if (result == null) {
93-
throw new IllegalArgumentException("no such build scope");
94-
}
95-
return Collections.singleton(result);
102+
public Collection<BuildScope> select(ProjectPath projectPath, BuildPath buildPath) {
103+
HashSet<BuildScope> result = new HashSet<>();
104+
buildScopes.values().stream()
105+
.filter(s -> s.getProjectPaths().contains(projectPath)
106+
&& s.getBuildPaths().contains(buildPath))
107+
.forEach(result::add);
108+
return result;
96109
}
97110

98111
public Collection<BuildScope> union(Collection<BuildScope> bs1, Collection<BuildScope> bs2) {

shared/src/main/java/eu/maveniverse/maven/toolbox/shared/CommonBuilds.java

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
package eu.maveniverse.maven.toolbox.shared;
99

10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.HashSet;
13+
import java.util.Set;
14+
1015
/**
1116
* Set of constants meant to be used in "common builds".
1217
*/
@@ -72,4 +77,21 @@ public int order() {
7277
return 2;
7378
}
7479
};
80+
81+
public static final BuildScope MAVEN_TEST_BUILD_SCOPE = new BuildScope() {
82+
@Override
83+
public String getId() {
84+
return "test";
85+
}
86+
87+
@Override
88+
public Set<ProjectPath> getProjectPaths() {
89+
return Collections.singleton(PROJECT_PATH_TEST);
90+
}
91+
92+
@Override
93+
public Set<BuildPath> getBuildPaths() {
94+
return new HashSet<>(Arrays.asList(BUILD_PATH_COMPILE, BUILD_PATH_RUNTIME));
95+
}
96+
};
7597
}

shared/src/main/java/eu/maveniverse/maven/toolbox/shared/java/JavaLanguage.java

+26-14
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,32 @@ public enum MavenLevel {
3737
true,
3838
false,
3939
new BuildScopeMatrix(
40-
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
41-
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME))),
40+
Collections.singletonList(CommonBuilds.PROJECT_PATH_MAIN),
41+
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
42+
CommonBuilds.MAVEN_TEST_BUILD_SCOPE)),
4243
Maven3(
4344
true,
4445
false,
4546
true,
4647
false,
4748
new BuildScopeMatrix(
48-
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
49-
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME))),
50-
Maven4(
49+
Collections.singletonList(CommonBuilds.PROJECT_PATH_MAIN),
50+
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
51+
CommonBuilds.MAVEN_TEST_BUILD_SCOPE)),
52+
Maven4WithoutSystem(
5153
false,
5254
false,
5355
false,
5456
true,
5557
new BuildScopeMatrix(
58+
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
59+
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME))),
60+
Maven4WithSystem(
61+
true,
62+
false,
63+
false,
64+
true,
65+
new BuildScopeMatrix(
5666
Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
5767
Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME)));
5868

@@ -146,7 +156,7 @@ private Map<String, JavaDependencyScope> buildDependencyScopes() {
146156
false,
147157
buildScopeMatrix.union(
148158
buildScopeMatrix.byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
149-
buildScopeMatrix.singleton(
159+
buildScopeMatrix.select(
150160
CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
151161
result.put(
152162
DS_TEST,
@@ -262,7 +272,7 @@ private Map<String, JavaResolutionScope> buildResolutionScopes() {
262272
RS_TEST_COMPILE,
263273
this,
264274
ResolutionScope.Mode.ELIMINATE,
265-
buildScopeMatrix.singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
275+
buildScopeMatrix.select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
266276
Collections.singletonList(dependencyScopes.get(DS_SYSTEM)),
267277
nonTransitiveScopes));
268278
result.put(
@@ -271,7 +281,7 @@ private Map<String, JavaResolutionScope> buildResolutionScopes() {
271281
RS_TEST_RUNTIME,
272282
this,
273283
ResolutionScope.Mode.ELIMINATE,
274-
buildScopeMatrix.singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
284+
buildScopeMatrix.select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
275285
Collections.singletonList(dependencyScopes.get(DS_SYSTEM)),
276286
nonTransitiveScopes));
277287
return result;
@@ -337,7 +347,8 @@ private Optional<BuildScope> getMainProjectBuildScope(JavaDependencyScope javaDe
337347
.sorted(Comparator.comparing(BuildPath::order))
338348
.collect(Collectors.toList())) {
339349
for (BuildScope buildScope : javaDependencyScope.getPresence()) {
340-
if (buildScope.getProjectPath() == projectPath && buildScope.getBuildPath() == buildPath) {
350+
if (buildScope.getProjectPaths().contains(projectPath)
351+
&& buildScope.getBuildPaths().contains(buildPath)) {
341352
return Optional.of(buildScope);
342353
}
343354
}
@@ -418,8 +429,8 @@ private static int calculateWidth(JavaDependencyScope dependencyScope) {
418429
HashSet<ProjectPath> projectPaths = new HashSet<>();
419430
HashSet<BuildPath> buildPaths = new HashSet<>();
420431
dependencyScope.getPresence().forEach(s -> {
421-
projectPaths.add(s.getProjectPath());
422-
buildPaths.add(s.getBuildPath());
432+
projectPaths.addAll(s.getProjectPaths());
433+
buildPaths.addAll(s.getBuildPaths());
423434
});
424435
int result = 0;
425436
if (dependencyScope.isTransitive()) {
@@ -540,8 +551,9 @@ private JavaResolutionScope(
540551
this.mode = requireNonNull(mode, "mode");
541552
this.wantedPresence = Collections.unmodifiableSet(new HashSet<>(wantedPresence));
542553
Set<JavaDependencyScope> included = collectScopes(wantedPresence);
543-
if (explicitlyIncluded != null) {
544-
included.addAll(explicitlyIncluded);
554+
// here we may have null elements, based on existence of system scope
555+
if (explicitlyIncluded != null && !explicitlyIncluded.isEmpty()) {
556+
explicitlyIncluded.stream().filter(Objects::nonNull).forEach(included::add);
545557
}
546558
this.directlyIncluded = Collections.unmodifiableSet(included);
547559
this.transitivelyExcluded = Collections.unmodifiableSet(
@@ -638,7 +650,7 @@ private void dumpDependencyScopes() {
638650
System.out.println(" Presence: "
639651
+ s.getPresence().stream().map(BuildScope::getId).collect(Collectors.toSet()));
640652
System.out.println(" Main project scope: "
641-
+ s.getMainProjectBuildScope().get().getId());
653+
+ s.getMainProjectBuildScope().map(BuildScope::getId).orElse("null"));
642654
});
643655
}
644656

0 commit comments

Comments
 (0)