0

As the title suggests I was wondering if there was a way to execute a maven command like this from Java code ...

mvn --dependency:get -Dartifact=groupid.com:artifactId:1.0.0-SNAPSHOT

I can run this from the command line but when I try to run this using the Java ProcessBuilder I get

Unable to parse command line options: Unrecognized option: --dependency:get

It looks like the ProcessBuilder can't find the maven-dependency-plugin.

Here is my code snippet, note that the mvn --version command works but the command that requires the plugin does not :(

    private static void RunCommand() {
        //String command = "mvn --version";
        String command = "mvn --dependency:get -Dartifact=groupid.com:artifactId:1.0.0-SNAPSHOT";
        try {
            boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
            ProcessBuilder builder = new ProcessBuilder();
            builder.redirectErrorStream(true);  
            if (isWindows) {
                builder.command("cmd.exe", "/c", command);
            } else {
                builder.command("sh", "-c", command);
            }
            Process process = builder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            int exitCode = process.waitFor();
            System.out.println("\nExited with error code : " + exitCode);
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }

Is there anyway to tell ProcessBuilder where to find the maven plugin?

vscoder
  • 929
  • 1
  • 10
  • 25
  • maybe you could try MavenCli - check this out https://stackoverflow.com/questions/4206679/can-anyone-give-a-good-example-of-using-org-apache-maven-cli-mavencli-programmat – shikida Jun 30 '21 at 20:53
  • I was looking into that, and other similar questions, but I could not develop a Maven Embedder command that was equivalent to the mvn command I posted in the question. What or how would you structure the cli.doMain args to match the dependency:get? – vscoder Jun 30 '21 at 21:11

1 Answers1

1

It should be

mvn dependency:get

reference

jeanpic
  • 481
  • 3
  • 15
  • Thanks, when I ran the 'command' at the command line I was actually in a GIT Bash command line environment. I tried the command at a DOS prompt and it failed as above. So must be a difference between DOS and BASH. Anyways, removing the '--' gets me to the next error, not resolving the remote repository correctly, but that will be a another topic. – vscoder Jun 30 '21 at 20:56