无法通过MavenCli(maven-embedder)运行Maven任务 [英] Unable to run maven tasks through MavenCli (maven-embedder)
问题描述
我在程序中使用Maven embedder 3.3.3来以编程方式运行maven目标,并且每次运行MavenCli.doMain
方法时都会遇到以下错误:
I am using Maven embedder 3.3.3 in my program to run maven goals programmatically and I get the following error every time I run the MavenCli.doMain
method:
未设置
-Dmaven.multiModuleProjectDirectory
系统属性.检查$M2_HOME
环境变量和mvn
脚本是否匹配.
-Dmaven.multiModuleProjectDirectory
system property is not set. Check$M2_HOME
environment variable andmvn
script match.
推荐答案
由于 Maven 3.3.1 ,名为maven.multiModuleProjectDirectory
的新系统属性.默认情况下,它由
Since Maven 3.3.1, there is a new system property called maven.multiModuleProjectDirectory
. It is set by default to the root of the project (project base directory) by the mvn
(or mvn.bat
) script (so that is why you never experienced such an error before).
因此,通过maven-embedder
运行Maven时,还需要设置此系统属性(请参见
Therefore, when running Maven through maven-embedder
, you also need to set this system property (see source code where the check is made). It needs to be set to the project root.
要设置此系统属性,您可以将呼叫调整为doMain
并添加
To set this system property, you can adjust your call to doMain
and add
"-Dmaven.multiModuleProjectDirectory=" + projectRoot
给定参数.一个例子是
int result = cli.doMain(new String[] { "-Dmaven.multiModuleProjectDirectory=" + projectRoot, "install" }, "/path/to/project", System.out, System.err);
或者,您可以致电:
System.setProperty("maven.multiModuleProjectDirectory", projectRoot);
在调用MavenCli.doMain
方法之前,其中projectRoot
指向您正在构建的项目的根.
before invoking MavenCli.doMain
method, where projectRoot
points to root of the project you are building.
这篇关于无法通过MavenCli(maven-embedder)运行Maven任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!