Search in sources :

Example 11 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project maven-plugins by apache.

the class JavadocUtil method invokeMaven.

/**
     * Invoke Maven for the given project file with a list of goals and properties, the output will be in the
     * invokerlog file.
     * <br/>
     * <b>Note</b>: the Maven Home should be defined in the <code>maven.home</code> Java system property or defined in
     * <code>M2_HOME</code> system env variables.
     *
     * @param log a logger could be null.
     * @param localRepositoryDir the localRepository not null.
     * @param projectFile a not null project file.
     * @param goals a not null goals list.
     * @param properties the properties for the goals, could be null.
     * @param invokerLog the log file where the invoker will be written, if null using <code>System.out</code>.
     * @throws MavenInvocationException if any
     * @since 2.6
     */
protected static void invokeMaven(Log log, File localRepositoryDir, File projectFile, List<String> goals, Properties properties, File invokerLog) throws MavenInvocationException {
    if (projectFile == null) {
        throw new IllegalArgumentException("projectFile should be not null.");
    }
    if (!projectFile.isFile()) {
        throw new IllegalArgumentException(projectFile.getAbsolutePath() + " is not a file.");
    }
    if (goals == null || goals.size() == 0) {
        throw new IllegalArgumentException("goals should be not empty.");
    }
    if (localRepositoryDir == null || !localRepositoryDir.isDirectory()) {
        throw new IllegalArgumentException("localRepositoryDir '" + localRepositoryDir + "' should be a directory.");
    }
    String mavenHome = getMavenHome(log);
    if (StringUtils.isEmpty(mavenHome)) {
        String msg = "Could NOT invoke Maven because no Maven Home is defined. You need to have set the M2_HOME " + "system env variable or a maven.home Java system properties.";
        if (log != null) {
            log.error(msg);
        } else {
            System.err.println(msg);
        }
        return;
    }
    Invoker invoker = new DefaultInvoker();
    invoker.setMavenHome(new File(mavenHome));
    invoker.setLocalRepositoryDirectory(localRepositoryDir);
    InvocationRequest request = new DefaultInvocationRequest();
    request.setBaseDirectory(projectFile.getParentFile());
    request.setPomFile(projectFile);
    if (log != null) {
        request.setDebug(log.isDebugEnabled());
    } else {
        request.setDebug(true);
    }
    request.setGoals(goals);
    if (properties != null) {
        request.setProperties(properties);
    }
    File javaHome = getJavaHome(log);
    if (javaHome != null) {
        request.setJavaHome(javaHome);
    }
    if (log != null && log.isDebugEnabled()) {
        log.debug("Invoking Maven for the goals: " + goals + " with " + (properties == null ? "no properties" : "properties=" + properties));
    }
    InvocationResult result = invoke(log, invoker, request, invokerLog, goals, properties, null);
    if (result.getExitCode() != 0) {
        String invokerLogContent = readFile(invokerLog, "UTF-8");
        // see DefaultMaven
        if (invokerLogContent != null && (!invokerLogContent.contains("Scanning for projects...") || invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
            if (log != null) {
                log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS...");
                if (log.isDebugEnabled()) {
                    log.debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS...");
                }
            }
            result = invoke(log, invoker, request, invokerLog, goals, properties, "");
        }
    }
    if (result.getExitCode() != 0) {
        String invokerLogContent = readFile(invokerLog, "UTF-8");
        // see DefaultMaven
        if (invokerLogContent != null && (!invokerLogContent.contains("Scanning for projects...") || invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
            throw new MavenInvocationException(ERROR_INIT_VM);
        }
        throw new MavenInvocationException("Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath());
    }
}
Also used : Invoker(org.apache.maven.shared.invoker.Invoker) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) File(java.io.File) InvocationResult(org.apache.maven.shared.invoker.InvocationResult)

Example 12 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project ddf by codice.

the class OwaspDiffRunner method main.

public static void main(String[] args) throws OwaspDiffRunnerException {
    System.out.println(BEGIN_OWASP_AUDIT);
    try {
        mavenHome = getMavenHome();
        localRepo = getLocalRepo();
        String modulesOfChangedPoms = getModulesOfChangedPoms();
        if (modulesOfChangedPoms.isEmpty()) {
            System.out.println("No changed poms.");
            return;
        }
        InvocationRequest request = new DefaultInvocationRequest();
        request.setPomFile(new File(PROJECT_ROOT + File.separator + "pom.xml"));
        request.setBaseDirectory(new File(PROJECT_ROOT));
        request.setLocalRepositoryDirectory(new File(localRepo));
        request.setGoals(Arrays.asList("dependency-check:check", "--quiet", "-pl", modulesOfChangedPoms, "-Powasp"));
        invoker.setMavenHome(new File(mavenHome));
        System.out.println("-- Maven home: " + mavenHome);
        System.out.println("-- Local Maven repo: " + localRepo);
        InvocationResult mavenBuildResult;
        try {
            mavenBuildResult = invoker.execute(request);
        } catch (MavenInvocationException e) {
            throw new OwaspDiffRunnerException(OwaspDiffRunnerException.UNABLE_TO_RUN_MAVEN + modulesOfChangedPoms, e);
        }
        if (mavenBuildResult.getExitCode() != 0) {
            throw new OwaspDiffRunnerException(OwaspDiffRunnerException.FOUND_VULNERABILITIES);
        }
    } finally {
        System.out.println(END_OWASP_AUDIT);
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) File(java.io.File) InvocationResult(org.apache.maven.shared.invoker.InvocationResult)

Example 13 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project dspot by STAMP-project.

the class MavenAutomaticBuilder method runGoals.

private int runGoals(String pathToRootOfProject, String... goals) {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setGoals(Arrays.asList(goals));
    request.setPomFile(new File(pathToRootOfProject + FILE_SEPARATOR + POM_FILE));
    request.setJavaHome(new File(System.getProperty("java.home")));
    Properties properties = new Properties();
    properties.setProperty("enforcer.skip", "true");
    properties.setProperty("checkstyle.skip", "true");
    properties.setProperty("cobertura.skip", "true");
    properties.setProperty("skipITs", "true");
    properties.setProperty("rat.skip", "true");
    properties.setProperty("license.skip", "true");
    properties.setProperty("findbugs.skip", "true");
    properties.setProperty("gpg.skip", "true");
    request.setProperties(properties);
    Invoker invoker = new DefaultInvoker();
    invoker.setMavenHome(new File(this.mavenHome));
    LOGGER.info(String.format("run maven %s", Arrays.stream(goals).collect(Collectors.joining(" "))));
    if (Main.verbose) {
        invoker.setOutputHandler(System.out::println);
        invoker.setErrorHandler(System.err::println);
    } else {
        invoker.setOutputHandler(null);
        invoker.setErrorHandler(null);
    }
    try {
        return invoker.execute(request).getExitCode();
    } catch (MavenInvocationException e) {
        throw new RuntimeException(e);
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) Invoker(org.apache.maven.shared.invoker.Invoker) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) Properties(java.util.Properties)

Example 14 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project scala-maven-plugin by davidB.

the class ScalaContinuousTestMojo method postCompileActions.

@Override
protected void postCompileActions() throws Exception {
    if (test == null) {
        getLog().info("Now running all the unit tests. Use -Dtest=FooTest to run a single test by name");
    } else {
        getLog().info("Now running tests matching: " + test);
    }
    final InvocationRequest request = new DefaultInvocationRequest();
    request.setLocalRepositoryDirectory(localRepositoryPath);
    request.setInteractive(false);
    request.setErrorHandler(new SystemOutHandler(true));
    request.setOutputHandler(new SystemOutHandler(true));
    request.setBaseDirectory(project.getBasedir());
    request.setPomFile(new File(project.getBasedir(), "pom.xml"));
    request.setGoals(getMavenGoals());
    request.setOffline(false);
    if (test != null) {
        Properties properties = new Properties();
        properties.put("test", test);
        request.setProperties(properties);
    }
    if (getLog().isDebugEnabled()) {
        try {
            getLog().debug("Executing: " + new MavenCommandLineBuilder().build(request));
        } catch (CommandLineConfigurationException e) {
            getLog().debug("Failed to display command line: " + e.getMessage());
        }
    }
    try {
        invoker.execute(request);
    } catch (final MavenInvocationException e) {
        getLog().debug("Error invoking Maven: " + e.getMessage(), e);
        throw new BuildFailureException("Maven invocation failed. " + e.getMessage(), e);
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) BuildFailureException(org.apache.maven.BuildFailureException) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) SystemOutHandler(org.apache.maven.shared.invoker.SystemOutHandler) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) MavenCommandLineBuilder(org.apache.maven.shared.invoker.MavenCommandLineBuilder) CommandLineConfigurationException(org.apache.maven.shared.invoker.CommandLineConfigurationException) Properties(java.util.Properties) File(java.io.File)

Example 15 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project maven-plugins by apache.

the class AbstractJavadocMojo method getModulesLinks.

/**
 * Using Maven, a Javadoc link is given by <code>${project.url}/apidocs</code>.
 *
 * @return the detected Javadoc links using the Maven conventions for all modules defined in the current project
 *         or an empty list.
 * @throws MavenReportException if any
 * @see #detectOfflineLinks
 * @see #reactorProjects
 * @since 2.6
 */
private List<OfflineLink> getModulesLinks() throws MavenReportException {
    if (!detectOfflineLinks || isAggregator() || reactorProjects == null) {
        return Collections.emptyList();
    }
    getLog().debug("Trying to add links for modules...");
    Set<String> dependencyArtifactIds = new HashSet<>();
    final Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
    for (Artifact artifact : dependencyArtifacts) {
        dependencyArtifactIds.add(artifact.getId());
    }
    List<OfflineLink> modulesLinks = new ArrayList<>();
    String javadocDirRelative = PathUtils.toRelative(project.getBasedir(), getOutputDirectory());
    for (MavenProject p : reactorProjects) {
        if (!dependencyArtifactIds.contains(p.getArtifact().getId()) || (p.getUrl() == null)) {
            continue;
        }
        File location = new File(p.getBasedir(), javadocDirRelative);
        if (!location.exists()) {
            if (getLog().isDebugEnabled()) {
                getLog().debug("Javadoc directory not found: " + location);
            }
            String javadocGoal = getFullJavadocGoal();
            getLog().info("The goal '" + javadocGoal + "' has not been previously called for the module: '" + p.getId() + "'. Trying to invoke it...");
            File invokerDir = new File(project.getBuild().getDirectory(), "invoker");
            invokerDir.mkdirs();
            File invokerLogFile = FileUtils.createTempFile("maven-javadoc-plugin", ".txt", invokerDir);
            try {
                JavadocUtil.invokeMaven(getLog(), new File(localRepository.getBasedir()), p.getFile(), Collections.singletonList(javadocGoal), null, invokerLogFile);
            } catch (MavenInvocationException e) {
                logError("MavenInvocationException: " + e.getMessage(), e);
                String invokerLogContent = JavadocUtil.readFile(invokerLogFile, null);
                // the JVM won't start (opposite of what it was).
                if (invokerLogContent != null && invokerLogContent.contains(JavadocUtil.ERROR_INIT_VM)) {
                    throw new MavenReportException(e.getMessage(), e);
                }
            } finally {
                // just create the directory to prevent repeated invocations..
                if (!location.exists()) {
                    getLog().warn("Creating fake javadoc directory to prevent repeated invocations: " + location);
                    location.mkdirs();
                }
            }
        }
        if (location.exists()) {
            String url = getJavadocLink(p);
            OfflineLink ol = new OfflineLink();
            ol.setUrl(url);
            ol.setLocation(location.getAbsolutePath());
            if (getLog().isDebugEnabled()) {
                getLog().debug("Added Javadoc offline link: " + url + " for the module: " + p.getId());
            }
            modulesLinks.add(ol);
        }
    }
    return modulesLinks;
}
Also used : MavenProject(org.apache.maven.project.MavenProject) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) ArrayList(java.util.ArrayList) OfflineLink(org.apache.maven.plugins.javadoc.options.OfflineLink) File(java.io.File) BootclasspathArtifact(org.apache.maven.plugins.javadoc.options.BootclasspathArtifact) JavadocPathArtifact(org.apache.maven.plugins.javadoc.options.JavadocPathArtifact) ResourcesArtifact(org.apache.maven.plugins.javadoc.options.ResourcesArtifact) Artifact(org.apache.maven.artifact.Artifact) TagletArtifact(org.apache.maven.plugins.javadoc.options.TagletArtifact) DocletArtifact(org.apache.maven.plugins.javadoc.options.DocletArtifact) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

MavenInvocationException (org.apache.maven.shared.invoker.MavenInvocationException)18 File (java.io.File)16 DefaultInvocationRequest (org.apache.maven.shared.invoker.DefaultInvocationRequest)15 InvocationRequest (org.apache.maven.shared.invoker.InvocationRequest)15 InvocationResult (org.apache.maven.shared.invoker.InvocationResult)13 DefaultInvoker (org.apache.maven.shared.invoker.DefaultInvoker)11 Invoker (org.apache.maven.shared.invoker.Invoker)11 Properties (java.util.Properties)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Artifact (org.apache.maven.artifact.Artifact)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 MavenProject (org.apache.maven.project.MavenProject)2 MavenReportException (org.apache.maven.reporting.MavenReportException)2 CommandLineConfigurationException (org.apache.maven.shared.invoker.CommandLineConfigurationException)2 MavenCommandLineBuilder (org.apache.maven.shared.invoker.MavenCommandLineBuilder)2 RunFailureException (org.apache.maven.shared.scriptinterpreter.RunFailureException)2