1

I'm working on a plugin capable of displaying the list of the dependencies of a Maven project. To make this possible, I read the pom.xml file and print data on the console for debugging. This worked just fine, until I tried it from the JAR file : I got a FileNotFoundException (for the pom.xml file)

  • Is there a better way to list dependencies of a Maven project ?
  • How to read pom.xml when we are inside the JAR file ?

Here is my original question

Mattew Eon
  • 1,722
  • 1
  • 21
  • 38
  • You need to show the code that you're using. But the big tip is that you cannot read resources from the classpath as `java.io.File` objects because there are no "files" inside a jar/ – Steve C Jan 16 '18 at 10:54
  • I can't because my code is on another computer in my company and I can't move the data to my personal computer. I used the code specified in my original question with small upgrades, but this is not important here. I have to use `InputStream` ? And if yes, how can I call `InputStream` when it's on the jar file and how can I use `File` from `java.io` when it's not running from the Jar file ? – Mattew Eon Jan 16 '18 at 11:05
  • 2
    You can try to read the pom.xml as resource. – J Fabian Meier Jan 16 '18 at 11:06
  • I works when I want to read the pom.xml as resource, but I have to specify the path and it's dynamic : `META-INF/maven/{groupId}/{artifactId}/pom.xml`. I don't know how to get these two specific data at runtime – Mattew Eon Jan 16 '18 at 13:35
  • You know some things like mvn dependency:tree ? – khmarbaise Jan 16 '18 at 14:01
  • @khmarbaise I am aware of that but I can't use it. The main goal is to list dependencies on `dev` and `prod` environements. Maven is not installed here, so it is not possible to run a `mvn` command – Mattew Eon Jan 16 '18 at 14:05

2 Answers2

5

As suggested JF Meier, I tried to read the pom.xml file as a resource and it worked.

MyClass obj = new MyClass();
InputStream pomFile = obj.getClass().getClassLoader().getResourceAsStream("META-INF/maven/groupId/artifactId/pom.xml");

The only issue now is that the path to the file will not be the same for each project as it depends of the groupId and the artifactId

Mattew Eon
  • 1,722
  • 1
  • 21
  • 38
0

What worked for me was

$ unzip foo.jar foo
$ cat foo/META-INF/maven/com.bar/foo/pom.xml

Another idea that didn't work for me instead of unzipping is jar -xf foo.jar

Zap
  • 325
  • 1
  • 6
  • 23