Skip to content

Allow defined and automatic modules to co-exist #164

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Consumer;

import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaProvides;

Expand Down Expand Up @@ -249,22 +250,47 @@ public String extract(Path path) throws IOException {
request.isIncludeStatic());
}

// in case of identical module names, first one wins
Map<T, JavaModuleDescriptor> definedModules = new LinkedHashMap<>();
Map<T, JavaModuleDescriptor> automaticModules = new LinkedHashMap<>();

pathElements.forEach((k, v) -> {
if (v != null && !v.isAutomatic()) {
definedModules.put(k, v);
} else {
automaticModules.put(k, v);
}
});

// in case of identical module names, first one wins, others drop onto classpath
// if they are automatic modules.
Set<String> collectedModules = new HashSet<>(requiredNamedModules.size());

for (Entry<T, JavaModuleDescriptor> entry : pathElements.entrySet()) {
if (entry.getValue() != null
&& requiredNamedModules.contains(entry.getValue().name())) {
if (collectedModules.add(entry.getValue().name())) {
result.getModulepathElements()
.put(
entry.getKey(),
moduleNameSources.get(entry.getValue().name()));
Consumer<Map<T, JavaModuleDescriptor>> moduleAcceptor = moduleSet -> {
for (Entry<T, JavaModuleDescriptor> entry : moduleSet.entrySet()) {
if (entry.getValue() != null
&& requiredNamedModules.contains(entry.getValue().name())) {
if (collectedModules.add(entry.getValue().name())) {
result.getModulepathElements()
.put(
entry.getKey(),
moduleNameSources.get(entry.getValue().name()));
// if the module is an automatic module, add it to the classpath
} else if (entry.getValue().isAutomatic()) {
result.getClasspathElements().add(entry.getKey());
}
} else {
result.getClasspathElements().add(entry.getKey());
}
} else {
result.getClasspathElements().add(entry.getKey());
}
}
};

// process defined modules first. This fixes a corner case where a project creates
// a main artifact that is a JPMS module and a tests artifact that is not. If the
// main artifact and the test artifact happen to have the same module id (one from
// the module-info, one from the automatic module naming), the main artifact will
// be on the module path and the test artifact will be on the class path.
moduleAcceptor.accept(definedModules);
moduleAcceptor.accept(automaticModules);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,32 @@ void testIdenticalModuleNames() throws Exception {
ResolvePathsRequest<Path> request =
ResolvePathsRequest.ofPaths(Arrays.asList(pj1, pj2)).setMainModuleDescriptor(mockModuleInfoJava);

when(asmParser.getModuleDescriptor(pj1))
.thenReturn(JavaModuleDescriptor.newModule("plexus.java").build());
when(asmParser.getModuleDescriptor(pj2))
.thenReturn(JavaModuleDescriptor.newModule("plexus.java").build());

ResolvePathsResult<Path> result = locationManager.resolvePaths(request);

assertThat(result.getMainModuleDescriptor()).isEqualTo(descriptor);
assertThat(result.getPathElements()).hasSize(2);
assertThat(result.getModulepathElements()).hasSize(1);
assertThat(result.getModulepathElements()).containsKey(pj1);
assertThat(result.getModulepathElements()).doesNotContainKey(pj2);
assertThat(result.getClasspathElements()).isEmpty();
assertThat(result.getPathExceptions()).isEmpty();
}

@Test
public void testIdenticalAutomaticModuleNames() throws Exception {
Path pj1 = Paths.get("src/test/resources/jar.empty/plexus-java-1.0.0-SNAPSHOT.jar");
Path pj2 = Paths.get("src/test/resources/jar.empty.2/plexus-java-2.0.0-SNAPSHOT.jar");
JavaModuleDescriptor descriptor =
JavaModuleDescriptor.newModule("base").requires("plexus.java").build();
when(qdoxParser.fromSourcePath(any(Path.class))).thenReturn(descriptor);
ResolvePathsRequest<Path> request =
ResolvePathsRequest.ofPaths(Arrays.asList(pj1, pj2)).setMainModuleDescriptor(mockModuleInfoJava);

when(asmParser.getModuleDescriptor(pj1))
.thenReturn(
JavaModuleDescriptor.newAutomaticModule("plexus.java").build());
Expand All @@ -173,8 +199,35 @@ void testIdenticalModuleNames() throws Exception {
assertThat(result.getPathElements()).hasSize(2);
assertThat(result.getModulepathElements()).containsOnlyKeys(pj1);
assertThat(result.getModulepathElements()).doesNotContainKey(pj2);
assertThat(result.getClasspathElements()).hasSize(0);
assertThat(result.getPathExceptions()).hasSize(0);
assertThat(result.getClasspathElements()).hasSize(1);
assertThat(result.getPathExceptions()).isEmpty();
}

@Test
public void testMainJarModuleAndTestJarAutomatic() throws Exception {
Path pj1 = Paths.get("src/test/resources/jar.tests/plexus-java-1.0.0-SNAPSHOT.jar");
Path pj2 = Paths.get("src/test/resources/jar.tests/plexus-java-1.0.0-SNAPSHOT-tests.jar");
JavaModuleDescriptor descriptor =
JavaModuleDescriptor.newModule("base").requires("plexus.java").build();
when(qdoxParser.fromSourcePath(any(Path.class))).thenReturn(descriptor);
ResolvePathsRequest<Path> request =
ResolvePathsRequest.ofPaths(Arrays.asList(pj1, pj2)).setMainModuleDescriptor(mockModuleInfoJava);

when(asmParser.getModuleDescriptor(pj1))
.thenReturn(JavaModuleDescriptor.newModule("plexus.java").build());
when(asmParser.getModuleDescriptor(pj2)).thenReturn(null);

ResolvePathsResult<Path> result = locationManager.resolvePaths(request);

assertThat(result.getMainModuleDescriptor()).isEqualTo(descriptor);
assertThat(result.getPathElements()).hasSize(2);
assertThat(result.getModulepathElements()).hasSize(1);
assertThat(result.getModulepathElements()).containsKey(pj1);
assertThat(result.getModulepathElements()).doesNotContainKey(pj2);
assertThat(result.getClasspathElements()).hasSize(1);
assertThat(result.getClasspathElements()).contains(pj2);
assertThat(result.getClasspathElements()).doesNotContain(pj1);
assertThat(result.getPathExceptions()).isEmpty();
}

@Test
Expand Down
Binary file not shown.
Binary file not shown.