in src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java [225:390]
protected void preparePaths(Set<File> sourceFiles) {
File mainOutputDirectory = new File(getProject().getBuild().getOutputDirectory());
File mainModuleDescriptorClassFile = new File(mainOutputDirectory, "module-info.class");
JavaModuleDescriptor mainModuleDescriptor = null;
File testModuleDescriptorJavaFile = new File("module-info.java");
JavaModuleDescriptor testModuleDescriptor = null;
// Go through the source files to respect includes/excludes
for (File sourceFile : sourceFiles) {
// @todo verify if it is the root of a sourcedirectory?
if ("module-info.java".equals(sourceFile.getName())) {
testModuleDescriptorJavaFile = sourceFile;
break;
}
}
// Get additional information from the main module descriptor, if available
if (mainModuleDescriptorClassFile.exists()) {
ResolvePathsResult<String> result;
try {
ResolvePathsRequest<String> request = ResolvePathsRequest.ofStrings(testPath)
.setIncludeStatic(true)
.setMainModuleDescriptor(mainModuleDescriptorClassFile.getAbsolutePath());
Toolchain toolchain = getToolchain();
if (toolchain instanceof DefaultJavaToolChain) {
request.setJdkHome(((DefaultJavaToolChain) toolchain).getJavaHome());
}
result = locationManager.resolvePaths(request);
for (Entry<String, Exception> pathException :
result.getPathExceptions().entrySet()) {
Throwable cause = pathException.getValue();
while (cause.getCause() != null) {
cause = cause.getCause();
}
String fileName =
Paths.get(pathException.getKey()).getFileName().toString();
getLog().warn("Can't extract module name from " + fileName + ": " + cause.getMessage());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
mainModuleDescriptor = result.getMainModuleDescriptor();
pathElements = new LinkedHashMap<>(result.getPathElements().size());
pathElements.putAll(result.getPathElements());
modulepathElements = result.getModulepathElements().keySet();
classpathElements = result.getClasspathElements();
}
// Get additional information from the test module descriptor, if available
if (testModuleDescriptorJavaFile.exists()) {
ResolvePathsResult<String> result;
try {
ResolvePathsRequest<String> request = ResolvePathsRequest.ofStrings(testPath)
.setMainModuleDescriptor(testModuleDescriptorJavaFile.getAbsolutePath());
Toolchain toolchain = getToolchain();
if (toolchain instanceof DefaultJavaToolChain) {
request.setJdkHome(((DefaultJavaToolChain) toolchain).getJavaHome());
}
result = locationManager.resolvePaths(request);
} catch (IOException e) {
throw new RuntimeException(e);
}
testModuleDescriptor = result.getMainModuleDescriptor();
}
if (!useModulePath) {
pathElements = Collections.emptyMap();
modulepathElements = Collections.emptyList();
classpathElements = testPath;
return;
}
if (StringUtils.isNotEmpty(getRelease())) {
if (Integer.parseInt(getRelease()) < 9) {
pathElements = Collections.emptyMap();
modulepathElements = Collections.emptyList();
classpathElements = testPath;
return;
}
} else if (Double.parseDouble(getTarget()) < Double.parseDouble(MODULE_INFO_TARGET)) {
pathElements = Collections.emptyMap();
modulepathElements = Collections.emptyList();
classpathElements = testPath;
return;
}
if (testModuleDescriptor != null) {
modulepathElements = testPath;
classpathElements = Collections.emptyList();
if (mainModuleDescriptor != null) {
if (getLog().isDebugEnabled()) {
getLog().debug("Main and test module descriptors exist:");
getLog().debug(" main module = " + mainModuleDescriptor.name());
getLog().debug(" test module = " + testModuleDescriptor.name());
}
if (testModuleDescriptor.name().equals(mainModuleDescriptor.name())) {
if (compilerArgs == null) {
compilerArgs = new ArrayList<>();
}
compilerArgs.add("--patch-module");
StringBuilder patchModuleValue = new StringBuilder();
patchModuleValue.append(testModuleDescriptor.name());
patchModuleValue.append('=');
for (String root : getProject().getCompileSourceRoots()) {
if (Files.exists(Paths.get(root))) {
patchModuleValue.append(root).append(PS);
}
}
compilerArgs.add(patchModuleValue.toString());
} else {
getLog().debug("Black-box testing - all is ready to compile");
}
} else {
// No main binaries available? Means we're a test-only project.
if (!mainOutputDirectory.exists()) {
return;
}
// very odd
// Means that main sources must be compiled with -modulesource and -Xmodule:<moduleName>
// However, this has a huge impact since you can't simply use it as a classpathEntry
// due to extra folder in between
throw new UnsupportedOperationException(
"Can't compile test sources " + "when main sources are missing a module descriptor");
}
} else {
if (mainModuleDescriptor != null) {
if (compilerArgs == null) {
compilerArgs = new ArrayList<>();
}
compilerArgs.add("--patch-module");
StringBuilder patchModuleValue = new StringBuilder(mainModuleDescriptor.name())
.append('=')
.append(mainOutputDirectory)
.append(PS);
for (String root : compileSourceRoots) {
patchModuleValue.append(root).append(PS);
}
compilerArgs.add(patchModuleValue.toString());
compilerArgs.add("--add-reads");
compilerArgs.add(mainModuleDescriptor.name() + "=ALL-UNNAMED");
} else {
modulepathElements = Collections.emptyList();
classpathElements = testPath;
}
}
}