我有一个多模块Maven项目(https://github.com/veniltonjr/msplearning)Maven的祈求:IllegalStateException异常
我的一个模块我需要运行程序从Maven的命令打造“干净安装”,但是当我调用执行发生以下错误:
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的家,这在我的情况是在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
感谢您的评论@khmarbaise。当我在根级别运行mvn install时,构建已成功完成。但是我需要运行这个编程命令,因为我需要在RESTful服务中提供输出工件。 – falvojr
@veniltonjr要执行上面的代码,你需要在你的机器上安装Maven还是只需几个jar文件就够了? – Lucy