1

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");
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
User 0234
  • 135
  • 2
  • 11
  • Why not just create an `uber jar` (jar with all dependencies) and then work with that ? You can refer to [this](https://stackoverflow.com/a/574650/679824) – Krishnan Mahadevan Jul 08 '19 at 17:38
  • Issue is not with accessing jars. Issue is unable to point pom.xml for Main class while executing jar as pom.xml is sitting inside META-INF folder. – User 0234 Jul 09 '19 at 02:55
  • lets take a step back. What are you trying to solve? Your question seems to suggest that you just need a jar of jars and that should get the problem resolved. Instead of you trying to create a jar file that would access other jars in the classpath and then readup their pom file and then do stuff with that, wouldn't be easier if you just created an uber jar (which is a jar that contains all the dependencies it needs as one big fat jar) and work with it? – Krishnan Mahadevan Jul 10 '19 at 04:18

1 Answers1

0

You can prepare the path using the .. in the path. It will give you the parent directory location from where you are at.

Updated

Suppose you folder structure is as

C:/my-proj/java/Main.java
C:/my-proj/META-INF/pom.xml

Then if you want to find the pom.xml. You can do something like below

String str = System.getProperty("user.dir");
System.out.println(str+"/../META-INF/pom.xml");

Try this.

A_01
  • 1,021
  • 11
  • 27
  • Even the update doesnt work and wont work. Because pom.xml will be inside a jar file like `C:\TestProject\lib\TestProject.jar!\META-INF\maven\com.test.package\TestProject\pom.xml` – User 0234 Jul 09 '19 at 10:40
  • String str = System.getProperty("user.dir"); System.out.println(str); what do you see in str? – A_01 Jul 09 '19 at 10:59
  • It printed `C:\TestProject` and if I do `str+"/../META-INF/maven/com.test.package/TestProject/pom.xml"` it prints `C:\TestProject\..\META-INF\maven\com.test.package\TestProject\pom.xml` – User 0234 Jul 09 '19 at 11:16
  • Ok I think you can find the pom file at this location then. String str = System.getProperty("user.dir"); System.out.println(str+"/META-INF/maven/com.test.package/TestProject/pom.xml"); – A_01 Jul 09 '19 at 11:16
  • It wont find... `pom.xml` is inside `.jar` file not in a folder. – User 0234 Jul 09 '19 at 11:41