Example usage for org.apache.maven.execution MavenExecutionRequest setUserToolchainsFile

List of usage examples for org.apache.maven.execution MavenExecutionRequest setUserToolchainsFile

Introduction

In this page you can find the example usage for org.apache.maven.execution MavenExecutionRequest setUserToolchainsFile.

Prototype

MavenExecutionRequest setUserToolchainsFile(File userToolchainsFile);

Source Link

Usage

From source file:org.hudsonci.maven.eventspy_30.handler.MavenExecutionRequestHandler.java

License:Open Source License

private void configureToolChains(final MavenExecutionRequest event) throws IOException {
    // If there is a toolchains document, then write its content to file and configure the request to use it
    DocumentReference document = getCallback().getToolChainsDocument();
    if (document == null)
        return;//from w  w w . j a v a 2  s  . c om

    if (event.getUserToolchainsFile() != DEFAULT_USER_TOOLCHAINS_FILE) {
        log.warn(
                "Custom tool-chains file configured via command-line as well as via document; document taking precedence");
    }

    log.info("Using tool-chains document ID: {}", document.getId());
    log.trace("Content:\n{}", document.getContent()); // FIXME: May contain sensitive data?

    File file = new File(getCallback().getMavenContextDirectory(), "toolchains.xml");
    File dir = file.getParentFile();
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            log.warn("Failed to create directory structure for: {}", file);
        }
    }

    // Document should not really contain sensitive details, so just leave it around
    Writer writer = new BufferedWriter(new FileWriter(file));
    try {
        writer.write(document.getContent());
    } finally {
        writer.close();
    }

    log.debug("Wrote toolchains.xml: {}", file);
    event.setUserToolchainsFile(file);
}

From source file:org.sonatype.maven.shell.commands.maven.MavenCommand.java

License:Open Source License

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();//from   w  w w  . j  a  v  a  2s .c om
    Variables vars = context.getVariables();

    if (version) {
        io.println(maven.getVersion());
        return Result.SUCCESS;
    }

    // HACK: support --encrypt-master-password and --encrypt-password
    if (encryptMasterPassword != null || encryptPassword != null) {
        return doEncryptPassword(context);
    }

    System.setProperty(MavenSystem.MAVEN_HOME, vars.get(SHELL_HOME, File.class).getAbsolutePath());

    MavenRuntimeConfiguration config = new MavenRuntimeConfiguration();

    config.setBaseDirectory(vars.get(SHELL_USER_DIR, File.class));

    if (file != null) {
        config.setPomFile(file);
    }
    if (profiles != null) {
        config.getProfiles().addAll(profiles);
    }
    if (quiet != null) {
        config.setQuiet(quiet);
    }
    if (debug != null) {
        config.setDebug(debug);
    }
    if (showVersion != null) {
        config.setShowVersion(showVersion);
    }
    if (props != null) {
        config.getProperties().putAll(props);
    }
    if (settingsFile != null) {
        config.setSettingsFile(settingsFile);
    }
    if (globalSettingsFile != null) {
        config.setGlobalSettingsFile(globalSettingsFile);
    }
    if (logFile != null) {
        config.setLogFile(logFile);
    }

    customize(config);

    MavenRuntime runtime = maven.create(config);
    MavenExecutionRequest request = runtime.create();

    if (offline != null) {
        request.setOffline(offline);
    }
    if (goals != null) {
        request.setGoals(goals);
    }
    if (batch != null) {
        request.setInteractiveMode(!batch);
    }
    if (resumeFrom != null) {
        request.setResumeFrom(resumeFrom);
    }
    if (toolChainsFile != null) {
        request.setUserToolchainsFile(toolChainsFile);
    }
    if (showErrors != null) {
        request.setShowErrors(showErrors);
    }
    if (nonRecursive != null) {
        request.setRecursive(!nonRecursive);
    }
    if (updateSnapshots != null) {
        request.setUpdateSnapshots(updateSnapshots);
    }
    if (noSnapshotUpdates != null) {
        request.setNoSnapshotUpdates(noSnapshotUpdates);
    }
    if (selectedProjects != null) {
        request.setSelectedProjects(selectedProjects);
    }

    if (strictChecksums) {
        request.setGlobalChecksumPolicy(CHECKSUM_POLICY_FAIL);
    }
    if (laxChecksums) {
        request.setGlobalChecksumPolicy(CHECKSUM_POLICY_WARN);
    }

    if (failFast) {
        request.setReactorFailureBehavior(REACTOR_FAIL_FAST);
    } else if (failAtEnd) {
        request.setReactorFailureBehavior(REACTOR_FAIL_AT_END);
    } else if (failNever) {
        request.setReactorFailureBehavior(REACTOR_FAIL_NEVER);
    }

    if (alsoMake && !alsoMakeDependents) {
        request.setMakeBehavior(REACTOR_MAKE_UPSTREAM);
    } else if (!alsoMake && alsoMakeDependents) {
        request.setMakeBehavior(REACTOR_MAKE_DOWNSTREAM);
    } else if (alsoMake && alsoMakeDependents) {
        request.setMakeBehavior(REACTOR_MAKE_BOTH);
    }

    // Customize the plugin groups
    request.addPluginGroup("org.apache.maven.plugins");
    request.addPluginGroup("org.codehaus.mojo");
    request.addPluginGroup("com.sonatype.maven.plugins");
    request.addPluginGroup("org.sonatype.maven.plugins");

    // Setup output colorization
    StreamSet current = StreamJack.current();
    StreamSet streams;
    if (color == null || color) {
        // Complain if the user asked for color and its not supported
        if (color != null && !io.getTerminal().isAnsiSupported()) {
            log.warn("ANSI color is not supported by the current terminal");
        }
        streams = new StreamSet(current.in, new ColorizingStream(current.out),
                new ColorizingStream(current.err));
    } else {
        streams = current;
    }
    config.setStreams(streams);

    StreamJack.register(streams);

    // Execute Maven
    int result = 0;
    try {
        result = runtime.execute(request);
    } finally {
        StreamJack.deregister();
        // HACK: Not sure why, but we need to reset the terminal after some mvn builds
        io.getTerminal().reset();
    }

    if (growl) {
        String cl = String.format("%s %s", getName(), Strings.join(context.getArguments(), " "));

        if (result == 0) {
            growler.growl(Notifications.BUILD_PASSED, "BUILD SUCCESS", // TODO: i18n
                    cl);
        } else {
            growler.growl(Notifications.BUILD_FAILED, "BUILD FAILURE", // TODO: i18n
                    cl);
        }
    }

    return result;
}