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;
}
Aggregations