use of org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor in project maven-plugins by apache.
the class TestCompilerMojo method preparePaths.
@Override
protected void preparePaths(Set<File> sourceFiles) {
File mainOutputDirectory = new File(getProject().getBuild().getOutputDirectory());
File mainModuleDescriptor = new File(mainOutputDirectory, "module-info.class");
boolean hasTestModuleDescriptor = false;
// 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())) {
hasTestModuleDescriptor = true;
break;
}
}
if (release != null) {
if (Integer.valueOf(release) < 9) {
pathElements = Collections.emptyMap();
modulepathElements = Collections.emptyList();
classpathElements = testPath;
return;
}
} else if (Double.valueOf(getTarget()) < Double.valueOf(MODULE_INFO_TARGET)) {
pathElements = Collections.emptyMap();
modulepathElements = Collections.emptyList();
classpathElements = testPath;
return;
}
if (hasTestModuleDescriptor) {
modulepathElements = testPath;
classpathElements = Collections.emptyList();
if (mainModuleDescriptor.exists()) {
// maybe some extra analysis required
} else {
// 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.exists()) {
ResolvePathsResult<String> result;
try {
ResolvePathsRequest<String> request = ResolvePathsRequest.withStrings(testPath).setMainModuleDescriptor(mainModuleDescriptor.getAbsolutePath());
Toolchain toolchain = getToolchain();
if (toolchain != null && toolchain instanceof DefaultJavaToolChain) {
request.setJdkHome(((DefaultJavaToolChain) toolchain).getJavaHome());
}
result = locationManager.resolvePaths(request);
} catch (IOException e) {
throw new RuntimeException(e);
}
JavaModuleDescriptor moduleDescriptor = result.getMainModuleDescriptor();
pathElements = new LinkedHashMap<String, JavaModuleDescriptor>(result.getPathElements().size());
pathElements.putAll(result.getPathElements());
modulepathElements = result.getModulepathElements().keySet();
classpathElements = result.getClasspathElements();
if (compilerArgs == null) {
compilerArgs = new ArrayList<String>();
}
compilerArgs.add("--patch-module");
StringBuilder patchModuleValue = new StringBuilder(moduleDescriptor.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(moduleDescriptor.name() + "=ALL-UNNAMED");
} else {
modulepathElements = Collections.emptyList();
classpathElements = testPath;
}
}
}
Aggregations