12

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!

falvojr
  • 3,060
  • 4
  • 31
  • 54
  • 1
    Why do you need to run such a command? Doesn't a `mvn install` on the root level work? – khmarbaise Jan 18 '14 at 10:30
  • Thanks for the comment @khmarbaise. When I run mvn install on the root level the build is completed successfully. But I need to run this programmatically command because I need to provide the output artifact in a RESTful service. – falvojr Jan 18 '14 at 17:53
  • 1
    @veniltonjr To execute the above code do you need Maven installed on your machine or just a few jar files are enough?? – Lucy Oct 15 '14 at 06:39
  • Hi @Lucy! This solution, specifically, uses the environment variable of MV3_HOME configured on the server. Therefore the Maven installation is required. – falvojr Oct 16 '14 at 18:17

2 Answers2

2

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

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • Thanks for your reply @RobertScholte! But none of the options worked. Do you have any more suggestions for this problem? Perform the method `invoke` on the service [ProductRESTfulServer](https://github.com/veniltonjr/msplearning/blob/master/restful-app/src/main/java/com/msplearning/restful/app/ProductRESTfulServer.java), which is in one of the modules of my parent project ([msplearning](https://github.com/veniltonjr/msplearning)). – falvojr Jan 18 '14 at 18:46
  • I think Karl-Heinz asked the correct question. *What* are you trying to build? Which pom.xml? – Robert Scholte Jan 18 '14 at 21:00
  • I really want to build the pom of parent project: `msplearning/pom.xml` – falvojr Jan 18 '14 at 21:56
  • 1
    Aren't you confusing buildtime with runtime? I don't the any reason why you need to rebuild the project during runtime. I tried to build your project but I'm missing the ANDROID_HOME. A few remarks: your project can only be built with M3.0.x, not with M3.1.x+ (doesn't have to be a problem, though). And during development the version must always and with -SNAPSHOT. – Robert Scholte Jan 18 '14 at 22:54
  • My context will be the following: I have a RESTful service that receives an Id that will be set in a .properties file of my project android-app. The aim of this service is to run a build for the modified .properties to be encapsulated in the resulting APK. Again thanks for the help! – falvojr Jan 19 '14 at 01:07
  • 1
    That means that you need the sources of that project. Either checkout to the sources in TMP_android-app directory (for instance) or include http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#project and unpack it in TMP_android-app. Now set request.baseDir to TMP_android-app and run `verify` (not 'clean install', too much overhead) – Robert Scholte Jan 19 '14 at 11:54
  • Thanks @Robert!!! To solve the problem with the ${maven.home} had to set the MavenHome in Involer object. Tried running the `clean install`, but the maven-clean-plugin could not delete the file androidannotations.log on target folder. Thus the `verify` or `install` worked perfectly. – falvojr Jan 25 '14 at 23:36
1

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>
Chris Smowton
  • 2,410
  • 2
  • 17
  • 15