private Map getModulePathElements()

in src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java [399:468]


    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.ofFiles(dependencyArtifacts);

            Optional<Toolchain> toolchain = getToolchain();
            if (toolchain.isPresent()
                    && toolchain.orElseThrow(NoSuchElementException::new) instanceof DefaultJavaToolChain) {
                Toolchain toolcahin1 = toolchain.orElseThrow(NoSuchElementException::new);
                request.setJdkHome(new File(((DefaultJavaToolChain) toolcahin1).getJavaHome()));
            }

            ResolvePathsResult<File> resolvePathsResult = locationManager.resolvePaths(request);

            for (Map.Entry<File, JavaModuleDescriptor> entry :
                    resolvePathsResult.getPathElements().entrySet()) {
                JavaModuleDescriptor descriptor = entry.getValue();
                if (descriptor == null) {
                    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);
                }

                // Don't warn for automatic modules, let the jlink tool do that
                getLog().debug(" module: " + descriptor.name() + " automatic: " + descriptor.isAutomatic());
                if (modulepathElements.containsKey(descriptor.name())) {
                    getLog().warn("The module name " + descriptor.name() + " does already exists.");
                }
                modulepathElements.put(descriptor.name(), entry.getKey());
            }

            // This part is for the module in target/classes ? (Hacky..)
            // FIXME: Is there a better way to identify that code exists?
            if (outputDirectory.exists()) {
                List<File> singletonList = Collections.singletonList(outputDirectory);

                ResolvePathsRequest<File> singleModuls = ResolvePathsRequest.ofFiles(singletonList);

                ResolvePathsResult<File> resolvePaths = locationManager.resolvePaths(singleModuls);
                for (Entry<File, JavaModuleDescriptor> entry :
                        resolvePaths.getPathElements().entrySet()) {
                    JavaModuleDescriptor descriptor = entry.getValue();
                    if (descriptor == null) {
                        String message = "The given project " + entry.getKey()
                                + " does not contain a module-info.java file. So it can't be linked.";
                        getLog().error(message);
                        throw new MojoFailureException(message);
                    }
                    if (modulepathElements.containsKey(descriptor.name())) {
                        getLog().warn("The module name " + descriptor.name() + " does already exists.");
                    }
                    modulepathElements.put(descriptor.name(), entry.getKey());
                }
            }

        } catch (IOException e) {
            getLog().error(e.getMessage());
            throw new MojoFailureException(e.getMessage());
        }

        return modulepathElements;
    }