1

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?

Omar
  • 21
  • 2
  • Are you trying to implement a maven plugin ? Or are these test a kind of integration tests? And what is the role of Tomcat here? – khmarbaise Sep 07 '17 at 06:07

2 Answers2

0

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.

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
Stefan
  • 12,108
  • 5
  • 47
  • 66
  • Running TestNG programmatically is a great work-around solution and actually works better than invoking Maven from what I can see. I still find it strange that I cannot access the MAVEN_HOME environment variable. As I already noted, I already set up the environment variable for this and have no issues running Maven from the command-line. – Omar Sep 07 '17 at 17:31
  • It seems after some testings, running TestNg programmatically is working for me in Eclipse's Tomcat but does not work on my local Tomcat, so this is not solved for me. – Omar Sep 07 '17 at 19:56
0

Try to replace

invoker.setMavenHome(new File(System.getenv("MAVEN_HOME"))) 

with

invoker.setMavenHome(new File(System.getenv().get("MAVEN_HOME")))
DrJunior
  • 3
  • 1