Search in sources :

Example 1 with ModuleNameSource

use of org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource 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 ModuleNameSource

use of org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource 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)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Toolchain (org.apache.maven.toolchain.Toolchain)2 DefaultJavaToolChain (org.apache.maven.toolchain.java.DefaultJavaToolChain)2 JavaModuleDescriptor (org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor)2 ModuleNameSource (org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource)2