我有一个多模块的Maven项目(https://github.com/veniltonjr/msplearning)。
我的其中一个模块需要以编程方式运行Maven构建中的“clean install”命令,但当我调用执行这些目标时,会出现以下错误:
java.lang.IllegalStateException:
Maven应用程序目录未指定,并且系统属性中未提供${maven.home}。请至少指定其中之一。
在Maven Invoker文档中指出,必须存在 M2_HOME 环境变量。
我已经在我的操作系统中设置了此变量。这不足以使 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!
mvn install
不可以吗? - khmarbaise