Reputation: 21
I have built a Java web application that receives a url from a form and performs TestNG tests against those URLs using a local Maven installation from the command line. My test classes are in src/main/java which is not conventional, but since my tests are run after my build, I moved them there.
When I use the Maven invoker from my servlet in Eclipse's Tomcat, my tests run without issue and provide an outputted result in my web application. The problem that I am having is that when I export my application as a .war file and put it in the webapps folder of my local Tomcat server on my Windows machine, my local Maven installation cannot be found.
HTTP Status 500 - Maven application directory was not specified, and ${maven.home} is not provided in the system properties. Please specify at least on of these.
...
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.
org.apache.maven.shared.invoker.MavenCommandLineBuilder.checkRequiredState(MavenCommandLineBuilder.java:127)
org.apache.maven.shared.invoker.MavenCommandLineBuilder.build(MavenCommandLineBuilder.java:62)
org.apache.maven.shared.invoker.DefaultInvoker.execute(DefaultInvoker.java:100)
com.xxxxx.qa.servlets.RunTestServlet.doPost(RunTestServlet.java:104)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Here is the Maven Invoker code that I am using:
InvocationRequest invocationRequest = new DefaultInvocationRequest();
invocationRequest.setPomFile( new File( "pom.xml" ) );
invocationRequest.setGoals( Collections.singletonList( "test surefire-report:report " + formatTest));
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(System.getenv("MAVEN_HOME")));
InvocationResult result = invoker.execute( invocationRequest);
I have no issues running Maven commands from the command line and have correctly added my Maven directory to my MAVEN_HOME environment variable.
I tried some of the answers offered on this page but did not have any success - Maven Invoker: IllegalStateException
I also tried running a Tomcat setevn.bat batch file in Tomcat-Home/bin:
set "maven.home=C:\XXXXX\XXXXX\apache-maven-3.5.0-bin\apache-maven-3.5.0"
...
set "MAVEN_HOME=C:\XXXXX\XXXXX\apache-maven-3.5.0-bin\apache-maven-3.5.0"
Any ideas?
Upvotes: 1
Views: 2424
Reputation: 3
Try to replace
invoker.setMavenHome(new File(System.getenv("MAVEN_HOME")))
with
invoker.setMavenHome(new File(System.getenv().get("MAVEN_HOME")))
Upvotes: 0
Reputation: 12453
See this answer on how to set Environment variables for java installation (or anything else).
You don't need Maven to run TestNG, see run TestNg programmatically.
Upvotes: 0