【发布时间】:2019-11-12 00:57:13
【问题描述】:
我有一个多模块 Maven 项目 (https://github.com/veniltonjr/msplearning)
我的一个模块我需要以编程方式运行来自 Maven build "clean install" 的命令,但是当我调用这些目标的执行时,会发生以下错误:
java.lang.IllegalStateException: 未指定Maven应用程序目录,系统属性中未提供${maven.home}。请至少指定其中一项。
Maven Invoker 文档中说 M2_HOME 环境变量必须存在。
已经在我的 SO 中设置了这个变量。这应该不足以使方法invoke 工作?遵循我运行相关方法的代码 sn-p:
Invoker invoker = new DefaultInvoker();
invoker.setLocalRepositoryDirectory(new File("C:\\git\\msplearning"));
InvocationRequest request = new DefaultInvocationRequest();
request.setGoals(Arrays.asList("clean", "install"));
InvocationResult result = invoker.execute(request); // Exception occours here...
已经,谢谢!
已编辑(解决方案)
我必须设置 POM 并设置 Maven Home,在我的情况下,它位于 M3_HOME 环境变量中:
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("C:\\git\\msplearning\\pom.xml"));
request.setGoals(Collections.singletonList("verify"));
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(System.getenv("M3_HOME")));
InvocationResult result = invoker.execute(request);
感谢@RobertScholte 和@khmarbaise!
【问题讨论】:
-
为什么需要运行这样的命令?根级别的
mvn install不起作用吗? -
感谢@khmarbaise 的评论。当我在根级别运行 mvn install 时,构建成功完成。但我需要以编程方式运行此命令,因为我需要在 RESTful 服务中提供输出工件。
-
@veniltonjr 要执行上述代码,您需要在您的机器上安装 Maven 还是只需几个 jar 文件就足够了??
-
嗨@Lucy!该方案具体使用了服务器上配置的 MV3_HOME 环境变量。因此需要安装 Maven。