Java Code Examples for org.apache.maven.shared.invoker.InvocationRequest#setBatchMode()

The following examples show how to use org.apache.maven.shared.invoker.InvocationRequest#setBatchMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CreateProjectMojoIT.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private InvocationResult setup(Properties params)
        throws MavenInvocationException, FileNotFoundException, UnsupportedEncodingException {

    params.setProperty("platformGroupId", ToolsConstants.IO_QUARKUS);
    params.setProperty("platformArtifactId", "quarkus-bom");
    params.setProperty("platformVersion", getPluginVersion());

    InvocationRequest request = new DefaultInvocationRequest();
    request.setBatchMode(true);
    request.setGoals(Collections.singletonList(
            getPluginGroupId() + ":" + getPluginArtifactId() + ":" + getPluginVersion() + ":create"));
    request.setDebug(false);
    request.setShowErrors(false);
    request.setProperties(params);
    getEnv().forEach(request::addShellEnvironment);
    File log = new File(testDir, "build-create-" + testDir.getName() + ".log");
    PrintStreamLogger logger = new PrintStreamLogger(new PrintStream(new FileOutputStream(log), false, "UTF-8"),
            InvokerLogger.DEBUG);
    invoker.setLogger(logger);
    return invoker.execute(request);
}
 
Example 2
Source File: GenerateConfigIT.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void generateConfig(String filename)
        throws MavenInvocationException, FileNotFoundException, UnsupportedEncodingException {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setBatchMode(true);
    request.setGoals(Collections
            .singletonList(getPluginGroupId() + ":" + getPluginArtifactId() + ":"
                    + getPluginVersion() + ":generate-config"));
    Properties properties = new Properties();
    properties.setProperty("file", filename);
    request.setProperties(properties);
    getEnv().forEach(request::addShellEnvironment);
    File log = new File(testDir, "build-generate-config-" + testDir.getName() + ".log");
    PrintStreamLogger logger = new PrintStreamLogger(new PrintStream(new FileOutputStream(log), false, "UTF-8"),
            InvokerLogger.DEBUG);
    invoker.setLogger(logger);
    invoker.execute(request);
}
 
Example 3
Source File: ListExtensionsIT.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private List<String> listExtensions()
        throws MavenInvocationException, IOException {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setBatchMode(true);
    request.setGoals(Collections.singletonList(
            getPluginGroupId() + ":" + getPluginArtifactId() + ":" + getPluginVersion() + ":list-extensions"));
    getEnv().forEach(request::addShellEnvironment);

    File outputLog = new File(testDir, "output.log");
    InvocationOutputHandler outputHandler = new PrintStreamHandler(
            new PrintStream(new TeeOutputStream(System.out, Files.newOutputStream(outputLog.toPath())), true, "UTF-8"),
            true);
    invoker.setOutputHandler(outputHandler);

    File invokerLog = new File(testDir, "invoker.log");
    PrintStreamLogger logger = new PrintStreamLogger(new PrintStream(new FileOutputStream(invokerLog), false, "UTF-8"),
            InvokerLogger.DEBUG);
    invoker.setLogger(logger);

    invoker.execute(request);
    return Files.readAllLines(outputLog.toPath());
}
 
Example 4
Source File: AddExtensionIT.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void addExtension(boolean plural, String ext)
        throws MavenInvocationException, FileNotFoundException, UnsupportedEncodingException {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setBatchMode(true);
    request.setGoals(Collections.singletonList(
            getPluginGroupId() + ":" + getPluginArtifactId() + ":" + getPluginVersion() + ":add-extension"));
    Properties properties = new Properties();
    properties.setProperty("platformGroupId", "io.quarkus");
    properties.setProperty("platformArtifactId", "quarkus-bom");
    properties.setProperty("platformVersion", getPluginVersion());
    if (plural) {
        properties.setProperty("extensions", ext);
    } else {
        properties.setProperty("extension", ext);
    }
    request.setProperties(properties);
    getEnv().forEach(request::addShellEnvironment);
    File log = new File(testDir, "build-add-extension-" + testDir.getName() + ".log");
    PrintStreamLogger logger = new PrintStreamLogger(new PrintStream(new FileOutputStream(log), false, "UTF-8"),
            InvokerLogger.DEBUG);
    invoker.setLogger(logger);
    invoker.execute(request);
}
 
Example 5
Source File: MavenDependencyResolver.java    From carnotzet with Apache License 2.0 6 votes vote down vote up
private void executeMavenBuild(List<String> goals, InvocationOutputHandler outputHandler) {
	log.debug("Invoking maven with goals {}", goals);
	InvocationRequest request = new DefaultInvocationRequest();
	request.setBatchMode(true);
	request.setGoals(goals);
	// reset MAVEN_DEBUG_OPTS to allow debugging without blocking the invoker calls
	request.addShellEnvironment("MAVEN_DEBUG_OPTS", "");
	InvocationOutputHandler outHandler = outputHandler;
	if (outHandler == null) {
		outHandler = log::debug;
	}
	request.setOutputHandler(outHandler);
	try {
		InvocationResult result = maven.execute(request);
		if (result.getExitCode() != 0) {
			throw new MavenInvocationException("Maven process exited with non-zero code [" + result.getExitCode() + "]. "
					+ "Retry with debug log level enabled to see the maven invocation logs");
		}
	}
	catch (MavenInvocationException e) {
		throw new CarnotzetDefinitionException("Error invoking mvn " + goals, e);
	}
}
 
Example 6
Source File: RunnableMavenInvoker.java    From repairnator with MIT License 5 votes vote down vote up
@Override
public void run() {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setPomFile(new File(this.mavenHelper.getPomFile()));
    request.setGoals(Arrays.asList(this.mavenHelper.getGoal()));
    Properties props = this.mavenHelper.getProperties();
    if (System.getenv("M2_HOME") == null) {
        // sensible value
        // https://stackoverflow.com/questions/14793015/programmatically-launch-m2e-maven-command
        String mavenHome = RepairnatorConfig.getInstance().getMavenHome();
        System.out.println("M2_HOME not found, using provided input value instead - " + mavenHome);
        System.setProperty("maven.home", mavenHome);
    } else if ( System.getProperty("maven.home") == null ) {
        System.setProperty("maven.home", System.getenv("M2_HOME"));
    }
    request.setProperties(props);
    request.setBatchMode(true);
    request.setShowErrors(true);

    Invoker invoker = new DefaultInvoker();

    if (this.mavenHelper.getErrorHandler() != null) {
        invoker.setErrorHandler(this.mavenHelper.getErrorHandler());
    }
    invoker.setOutputHandler(this.mavenHelper.getOutputHandler());

    try {
        InvocationResult result = invoker.execute(request);
        this.exitCode = result.getExitCode();
    } catch (MavenInvocationException e) {
        this.logger.error("Error while executing goal " + this.mavenHelper.getGoal()
                + " on the following pom file: " + this.mavenHelper.getPomFile()
                + ". Properties: " + this.mavenHelper.getProperties());
        this.logger.error(e.getMessage());
        this.mavenHelper.getInspector().getJobStatus().addStepError(this.mavenHelper.getName(), e.getMessage());
        this.exitCode = MavenHelper.MAVEN_ERROR;
    }
}