1

我正在使用 Maven Invoker 在 Java 代码中运行一些 Maven 命令。以下是我在编写自己的代码之前阅读的参考资料:

maven-invoker-illegal-state-exception ,如何运行 maven-from-java , Maven Invoker 用法

我的代码是这样的:

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("absolute/path/of/pom.xml"));
request.setGoals(Arrays.asList("clean", "install"));

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File("absolute/path/of/maven/"));
invoker.setWorkingDirectory(new File("./")); //will fail with or without this line

InvocationResult result = invoker.execute(request);
System.out.println(result.getExitCode());

我得到的ExitCode总是 1,这意味着构建失败。但是,我可以从命令行正确执行所有 Maven 命令。

另外我想通过参考这个答案pom.xml下载使用声明的依赖项。正确包含在.request.setGoals(Arrays.asList("dependency:copy-dependencies", "-DoutputDirectory=OUTPUT_DIR"));maven-dependency_pluginpom.xml

是因为我正在编写一个多模块项目,而这一切都在一个模块下,而不是从根目录?如果是这样,正确的方法是什么?非常感谢。


更新

最后我参考了这个页面并改为使用ProcessBuilder它并且有效。

但我仍然不明白为什么Invoker对我来说失败了。任何答案将不胜感激。

4

0 回答 0