2

I would like to build a JUnit integration test that launches a Java process (Spring based) and then makes calls against that process.

If I would call this from the command line I would launch the Java process by calling mvn exec:java -DmainClass=myClass -Dblahblah from the command line in my pom directory

Is there any way to call that exec:main from inside my Java tester class, so that my tester can execute calls against process and validate results?

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
  • possible duplicate of [How to run maven from java?](http://stackoverflow.com/questions/5141788/how-to-run-maven-from-java) – FrobberOfBits Apr 09 '14 at 17:45

2 Answers2

2

Use the Maven invocation API

The code is going to be something similar to the following:

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Collections.singletonList( "install" ) );

Invoker invoker = new DefaultInvoker();
invoker.execute( request );
FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
1

You can use maven-invoker

Here is the what you need

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Collections.singletonList( "exec:java -DmainClass=com.vgrazi.MyClass -Dparam1=value1" ) );

Invoker invoker = new DefaultInvoker();
invoker.execute( request );
Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thanks. Is there any way to send in the -D parameters? – Victor Grazi Apr 09 '14 at 17:43
  • try with http://maven.apache.org/shared-archives/maven-invoker-2.1.1/apidocs/org/apache/maven/shared/invoker/DefaultInvocationRequest.html#addShellEnvironment%28java.lang.String,%20java.lang.String%29 – jmj Apr 09 '14 at 17:45
  • Actually I just added my -D parameters right at the end of the string, and it worked. In other words "exec:java -Dblahblah -Dblahmore". Thanks! – Victor Grazi Apr 09 '14 at 18:13