Maven Invoker:IllegalStateException [英] Maven Invoker: IllegalStateException
问题描述
我有一个多模块Maven项目( https://github.com/veniltonjr/msplearning )
I have a multi-module Maven project (https://github.com/veniltonjr/msplearning)
我的一个模块 我需要以编程方式运行Maven构建的命令clean install ,但是当我调用这些目标的执行时发生以下错误:
One of my modules I need run programmatically the command from Maven build "clean install", but when I invoke the execution of these goals the following error occurs:
java.lang.IllegalStateException:
Maven未指定应用程序目录,系统属性中未提供$ {maven.home}。请至少注明以下内容。
在 Maven Invoker 文档据说M2_HOME环境变量必须存在。
In the Maven Invoker documentation is said that M2_HOME environment variable must exist.
已经在我的SO中设置了这个变量。这不足以使方法调用
工作?按照我运行相关方法的代码片段:
Already have this variable set in my SO. This should not be enough to make the method invoke
work? Follows the code snippet where I run the method in question:
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环境变量中:
I had to set the POM and also set the Maven Home, which in my case is in the M3_HOME environment variable:
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!
Thanks @RobertScholte and @khmarbaise!
推荐答案
设置 request.pomFile
或 request.baseDirectory
以便调用者知道应该执行Apache Maven的目录或文件。
Either set the request.pomFile
or request.baseDirectory
so the Invoker knows from which directory or file Apache Maven should be executed.
这篇关于Maven Invoker:IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!