Search in sources :

Example 1 with DefaultJavaToolChain

use of org.apache.maven.toolchain.java.DefaultJavaToolChain in project maven-plugins by apache.

the class CompilerMojo method preparePaths.

@Override
protected void preparePaths(Set<File> sourceFiles) {
    assert compilePath != null;
    File moduleDescriptorPath = null;
    boolean hasModuleDescriptor = false;
    for (File sourceFile : sourceFiles) {
        if ("module-info.java".equals(sourceFile.getName())) {
            moduleDescriptorPath = sourceFile;
            hasModuleDescriptor = true;
            break;
        }
    }
    if (hasModuleDescriptor) {
        // For now only allow named modules. Once we can create a graph with ASM we can specify exactly the modules
        // and we can detect if auto modules are used. In that case, MavenProject.setFile() should not be used, so
        // you cannot depend on this project and so it won't be distributed.
        modulepathElements = new ArrayList<String>(compilePath.size());
        classpathElements = new ArrayList<String>(compilePath.size());
        pathElements = new LinkedHashMap<String, JavaModuleDescriptor>(compilePath.size());
        ResolvePathsResult<File> resolvePathsResult;
        try {
            Collection<File> dependencyArtifacts = getCompileClasspathElements(getProject());
            ResolvePathsRequest<File> request = ResolvePathsRequest.withFiles(dependencyArtifacts).setMainModuleDescriptor(moduleDescriptorPath);
            Toolchain toolchain = getToolchain();
            if (toolchain != null && toolchain instanceof DefaultJavaToolChain) {
                request.setJdkHome(new File(((DefaultJavaToolChain) toolchain).getJavaHome()));
            }
            resolvePathsResult = locationManager.resolvePaths(request);
            JavaModuleDescriptor moduleDescriptor = resolvePathsResult.getMainModuleDescriptor();
            for (Map.Entry<File, ModuleNameSource> entry : resolvePathsResult.getModulepathElements().entrySet()) {
                if (ModuleNameSource.FILENAME.equals(entry.getValue())) {
                    final String message = "Required filename-based automodules detected. " + "Please don't publish this project to a public artifact repository!";
                    if (moduleDescriptor.exports().isEmpty()) {
                        // application
                        getLog().info(message);
                    } else {
                        // library
                        writeBoxedWarning(message);
                    }
                    break;
                }
            }
            for (Map.Entry<File, JavaModuleDescriptor> entry : resolvePathsResult.getPathElements().entrySet()) {
                pathElements.put(entry.getKey().getPath(), entry.getValue());
            }
            for (File file : resolvePathsResult.getClasspathElements()) {
                classpathElements.add(file.getPath());
            }
            for (File file : resolvePathsResult.getModulepathElements().keySet()) {
                modulepathElements.add(file.getPath());
            }
        } catch (IOException e) {
            getLog().warn(e.getMessage());
        }
    } else {
        classpathElements = compilePath;
        modulepathElements = Collections.emptyList();
    }
}
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) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ModuleNameSource(org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource)

Example 2 with DefaultJavaToolChain

use of org.apache.maven.toolchain.java.DefaultJavaToolChain in project maven-plugins by apache.

the class JLinkMojo method getModulePathElements.

private Map<String, File> getModulePathElements() throws MojoFailureException {
    // For now only allow named modules. Once we can create a graph with ASM we can specify exactly the modules
    // and we can detect if auto modules are used. In that case, MavenProject.setFile() should not be used, so
    // you cannot depend on this project and so it won't be distributed.
    Map<String, File> modulepathElements = new HashMap<>();
    try {
        Collection<File> dependencyArtifacts = getCompileClasspathElements(getProject());
        ResolvePathsRequest<File> request = ResolvePathsRequest.withFiles(dependencyArtifacts);
        Toolchain toolchain = getToolchain();
        if (toolchain != null && toolchain instanceof DefaultJavaToolChain) {
            request.setJdkHome(new File(((DefaultJavaToolChain) toolchain).getJavaHome()));
        }
        ResolvePathsResult<File> resolvePathsResult = locationManager.resolvePaths(request);
        for (Map.Entry<File, JavaModuleDescriptor> entry : resolvePathsResult.getPathElements().entrySet()) {
            if (entry.getValue() != null) {
                // Don't warn for automatic modules, let the jlink tool do that
                modulepathElements.put(entry.getValue().name(), entry.getKey());
            } else {
                String message = "The given dependency " + entry.getKey() + " does not have a module-info.java file. So it can't be linked.";
                getLog().error(message);
                throw new MojoFailureException(message);
            }
        }
    } catch (IOException e) {
        getLog().warn(e.getMessage());
    }
    return modulepathElements;
}
Also used : JavaModuleDescriptor(org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor) HashMap(java.util.HashMap) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) Toolchain(org.apache.maven.toolchain.Toolchain) DefaultJavaToolChain(org.apache.maven.toolchain.java.DefaultJavaToolChain) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DefaultJavaToolChain

use of org.apache.maven.toolchain.java.DefaultJavaToolChain in project xtext-xtend by eclipse.

the class AbstractXtendCompilerMojo method getBootClassPath.

