Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Invoker: IllegalStateException

Tags:

java

maven

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!

like image 551
falvojr Avatar asked Jan 18 '14 04:01

falvojr


2 Answers

Either set the request.pomFile or request.baseDirectory so the Invoker knows from which directory or file Apache Maven should be executed.

like image 196
Robert Scholte Avatar answered Sep 28 '22 09:09

Robert Scholte


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>
like image 27
Chris Smowton Avatar answered Sep 28 '22 10:09

Chris Smowton