vscoder
vscoder

Reputation: 1007

Execute Maven plugin command from Java code

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?

Upvotes: 0

Views: 795

Answers (1)

jeanpic
jeanpic

Reputation: 541

It should be

mvn dependency:get

reference

Upvotes: 1

Related Questions