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.