1

I'm using Maven Invoker to run some maven command in Java code. Here are the references I read before doing my own code:

maven-invoker-illegal-state-exception, how-to-run-maven-from-java, Maven Invoker Usage

And my code is like:

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("absolute/path/of/pom.xml"));
request.setGoals(Arrays.asList("clean", "install"));

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File("absolute/path/of/maven/"));
invoker.setWorkingDirectory(new File("./")); //will fail with or without this line

InvocationResult result = invoker.execute(request);
System.out.println(result.getExitCode());

The ExitCode I get is always 1, which means there is a build fail. However, I can correctly execute all Maven command from the command line.

Also I'd like to download the dependencies declared in pom.xml by using request.setGoals(Arrays.asList("dependency:copy-dependencies", "-DoutputDirectory=OUTPUT_DIR")); by referring to this answer. The maven-dependency_plugin is correctly included in the pom.xml.

Is it because I'm writing a multi-module project and all this is under one module not from the root? If so what is the right way to do this? Thanks a lot.


Update:

Finally I referred to this page and changed to use ProcessBuilder instead and that works.

But still I don't unserstand why the Invoker fails for me. Any answer will be appreciated.

Shush
  • 77
  • 1
  • 16
  • If you have multi module project which is not in root... you should refactor your multi module project...What is the problem here? Furthermore what about using `request.setDebug( true );` to see what's the problem? And furthermore you can set on the `invoker.setErrorHandler( InvocationOutputHandler errorHandler )` ...Apart from that where is the pom.xml you would to run located which is the important part for the workingDirectory? – khmarbaise Aug 21 '17 at 17:22
  • I always have `ERROR: JAVA_HOME is set to an invalid directory. JAVA_HOME = "C:\Java\jdk1.8_77\\bin" Please set the JAVA_HOME variable in your environment to match the location of your Java installation` but this path is the installation path. The pom is under `module/pom.xml` and the java file is under `module/src/main/java/RunPom.java`. – Shush Aug 22 '17 at 07:59
  • @khmarbaise thanks for your reply. The objectif is to make sure each module can be compiled and packaged seperately. – Shush Aug 22 '17 at 16:27

0 Answers0