I am trying to create an executable jar for my Maven-TestNG project. My ultimate target is to execute code in any machine just by jar files. My plan is to put all required/dependency jar files in a folder and pass the folder as -classpath.
As an executable jar needs a Main class, I created a main class and performing the goals by using InvocationRequest . As part of my goal, my TestNG.xml will be executed.
InvocationRequest request = new DefaultInvocationRequest();
File file = new File("pom.xml");
File fileWithPath = new File(file.getAbsolutePath());
request.setPomFile(new File(fileWithPath));
request.setGoals(Collections.singletonList("clean verify"));
DefaultInvoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(System.getenv("M2_HOME")));
try {
invoker.execute(request);
} catch (MavenInvocationException e) {
e.printStackTrace();
}
Above code is working fine when I execute it from Eclipse.
But once I do packaging and try to execute Java -cp lib/* com.test.package.runner.InvokerClass
from C:\TestProject
by using , it is showing as C:\TestProject\lib\pom.xml
is not found. I understood that pom.xml wont present on jar level, Instead it will be under C:\TestProject\lib\TestProject.jar!\META-INF\maven\com.test.package\TestProject\pom.xml
.
So how can I get the path of pom.xml from META-INF folder during run-time?
I tried setting pom.xml path in different ways as follows. But nothing worked well. Any suggestions please?
Most of the time, I got nullpointer exception as it couldn't find the path.
URL path = InvokerClass.class.getClass().getClassLoader().getResource("pom.xml");
URL path = InvokerClass.class.getClass().getClassLoader().getResource("./pom.xml");
URL path = InvokerClass.class.getClass().getClassLoader().getResource("META-INF/maven/com.adp.aca.test/aca-test-regression/pom.xml");
URL path = InvokerClass.class.getClass().getClassLoader().getResource("./META-INF/maven/com.adp.aca.test/aca-test-regression/pom.xml");