Maven Invoker:IllegalStateException

我有一个多模块Maven项目( https://github.com/veniltonjr/msplearning )

我需要的一个模块我需要以编程方式运行来自Maven构建“clean install的命令 ,但是当我调用这些目标的执行时会发生以下错误:

java.lang.IllegalStateException: 未指定Maven应用程序目录,系统属性中未提供$ {maven.home}。 请至少详细说明。

在Maven Invoker文档中说M2_HOME环境变量必须存在。

已经在我的SO中设置了这个变量。 这不足以使方法invoke工作? 按照我运行相关方法的代码段:

 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!

设置request.pomFilerequest.baseDirectory以便Invoker知道应该执行Apache Maven的目录或文件。