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?

2
  • maybe you could try MavenCli - check this out stackoverflow.com/questions/4206679/… Commented Jun 30, 2021 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? Commented Jun 30, 2021 at 21:11

1 Answer 1

1

It should be

mvn dependency:get

reference

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.