Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

java - Maven Invoker: IllegalStateException

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...