I have a multi-module Maven project (https://github.com/veniltonjr/msplearning)
One of my modules I need run programmatically the command from Maven build "clean install", but when I invoke the execution of these goals the following error occurs:
java.lang.IllegalStateException:
Maven application directory was not specified, and ${maven.home} is not provided in the system properties. Please specify at least on of these.
In the Maven Invoker documentation is said that M2_HOME environment variable must exist.
Already have this variable set in my SO. This should not be enough to make the method invoke
work? Follows the code snippet where I run the method in question:
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...
Already, thanks!
EDITED (The Solution)
I had to set the POM and also set the Maven Home, which in my case is in the M3_HOME environment variable:
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);
Thanks @RobertScholte and @khmarbaise!
Either set the request.pomFile
or request.baseDirectory
so the Invoker knows from which directory or file Apache Maven should be executed.
If you're running from a Maven-Surefire unit test then it's best to request that Surefire passes the maven.home
system property on to its child processes. Per https://maven.apache.org/shared/maven-invoker/usage.html you can do this by adding the following config stanza:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version> <!-- see surefire-page for available versions -->
<configuration>
<systemPropertyVariables>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
...
</build>
...
</project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With