Search in sources :

Example 1 with JavaModuleDescriptor

use of org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor in project maven-plugins by apache.

the class AbstractCompilerMojo method execute.

@Override
public void execute() throws MojoExecutionException, CompilationFailureException {
    // ----------------------------------------------------------------------
    // Look up the compiler. This is done before other code than can
    // cause the mojo to return before the lookup is done possibly resulting
    // in misconfigured POMs still building.
    // ----------------------------------------------------------------------
    Compiler compiler;
    getLog().debug("Using compiler '" + compilerId + "'.");
    try {
        compiler = compilerManager.getCompiler(compilerId);
    } catch (NoSuchCompilerException e) {
        throw new MojoExecutionException("No such compiler '" + e.getCompilerId() + "'.");
    }
    // -----------toolchains start here ----------------------------------
    // use the compilerId as identifier for toolchains as well.
    Toolchain tc = getToolchain();
    if (tc != null) {
        getLog().info("Toolchain in maven-compiler-plugin: " + tc);
        if (executable != null) {
            getLog().warn("Toolchains are ignored, 'executable' parameter is set to " + executable);
        } else {
            fork = true;
            // TODO somehow shaky dependency between compilerId and tool executable.
            executable = tc.findTool(compilerId);
        }
    }
    // ----------------------------------------------------------------------
    // 
    // ----------------------------------------------------------------------
    List<String> compileSourceRoots = removeEmptyCompileSourceRoots(getCompileSourceRoots());
    if (compileSourceRoots.isEmpty()) {
        getLog().info("No sources to compile");
        return;
    }
    // ----------------------------------------------------------------------
    // Create the compiler configuration
    // ----------------------------------------------------------------------
    CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    compilerConfiguration.setOutputLocation(getOutputDirectory().getAbsolutePath());
    compilerConfiguration.setOptimize(optimize);
    compilerConfiguration.setDebug(debug);
    if (debug && StringUtils.isNotEmpty(debuglevel)) {
        String[] split = StringUtils.split(debuglevel, ",");
        for (String aSplit : split) {
            if (!(aSplit.equalsIgnoreCase("none") || aSplit.equalsIgnoreCase("lines") || aSplit.equalsIgnoreCase("vars") || aSplit.equalsIgnoreCase("source"))) {
                throw new IllegalArgumentException("The specified debug level: '" + aSplit + "' is unsupported. " + "Legal values are 'none', 'lines', 'vars', and 'source'.");
            }
        }
        compilerConfiguration.setDebugLevel(debuglevel);
    }
    compilerConfiguration.setParameters(parameters);
    compilerConfiguration.setVerbose(verbose);
    compilerConfiguration.setShowWarnings(showWarnings);
    compilerConfiguration.setFailOnWarning(failOnWarning);
    compilerConfiguration.setShowDeprecation(showDeprecation);
    compilerConfiguration.setSourceVersion(getSource());
    compilerConfiguration.setTargetVersion(getTarget());
    compilerConfiguration.setReleaseVersion(getRelease());
    compilerConfiguration.setProc(proc);
    File generatedSourcesDirectory = getGeneratedSourcesDirectory();
    compilerConfiguration.setGeneratedSourcesDirectory(generatedSourcesDirectory != null ? generatedSourcesDirectory.getAbsoluteFile() : null);
    if (generatedSourcesDirectory != null) {
        String generatedSourcesPath = generatedSourcesDirectory.getAbsolutePath();
        compileSourceRoots.add(generatedSourcesPath);
        if (isTestCompile()) {
            getLog().debug("Adding " + generatedSourcesPath + " to test-compile source roots:\n  " + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n  "));
            project.addTestCompileSourceRoot(generatedSourcesPath);
            getLog().debug("New test-compile source roots:\n  " + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n  "));
        } else {
            getLog().debug("Adding " + generatedSourcesPath + " to compile source roots:\n  " + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n  "));
            project.addCompileSourceRoot(generatedSourcesPath);
            getLog().debug("New compile source roots:\n  " + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n  "));
        }
    }
    compilerConfiguration.setSourceLocations(compileSourceRoots);
    compilerConfiguration.setAnnotationProcessors(annotationProcessors);
    compilerConfiguration.setProcessorPathEntries(resolveProcessorPathEntries());
    compilerConfiguration.setSourceEncoding(encoding);
    compilerConfiguration.setFork(fork);
    if (fork) {
        if (!StringUtils.isEmpty(meminitial)) {
            String value = getMemoryValue(meminitial);
            if (value != null) {
                compilerConfiguration.setMeminitial(value);
            } else {
                getLog().info("Invalid value for meminitial '" + meminitial + "'. Ignoring this option.");
            }
        }
        if (!StringUtils.isEmpty(maxmem)) {
            String value = getMemoryValue(maxmem);
            if (value != null) {
                compilerConfiguration.setMaxmem(value);
            } else {
                getLog().info("Invalid value for maxmem '" + maxmem + "'. Ignoring this option.");
            }
        }
    }
    compilerConfiguration.setExecutable(executable);
    compilerConfiguration.setWorkingDirectory(basedir);
    compilerConfiguration.setCompilerVersion(compilerVersion);
    compilerConfiguration.setBuildDirectory(buildDirectory);
    compilerConfiguration.setOutputFileName(outputFileName);
    if (CompilerConfiguration.CompilerReuseStrategy.AlwaysNew.getStrategy().equals(this.compilerReuseStrategy)) {
        compilerConfiguration.setCompilerReuseStrategy(CompilerConfiguration.CompilerReuseStrategy.AlwaysNew);
    } else if (CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().equals(this.compilerReuseStrategy)) {
        if (getRequestThreadCount() > 1) {
            if (!skipMultiThreadWarning) {
                getLog().warn("You are in a multi-thread build and compilerReuseStrategy is set to reuseSame." + " This can cause issues in some environments (os/jdk)!" + " Consider using reuseCreated strategy." + System.getProperty("line.separator") + "If your env is fine with reuseSame, you can skip this warning with the " + "configuration field skipMultiThreadWarning " + "or -Dmaven.compiler.skipMultiThreadWarning=true");
            }
        }
        compilerConfiguration.setCompilerReuseStrategy(CompilerConfiguration.CompilerReuseStrategy.ReuseSame);
    } else {
        compilerConfiguration.setCompilerReuseStrategy(CompilerConfiguration.CompilerReuseStrategy.ReuseCreated);
    }
    getLog().debug("CompilerReuseStrategy: " + compilerConfiguration.getCompilerReuseStrategy().getStrategy());
    compilerConfiguration.setForceJavacCompilerUse(forceJavacCompilerUse);
    boolean canUpdateTarget;
    IncrementalBuildHelper incrementalBuildHelper = new IncrementalBuildHelper(mojoExecution, session);
    Set<File> sources;
    IncrementalBuildHelperRequest incrementalBuildHelperRequest = null;
    if (useIncrementalCompilation) {
        getLog().debug("useIncrementalCompilation enabled");
        try {
            canUpdateTarget = compiler.canUpdateTarget(compilerConfiguration);
            sources = getCompileSources(compiler, compilerConfiguration);
            preparePaths(sources);
            incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles(sources);
            // CHECKSTYLE_OFF: LineLength
            if ((compiler.getCompilerOutputStyle().equals(CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES) && !canUpdateTarget) || isDependencyChanged() || isSourceChanged(compilerConfiguration, compiler) || incrementalBuildHelper.inputFileTreeChanged(incrementalBuildHelperRequest)) // CHECKSTYLE_ON: LineLength
            {
                getLog().info("Changes detected - recompiling the module!");
                compilerConfiguration.setSourceFiles(sources);
            } else {
                getLog().info("Nothing to compile - all classes are up to date");
                return;
            }
        } catch (CompilerException e) {
            throw new MojoExecutionException("Error while computing stale sources.", e);
        }
    } else {
        getLog().debug("useIncrementalCompilation disabled");
        Set<File> staleSources;
        try {
            staleSources = computeStaleSources(compilerConfiguration, compiler, getSourceInclusionScanner(staleMillis));
            canUpdateTarget = compiler.canUpdateTarget(compilerConfiguration);
            if (compiler.getCompilerOutputStyle().equals(CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES) && !canUpdateTarget) {
                getLog().info("RESCANNING!");
                // TODO: This second scan for source files is sub-optimal
                String inputFileEnding = compiler.getInputFileEnding(compilerConfiguration);
                sources = computeStaleSources(compilerConfiguration, compiler, getSourceInclusionScanner(inputFileEnding));
                compilerConfiguration.setSourceFiles(sources);
            } else {
                compilerConfiguration.setSourceFiles(staleSources);
            }
            preparePaths(compilerConfiguration.getSourceFiles());
        } catch (CompilerException e) {
            throw new MojoExecutionException("Error while computing stale sources.", e);
        }
        if (staleSources.isEmpty()) {
            getLog().info("Nothing to compile - all classes are up to date");
            return;
        }
    }
    // Dividing pathElements of classPath and modulePath is based on sourceFiles
    compilerConfiguration.setClasspathEntries(getClasspathElements());
    compilerConfiguration.setModulepathEntries(getModulepathElements());
    Map<String, String> effectiveCompilerArguments = getCompilerArguments();
    String effectiveCompilerArgument = getCompilerArgument();
    if ((effectiveCompilerArguments != null) || (effectiveCompilerArgument != null) || (compilerArgs != null)) {
        if (effectiveCompilerArguments != null) {
            for (Map.Entry<String, String> me : effectiveCompilerArguments.entrySet()) {
                String key = me.getKey();
                String value = me.getValue();
                if (!key.startsWith("-")) {
                    key = "-" + key;
                }
                if (key.startsWith("-A") && StringUtils.isNotEmpty(value)) {
                    compilerConfiguration.addCompilerCustomArgument(key + "=" + value, null);
                } else {
                    compilerConfiguration.addCompilerCustomArgument(key, value);
                }
            }
        }
        if (!StringUtils.isEmpty(effectiveCompilerArgument)) {
            compilerConfiguration.addCompilerCustomArgument(effectiveCompilerArgument, null);
        }
        if (compilerArgs != null) {
            for (String arg : compilerArgs) {
                compilerConfiguration.addCompilerCustomArgument(arg, null);
            }
        }
    }
    // ----------------------------------------------------------------------
    if (getLog().isDebugEnabled()) {
        getLog().debug("Classpath:");
        for (String s : getClasspathElements()) {
            getLog().debug(" " + s);
        }
        if (!getModulepathElements().isEmpty()) {
            getLog().debug("Modulepath:");
            for (String s : getModulepathElements()) {
                getLog().debug(" " + s);
            }
        }
        getLog().debug("Source roots:");
        for (String root : getCompileSourceRoots()) {
            getLog().debug(" " + root);
        }
        try {
            if (fork) {
                if (compilerConfiguration.getExecutable() != null) {
                    getLog().debug("Excutable: ");
                    getLog().debug(" " + compilerConfiguration.getExecutable());
                }
            }
            String[] cl = compiler.createCommandLine(compilerConfiguration);
            if (getLog().isDebugEnabled() && cl != null && cl.length > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append(cl[0]);
                for (int i = 1; i < cl.length; i++) {
                    sb.append(" ");
                    sb.append(cl[i]);
                }
                getLog().debug("Command line options:");
                getLog().debug(sb);
            }
        } catch (CompilerException ce) {
            getLog().debug(ce);
        }
    }
    List<String> jpmsLines = new ArrayList<String>();
    // See http://openjdk.java.net/jeps/261
    final List<String> runtimeArgs = Arrays.asList("--upgrade-module-path", "--add-exports", "--add-reads", "--add-modules", "--limit-modules");
    // Custom arguments are all added as keys to an ordered Map
    Iterator<Map.Entry<String, String>> entryIter = compilerConfiguration.getCustomCompilerArgumentsEntries().iterator();
    while (entryIter.hasNext()) {
        Map.Entry<String, String> entry = entryIter.next();
        if (runtimeArgs.contains(entry.getKey())) {
            jpmsLines.add(entry.getKey());
            String value = entry.getValue();
            if (value == null) {
                entry = entryIter.next();
                value = entry.getKey();
            }
            jpmsLines.add(value);
        } else if ("--patch-module".equals(entry.getKey())) {
            jpmsLines.add("--patch-module");
            String value = entry.getValue();
            if (value == null) {
                entry = entryIter.next();
                value = entry.getKey();
            }
            String[] values = value.split("=");
            StringBuilder patchModule = new StringBuilder(values[0]);
            patchModule.append('=');
            Set<String> patchModules = new LinkedHashSet<>();
            Set<Path> sourceRoots = new HashSet<>(getCompileSourceRoots().size());
            for (String sourceRoot : getCompileSourceRoots()) {
                sourceRoots.add(Paths.get(sourceRoot));
            }
            String[] files = values[1].split(PS);
            for (String file : files) {
                Path filePath = Paths.get(file);
                if (getOutputDirectory().toPath().equals(filePath)) {
                    // this jar
                    patchModules.add("_");
                } else if (sourceRoots.contains(filePath)) {
                    // this jar
                    patchModules.add("_");
                } else {
                    JavaModuleDescriptor descriptor = getPathElements().get(file);
                    if (descriptor == null) {
                        getLog().warn("Can't locate " + file);
                    } else if (!values[0].equals(descriptor.name())) {
                        patchModules.add(descriptor.name());
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for (String mod : patchModules) {
                if (sb.length() > 0) {
                    sb.append(", ");
                }
                // use 'invalid' separator to ensure values are transformed
                sb.append(mod);
            }
            jpmsLines.add(patchModule + sb.toString());
        }
    }
    if (!jpmsLines.isEmpty()) {
        Path jpmsArgs = Paths.get(getOutputDirectory().getAbsolutePath(), "META-INF/jpms.args");
        try {
            Files.createDirectories(jpmsArgs.getParent());
            Files.write(jpmsArgs, jpmsLines, Charset.defaultCharset());
        } catch (IOException e) {
            getLog().warn(e.getMessage());
        }
    }
    if (StringUtils.isEmpty(compilerConfiguration.getSourceEncoding())) {
        getLog().warn("File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING + ", i.e. build is platform dependent!");
    }
    CompilerResult compilerResult;
    if (useIncrementalCompilation) {
        incrementalBuildHelperRequest.outputDirectory(getOutputDirectory());
        incrementalBuildHelper.beforeRebuildExecution(incrementalBuildHelperRequest);
        getLog().debug("incrementalBuildHelper#beforeRebuildExecution");
    }
    try {
        try {
            compilerResult = compiler.performCompile(compilerConfiguration);
        } catch (CompilerNotImplementedException cnie) {
            List<CompilerError> messages = compiler.compile(compilerConfiguration);
            compilerResult = convertToCompilerResult(messages);
        }
    } catch (Exception e) {
        // TODO: don't catch Exception
        throw new MojoExecutionException("Fatal error compiling", e);
    }
    if (useIncrementalCompilation) {
        if (incrementalBuildHelperRequest.getOutputDirectory().exists()) {
            getLog().debug("incrementalBuildHelper#afterRebuildExecution");
            // now scan the same directory again and create a diff
            incrementalBuildHelper.afterRebuildExecution(incrementalBuildHelperRequest);
        } else {
            getLog().debug("skip incrementalBuildHelper#afterRebuildExecution as the output directory doesn't exist");
        }
    }
    List<CompilerMessage> warnings = new ArrayList<CompilerMessage>();
    List<CompilerMessage> errors = new ArrayList<CompilerMessage>();
    List<CompilerMessage> others = new ArrayList<CompilerMessage>();
    for (CompilerMessage message : compilerResult.getCompilerMessages()) {
        if (message.getKind() == CompilerMessage.Kind.ERROR) {
            errors.add(message);
        } else if (message.getKind() == CompilerMessage.Kind.WARNING || message.getKind() == CompilerMessage.Kind.MANDATORY_WARNING) {
            warnings.add(message);
        } else {
            others.add(message);
        }
    }
    if (failOnError && !compilerResult.isSuccess()) {
        for (CompilerMessage message : others) {
            assert message.getKind() != CompilerMessage.Kind.ERROR && message.getKind() != CompilerMessage.Kind.WARNING && message.getKind() != CompilerMessage.Kind.MANDATORY_WARNING;
            getLog().info(message.toString());
        }
        if (!warnings.isEmpty()) {
            getLog().info("-------------------------------------------------------------");
            getLog().warn("COMPILATION WARNING : ");
            getLog().info("-------------------------------------------------------------");
            for (CompilerMessage warning : warnings) {
                getLog().warn(warning.toString());
            }
            getLog().info(warnings.size() + ((warnings.size() > 1) ? " warnings " : " warning"));
            getLog().info("-------------------------------------------------------------");
        }
        if (!errors.isEmpty()) {
            getLog().info("-------------------------------------------------------------");
            getLog().error("COMPILATION ERROR : ");
            getLog().info("-------------------------------------------------------------");
            for (CompilerMessage error : errors) {
                getLog().error(error.toString());
            }
            getLog().info(errors.size() + ((errors.size() > 1) ? " errors " : " error"));
            getLog().info("-------------------------------------------------------------");
        }
        if (!errors.isEmpty()) {
            throw new CompilationFailureException(errors);
        } else {
            throw new CompilationFailureException(warnings);
        }
    } else {
        for (CompilerMessage message : compilerResult.getCompilerMessages()) {
            switch(message.getKind()) {
                case NOTE:
                case OTHER:
                    getLog().info(message.toString());
                    break;
                case ERROR:
                    getLog().error(message.toString());
                    break;
                case MANDATORY_WARNING:
                case WARNING:
                default:
                    getLog().warn(message.toString());
                    break;
            }
        }
    }
}
Also used : JavaModuleDescriptor(org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) CompilerMessage(org.codehaus.plexus.compiler.CompilerMessage) ArrayList(java.util.ArrayList) Toolchain(org.apache.maven.toolchain.Toolchain) IncrementalBuildHelperRequest(org.apache.maven.shared.incremental.IncrementalBuildHelperRequest) CompilerConfiguration(org.codehaus.plexus.compiler.CompilerConfiguration) CompilerNotImplementedException(org.codehaus.plexus.compiler.CompilerNotImplementedException) CompilerException(org.codehaus.plexus.compiler.CompilerException) NoSuchCompilerException(org.codehaus.plexus.compiler.manager.NoSuchCompilerException) List(java.util.List) ArrayList(java.util.ArrayList) NoSuchCompilerException(org.codehaus.plexus.compiler.manager.NoSuchCompilerException) Path(java.nio.file.Path) Compiler(org.codehaus.plexus.compiler.Compiler) IncrementalBuildHelper(org.apache.maven.shared.incremental.IncrementalBuildHelper) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CompilerResult(org.codehaus.plexus.compiler.CompilerResult) IOException(java.io.IOException) CompilerException(org.codehaus.plexus.compiler.CompilerException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompilerNotImplementedException(org.codehaus.plexus.compiler.CompilerNotImplementedException) NoSuchCompilerException(org.codehaus.plexus.compiler.manager.NoSuchCompilerException) IOException(java.io.IOException) InclusionScanException(org.codehaus.plexus.compiler.util.scan.InclusionScanException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) Map(java.util.Map)

Example 2 with JavaModuleDescriptor

use of org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor 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 3 with JavaModuleDescriptor

use of org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor 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 4 with JavaModuleDescriptor

use of org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor in project maven-plugins by apache.

the class JModCreateMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    String jModExecutable;
    try {
        jModExecutable = getJModExecutable();
    } catch (IOException e) {
        throw new MojoFailureException("Unable to find jmod command: " + e.getMessage(), e);
    }
    File jModExecuteableFile = new File(jModExecutable);
    File jModExecutableParent = jModExecuteableFile.getParentFile().getParentFile();
    File jmodsFolderJDK = new File(jModExecutableParent, JMODS);
    getLog().debug("Parent: " + jModExecutableParent.getAbsolutePath());
    getLog().debug("jmodsFolder: " + jmodsFolderJDK.getAbsolutePath());
    preparePaths();
    failIfParametersAreNotInTheirValidValueRanges();
    getLog().info("Toolchain in maven-jmod-plugin: jmod [ " + jModExecutable + " ]");
    // We need to put the resulting x.jmod files into jmods folder otherwise is
    // seemed to be not working.
    // Check why?
    File modsFolder = new File(outputDirectory, "jmods");
    File resultingJModFile = new File(modsFolder, outputFileName + ".jmod");
    deleteOutputIfAlreadyExists(resultingJModFile);
    // create the jmods folder...
    modsFolder.mkdirs();
    this.modulePaths = new ArrayList<>();
    for (Entry<String, JavaModuleDescriptor> item : pathElements.entrySet()) {
        // Isn't there a better solution?
        if (item.getValue() == null) {
            String message = "The given dependency " + item.getKey() + " does not have a module-info.java file. So it can't be linked.";
            getLog().error(message);
            throw new MojoFailureException(message);
        }
        getLog().debug("pathElements Item:" + item.getKey() + " v:" + item.getValue().name());
        getLog().info(" -> module: " + item.getValue().name() + " ( " + item.getKey() + " )");
        // We use the real module name and not the artifact Id...
        this.modulePaths.add(item.getKey());
    }
    // The jmods directory of the JDK
    this.modulePaths.add(jmodsFolderJDK.getAbsolutePath());
    Commandline cmd;
    try {
        cmd = createJModCreateCommandLine(resultingJModFile);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    }
    cmd.setExecutable(jModExecutable);
    executeCommand(cmd, outputDirectory);
    if (projectHasAlreadySetAnArtifact()) {
        throw new MojoExecutionException("You have to use a classifier " + "to attach supplemental artifacts to the project instead of replacing them.");
    }
    getProject().getArtifact().setFile(resultingJModFile);
}
Also used : JavaModuleDescriptor(org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor) Commandline(org.codehaus.plexus.util.cli.Commandline) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) File(java.io.File)

Example 5 with JavaModuleDescriptor

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