Search in sources :

Example 6 with JavaModuleDescriptor

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;
        }
    }
}
Also used : JavaModuleDescriptor(org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor) IOException(java.io.IOException) Toolchain(org.apache.maven.toolchain.Toolchain) DefaultJavaToolChain(org.apache.maven.toolchain.java.DefaultJavaToolChain) File(java.io.File)

Aggregations

File (java.io.File)6 IOException (java.io.IOException)6 JavaModuleDescriptor (org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor)6 Toolchain (org.apache.maven.toolchain.Toolchain)5 Map (java.util.Map)4 DefaultJavaToolChain (org.apache.maven.toolchain.java.DefaultJavaToolChain)4 LinkedHashMap (java.util.LinkedHashMap)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 ModuleNameSource (org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1 IncrementalBuildHelper (org.apache.maven.shared.incremental.IncrementalBuildHelper)1 IncrementalBuildHelperRequest (org.apache.maven.shared.incremental.IncrementalBuildHelperRequest)1