Search in sources :

Example 11 with InvocationResult

use of org.apache.maven.shared.invoker.InvocationResult 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 12 with InvocationResult

use of org.apache.maven.shared.invoker.InvocationResult in project beakerx by twosigma.

the class MavenJarResolver method retrieve.

public AddMvnCommandResult retrieve(List<Dependency> dependencies, MvnLoggerWidget progress) {
    File finalPom = null;
    try {
        String pomAsString = pomFactory.createPom(pathToMavenRepo, dependencies, commandParams.getRepos());
        finalPom = saveToFile(commandParams.getPathToNotebookJars(), pomAsString);
        InvocationRequest request = createInvocationRequest();
        request.setOffline(commandParams.getOffline());
        request.setPomFile(finalPom);
        request.setUpdateSnapshots(true);
        Invoker invoker = getInvoker(progress);
        progress.display();
        InvocationResult invocationResult = invoker.execute(request);
        progress.close();
        return getResult(invocationResult, dependencies);
    } catch (Exception e) {
        return AddMvnCommandResult.error(e.getMessage());
    } finally {
        deletePomFolder(finalPom);
    }
}
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) File(java.io.File) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) IOException(java.io.IOException)

Example 13 with InvocationResult

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

the class SiteInvoker method invoke.

/**
 * @param projectFile not null, should be in the ${project.basedir}
 * @param invokerLog not null
 * @param mavenHome not null
 * @param goals the list of goals
 * @param properties the properties for the invoker
 */
