Search in sources :

Example 1 with CLIManager

use of org.apache.maven.cli.CLIManager in project kie-wb-common by kiegroup.

the class AFMavenCli method cli.

protected void cli(AFCliRequest cliRequest) throws Exception {
    // 
    // Parsing errors can happen during the processing of the arguments and we prefer not having to check if
    // the logger is null and construct this so we can use an SLF4J logger everywhere.
    // 
    slf4jLogger = new Slf4jStdoutLogger();
    CLIManager cliManager = new CLIManager();
    List<String> args = new ArrayList<String>();
    try {
        Path configFile = Paths.get(cliRequest.getMultiModuleProjectDirectory(), ".mvn/maven.config");
        if (java.nio.file.Files.isRegularFile(configFile)) {
            for (String arg : Files.toString(configFile.toFile(), Charsets.UTF_8).split("\\s+")) {
                args.add(arg);
            }
            CommandLine config = cliManager.parse(args.toArray(new String[args.size()]));
            List<?> unrecongized = config.getArgList();
            if (!unrecongized.isEmpty()) {
                throw new ParseException("Unrecognized maven.config entries: " + unrecongized);
            }
        }
    } catch (ParseException e) {
        System.err.println("Unable to parse maven.config: " + e.getMessage());
        cliManager.displayHelp(output);
        throw e;
    }
    try {
        args.addAll(0, Arrays.asList(cliRequest.getArgs()));
        cliRequest.setCommandLine(cliManager.parse(args.toArray(new String[args.size()])));
    } catch (ParseException e) {
        System.err.println("Unable to parse command line options: " + e.getMessage());
        cliManager.displayHelp(output);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        cliManager.displayHelp(ps);
        throw e;
    }
    if (cliRequest.getCommandLine().hasOption(CLIManager.HELP)) {
        cliManager.displayHelp(output);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        cliManager.displayHelp(ps);
        throw new ExitException(0);
    }
    if (cliRequest.getCommandLine().hasOption(CLIManager.VERSION)) {
        System.out.println(AFCLIReportingUtils.showVersion());
        throw new ExitException(0);
    }
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) CommandLine(org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) Slf4jStdoutLogger(org.apache.maven.cli.logging.Slf4jStdoutLogger) ParseException(org.apache.commons.cli.ParseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CLIManager(org.apache.maven.cli.CLIManager)

Example 2 with CLIManager

use of org.apache.maven.cli.CLIManager in project kie-wb-common by kiegroup.

the class MavenParameterTest method cliParameters.

@Test
public void cliParameters() {
    String settingsXml = "src/test/settings.xml";
    File file = new File(settingsXml);
    String settingPath = "-s" + file.getAbsolutePath();
    for (int i = 0; i < 50; i++) {
        new Thread(() -> {
            final CLIManager manager = new CLIManager();
            final String[] values = new String[] { "compile", settingPath, "-Dcompilation.ID=eb678741-0b34-409f-903e-addc083ab2aa", "dependency:build-classpath", "-Dmdep.outputFile=module.cpath" };
            try {
                final CommandLine commandLine = manager.parse(values);
                System.out.println(commandLine.getArgList());
                assertThat(commandLine.getArgs().length).isEqualTo(2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) CLIManager(org.apache.maven.cli.CLIManager) File(java.io.File) Test(org.junit.Test)

Example 3 with CLIManager

use of org.apache.maven.cli.CLIManager in project kie-wb-common by kiegroup.

the class ToolConfigTest method testGetTarget.

@Test
public void testGetTarget() throws ParseException {
    final String[] args = { "-t", "/fake/dir" };
    CommandLine cl = new CLIManager().parse(args);
    Path path = new ToolConfig(cl).getTarget();
    assertEquals(2, path.getNameCount());
    assertEquals("fake", path.getName(0).toString());
}
Also used : Path(java.nio.file.Path) CommandLine(org.apache.commons.cli.CommandLine) ToolConfig(org.kie.workbench.common.migration.cli.ToolConfig) CLIManager(org.apache.maven.cli.CLIManager) Test(org.junit.Test)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)3 CLIManager (org.apache.maven.cli.CLIManager)3 Path (java.nio.file.Path)2 ParseException (org.apache.commons.cli.ParseException)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Slf4jStdoutLogger (org.apache.maven.cli.logging.Slf4jStdoutLogger)1 ToolConfig (org.kie.workbench.common.migration.cli.ToolConfig)1