private String getBootClassPath() {
    Toolchain toolchain = toolchainManager.getToolchainFromBuildContext("jdk", session);
    if (toolchain instanceof DefaultJavaToolChain) {
        DefaultJavaToolChain javaToolChain = (DefaultJavaToolChain) toolchain;
        getLog().info("Using toolchain " + javaToolChain);
        if (javaSourceVersion != null) {
            JavaVersion version = JavaVersion.fromQualifier(javaSourceVersion);
            if (version.isAtLeast(JavaVersion.JAVA9)) {
                // bootclasspath only supported on Java8 and older
                return "";
            }
        }
        String[] includes = { "jre/lib/*", "jre/lib/ext/*", "jre/lib/endorsed/*" };
        String[] excludes = new String[0];
        Xpp3Dom config = (Xpp3Dom) javaToolChain.getModel().getConfiguration();
        if (config != null) {
            Xpp3Dom bootClassPath = config.getChild("bootClassPath");
            if (bootClassPath != null) {
                Xpp3Dom includeParent = bootClassPath.getChild("includes");
                if (includeParent != null) {
                    includes = getValues(includeParent.getChildren("include"));
                }
                Xpp3Dom excludeParent = bootClassPath.getChild("excludes");
                if (excludeParent != null) {
                    excludes = getValues(excludeParent.getChildren("exclude"));
                }
            }
        }
        return scanBootclasspath(javaToolChain.getJavaHome(), includes, excludes);
    }
    return "";
}
Also used : Toolchain(org.apache.maven.toolchain.Toolchain) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) DefaultJavaToolChain(org.apache.maven.toolchain.java.DefaultJavaToolChain) JavaVersion(org.eclipse.xtext.util.JavaVersion)

Example 4 with DefaultJavaToolChain

use of org.apache.maven.toolchain.java.DefaultJavaToolChain in project maven-plugins by apache.

the class JModCreateMojo method preparePaths.

private void preparePaths() {
    assert compilePath != null;
    boolean hasModuleDescriptor = false;
    // Assuming that the module-info.java is already compiled by compiler plugin so only
    // check if the module-info.class file exists.
    File moduleInfo = new File(targetClassesDirectory, "module-info.class");
    if (moduleInfo.exists() && moduleInfo.isFile()) {
        getLog().debug("We have found a module-info.class file.");
        hasModuleDescriptor = true;
    }
    if (hasModuleDescriptor) {
        // For now only allow named modules. Once we can create a graph with ASM we can specify exactly the modules
        // and we can detect if auto modules are used. In that case, MavenProject.setFile() should not be used, so
        // you cannot depend on this project and so it won't be distributed.
        modulepathElements = new ArrayList<String>();
        classpathElements = new ArrayList<String>();
        pathElements = new LinkedHashMap<String, JavaModuleDescriptor>();
        ResolvePathsResult<File> resolvePathsResult;
        try {
            Collection<File> dependencyArtifacts = getCompileClasspathElements(getProject());
            ResolvePathsRequest<File> request = ResolvePathsRequest.withFiles(dependencyArtifacts);
            Toolchain toolchain = getToolchain();
            if (toolchain != null && toolchain instanceof DefaultJavaToolChain) {
                request.setJdkHome(new File(((DefaultJavaToolChain) toolchain).getJavaHome()));
            }
            resolvePathsResult = locationManager.resolvePaths(request);
            JavaModuleDescriptor moduleDescriptor = resolvePathsResult.getMainModuleDescriptor();
            for (Map.Entry<File, ModuleNameSource> entry : resolvePathsResult.getModulepathElements().entrySet()) {
                getLog().debug("File: " + entry.getKey().getAbsolutePath() + " " + entry.getValue().name());
                if (ModuleNameSource.FILENAME.equals(entry.getValue())) {
                    final String message = "Required filename-based automodules detected. " + "Please don't publish this project to a public artifact repository!";
                    if (moduleDescriptor.exports().isEmpty()) {
                        // application
                        getLog().info(message);
                    } else {
                        // library
                        writeBoxedWarning(message);
                    }
                    break;
                }
            }
            for (Map.Entry<File, JavaModuleDescriptor> entry : resolvePathsResult.getPathElements().entrySet()) {
                getLog().debug("pathElements: " + entry.getKey().getPath() + " " + entry.getValue().name());
                pathElements.put(entry.getKey().getPath(), entry.getValue());
            }
            for (File file : resolvePathsResult.getClasspathElements()) {
                getLog().debug("classpathElements: File: " + file.getPath());
                classpathElements.add(file.getPath());
            }
            for (File file : resolvePathsResult.getModulepathElements().keySet()) {
                getLog().debug("modulepathElements: File: " + file.getPath());
                modulepathElements.add(file.getPath());
            }
        } catch (IOException e) {
            getLog().warn(e.getMessage());
        }
    } else {
        classpathElements = compilePath;
        modulepathElements = Collections.emptyList();
    }
}
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) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ModuleNameSource(org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource)

Example 5 with DefaultJavaToolChain

use of org.apache.maven.toolchain.java.DefaultJavaToolChain 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

DefaultJavaToolChain (org.apache.maven.toolchain.java.DefaultJavaToolChain)6 Toolchain (org.apache.maven.toolchain.Toolchain)5 File (java.io.File)4 IOException (java.io.IOException)4 JavaModuleDescriptor (org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor)4 Map (java.util.Map)3 LinkedHashMap (java.util.LinkedHashMap)2 ModuleNameSource (org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource)2 HashMap (java.util.HashMap)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)1 JavaVersion (org.eclipse.xtext.util.JavaVersion)1