private void invoke(File projectFile, File invokerLog, String mavenHome, List goals, List activeProfiles, Properties properties) {
    Invoker invoker = new DefaultInvoker();
    invoker.setMavenHome(new File(mavenHome));
    File localRepoDir = new File(localRepository.getBasedir());
    invoker.setLocalRepositoryDirectory(localRepoDir);
    InvocationRequest request = new DefaultInvocationRequest();
    request.setLocalRepositoryDirectory(localRepoDir);
    // request.setUserSettingsFile( settingsFile );
    request.setBatchMode(true);
    request.setShowErrors(getLog().isErrorEnabled());
    request.setDebug(getLog().isDebugEnabled());
    // request.setShowVersion( false );
    request.setBaseDirectory(projectFile.getParentFile());
    request.setPomFile(projectFile);
    request.setGoals(goals);
    request.setProperties(properties);
    request.setProfiles(activeProfiles);
    File javaHome = getJavaHome();
    if (javaHome != null) {
        request.setJavaHome(javaHome);
    }
    InvocationResult invocationResult;
    try {
        if (getLog().isDebugEnabled()) {
            getLog().debug("Invoking Maven for the goals: " + goals + " with properties=" + properties);
        }
        invocationResult = invoke(invoker, request, invokerLog, goals, properties, null);
    } catch (MavenInvocationException e) {
        getLog().error("Error when invoking Maven, consult the invoker log.");
        getLog().debug(e);
        return;
    }
    String invokerLogContent = null;
    Reader reader = null;
    try {
        reader = ReaderFactory.newReader(invokerLog, "UTF-8");
        invokerLogContent = IOUtil.toString(reader);
        reader.close();
        reader = null;
    } catch (IOException e) {
        getLog().error("IOException: " + e.getMessage());
        getLog().debug(e);
    } finally {
        IOUtil.close(reader);
    }
    if (invokerLogContent != null && invokerLogContent.contains("Error occurred during initialization of VM")) {
        getLog().info("Error occurred during initialization of VM, try to use an empty MAVEN_OPTS.");
        if (getLog().isDebugEnabled()) {
            getLog().debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS");
        }
        try {
            invocationResult = invoke(invoker, request, invokerLog, goals, properties, "");
        } catch (MavenInvocationException e) {
            getLog().error("Error when reinvoking Maven, consult the invoker log.");
            getLog().debug(e);
            return;
        }
    }
    if (invocationResult.getExitCode() != 0) {
        if (getLog().isErrorEnabled()) {
            getLog().error("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) Reader(java.io.Reader) IOException(java.io.IOException) File(java.io.File) InvocationResult(org.apache.maven.shared.invoker.InvocationResult)

Example 14 with InvocationResult

use of org.apache.maven.shared.invoker.InvocationResult in project maven-archetype by apache.

the class CreateProjectFromArchetypeMojo method invokePostArchetypeGenerationGoals.

private void invokePostArchetypeGenerationGoals(String goals, String artifactId) throws MojoExecutionException, MojoFailureException {
    getLog().info("Invoking post-archetype-generation goals: " + goals);
    File projectBasedir = new File(basedir, artifactId);
    if (projectBasedir.exists()) {
        InvocationRequest request = new DefaultInvocationRequest().setBaseDirectory(projectBasedir).setGoals(Arrays.asList(StringUtils.split(goals, ",")));
        try {
            InvocationResult result = invoker.execute(request);
            if (result.getExitCode() != 0) {
                throw new MojoExecutionException("Failed to invoke goals", result.getExecutionException());
            }
        } catch (MavenInvocationException e) {
            throw new MojoExecutionException("Cannot run additions goals.", e);
        }
    } else {
        getLog().info("Post-archetype-generation goals aborted: unavailable basedir " + projectBasedir);
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) 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 15 with InvocationResult

use of org.apache.maven.shared.invoker.InvocationResult in project maven-archetype by apache.

the class IntegrationTestMojo method invokePostArchetypeGenerationGoals.

private void invokePostArchetypeGenerationGoals(String goals, File basedir, File goalFile) throws IntegrationTestFailure, IOException, MojoExecutionException {
    FileLogger logger = setupLogger(basedir);
    if (!StringUtils.isBlank(goals)) {
        getLog().info("Invoking post-archetype-generation goals: " + goals);
        if (!localRepositoryPath.exists()) {
            localRepositoryPath.mkdirs();
        }
        // @formatter:off
        InvocationRequest request = new DefaultInvocationRequest().setBaseDirectory(basedir).setGoals(Arrays.asList(StringUtils.split(goals, ","))).setLocalRepositoryDirectory(localRepositoryPath).setInteractive(false).setShowErrors(true);
        // @formatter:on
        request.setDebug(debug);
        request.setShowVersion(showVersion);
        if (logger != null) {
            request.setErrorHandler(logger);
            request.setOutputHandler(logger);
        }
        File interpolatedSettingsFile = null;
        if (settingsFile != null) {
            File interpolatedSettingsDirectory = new File(project.getBuild().getOutputDirectory(), "archetype-it");
            if (interpolatedSettingsDirectory.exists()) {
                FileUtils.deleteDirectory(interpolatedSettingsDirectory);
            }
            interpolatedSettingsDirectory.mkdir();
            interpolatedSettingsFile = new File(interpolatedSettingsDirectory, "interpolated-" + settingsFile.getName());
            buildInterpolatedFile(settingsFile, interpolatedSettingsFile);
            request.setUserSettingsFile(interpolatedSettingsFile);
        }
        try {
            InvocationResult result = invoker.execute(request);
            getLog().info("Post-archetype-generation invoker exit code: " + result.getExitCode());
            if (result.getExitCode() != 0) {
                throw new IntegrationTestFailure("Execution failure: exit code = " + result.getExitCode(), result.getExecutionException());
            }
        } catch (MavenInvocationException e) {
            throw new IntegrationTestFailure("Cannot run additions goals.", e);
        }
    } else {
        getLog().info("No post-archetype-generation goals to invoke.");
    }
    // verify result
    ScriptRunner scriptRunner = new ScriptRunner(getLog());
    scriptRunner.setScriptEncoding(encoding);
    Map<String, Object> context = new LinkedHashMap<String, Object>();
    context.put("projectDir", basedir);
    try {
        scriptRunner.run("post-build script", goalFile.getParentFile(), postBuildHookScript, context, logger, "failure post script", true);
    } catch (RunFailureException e) {
        throw new IntegrationTestFailure("post build script failure failure: " + e.getMessage(), e);
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) ScriptRunner(org.apache.maven.shared.scriptinterpreter.ScriptRunner) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) LinkedHashMap(java.util.LinkedHashMap) RunFailureException(org.apache.maven.shared.scriptinterpreter.RunFailureException) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) File(java.io.File)

Aggregations

InvocationResult (org.apache.maven.shared.invoker.InvocationResult)16 DefaultInvocationRequest (org.apache.maven.shared.invoker.DefaultInvocationRequest)15 InvocationRequest (org.apache.maven.shared.invoker.InvocationRequest)15 File (java.io.File)14 MavenInvocationException (org.apache.maven.shared.invoker.MavenInvocationException)13 DefaultInvoker (org.apache.maven.shared.invoker.DefaultInvoker)11 Invoker (org.apache.maven.shared.invoker.Invoker)10 IOException (java.io.IOException)5 Properties (java.util.Properties)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 FileNotFoundException (java.io.FileNotFoundException)2 LinkedHashMap (java.util.LinkedHashMap)2 RunFailureException (org.apache.maven.shared.scriptinterpreter.RunFailureException)2 FileWriter (java.io.FileWriter)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1