从 Java 代码执行 Maven 插件命令

Execute Maven plugin command from Java code

正如标题所暗示的那样,我想知道是否有一种方法可以从 Java 代码中执行这样的 maven 命令...

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

我可以从命令行 运行 这个但是当我尝试 运行 这个使用 Java ProcessBuilder 我得到

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

ProcessBuilder 似乎找不到 maven-dependency-plugin。

这是我的代码片段,请注意 mvn --version 命令有效,但需要插件的命令无效 :(

    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();
        }       
    }

有没有办法告诉 ProcessBuilder 在哪里可以找到 maven 插件?

应该是

mvn dependency:get

reference