Search in sources :

Example 1 with Cli

use of org.commonjava.maven.ext.cli.Cli in project pom-manipulation-ext by release-engineering.

the class TestUtils method runCli.

/**
 * Run pom-manipulation-cli via run() with java params (-D arguments) in workingDir directory.
 *
 * @param args - List of additional command line arguments
 * @param params - Map of String keys and String values representing -D arguments
 * @param workingDir - Working directory in which you want the cli to be run.
 * @return Exit value
 * @throws Exception if an error occurs
 */
public static Integer runCli(List<String> args, Map<String, String> params, String workingDir) throws Exception {
    ArrayList<String> arguments = new ArrayList<>(args);
    Collections.addAll(arguments, toJavaParams(params).split("\\s+"));
    boolean argsContainsFile = false;
    for (String s : args) {
        if (s.startsWith("--file")) {
            argsContainsFile = true;
        }
    }
    if (!argsContainsFile) {
        arguments.add("--log=" + workingDir + File.separator + "build.log");
        arguments.add("--file=" + workingDir + File.separator + "pom.xml");
    }
    logger.info("Invoking CLI with {} ", arguments);
    int result = new Cli().run(arguments.toArray(new String[arguments.size()]));
    // This is a bit of a hack. The CLI, if log-to-file is enabled resets the logging. As we don't fork and run
    // in the same process this means we need to reset it back again. The benefit of not forking is a simpler test
    // harness and it saves time when running the tests.
    final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerContext.reset();
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("[%t] %level %logger{32} - %msg%n");
    ple.setContext(loggerContext);
    ple.start();
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setEncoder(ple);
    consoleAppender.setContext(loggerContext);
    consoleAppender.start();
    root.addAppender(consoleAppender);
    root.setLevel(Level.DEBUG);
    return result;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Cli(org.commonjava.maven.ext.cli.Cli) ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

LoggerContext (ch.qos.logback.classic.LoggerContext)1 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 ArrayList (java.util.ArrayList)1 Cli (org.commonjava.maven.ext.cli.Cli)1 Logger (org.slf4j.Logger)1