2014-01-18 63 views
5

我有一个多模块Maven项目(https://github.com/veniltonjr/msplearningMaven的祈求: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!

+1

为什么你需要运行这样的命令?在根目录下不是'mvn install'工作吗? – khmarbaise

+0

感谢您的评论@khmarbaise。当我在根级别运行mvn install时,构建已成功完成。但是我需要运行这个编程命令,因为我需要在RESTful服务中提供输出工件。 – falvojr

+1

@veniltonjr要执行上面的代码,你需要在你的机器上安装Maven还是只需几个jar文件就够了? – Lucy

回答

2

要么设置request.pomFilerequest.baseDirectory,以便Invoker知道应该执行哪个目录或文件Apache Maven。

+0

感谢您的回复@RobertScholte!但没有任何选项奏效。你对这个问题还有什么建议吗?在服务[ProductRESTfulServer]上执行'invoke'方法(https://github.com/veniltonjr/msplearning/blob/master/restful-app/src/main/java/com/msplearning/restful/app/ProductRESTfulServer.java ),这是我的父项目([msplearning](https://github.com/veniltonjr/msplearning))的一个模块。 – falvojr

+0

我认为卡尔海因茨提出了正确的问题。 *你想要建立什么?哪个pom.xml? –

+0

我真的想要构建父项目的pom:'msplearning/pom.xml' – falvojr

相关问题