如何以编程方式获取Maven依赖关系树

6mzjoqzu  于 2023-01-12  发布在  Maven
关注(0)|答案(3)|浏览(150)

我想通过阅读pom.xml文件而不连接到远程存储库,以编程方式打印maven依赖关系树(包括传递依赖关系在内的所有依赖关系)。

b4lqfgs4

b4lqfgs41#

不太可能,抱歉。另外,您是否查看了此处的答案和评论?How can you display the Maven dependency tree for the plugins in your project?

gzszwxb4

gzszwxb43#

您可以通过使用ProcessBuilder检索maven的dependency:tree命令的结果来实现这一点。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public static void main(String[] args) {

    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("cmd.exe", "/c", "mvn -f \"C:\\myprojectpath\"", "dependency:tree");

    try {

        Process process = processBuilder.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 (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

了解更多here .

相关问题