简体   繁体   English

Java - 从 JAR 访问 pom.xml 文件

[英]Java - access pom.xml file from JAR

I'm working on a plugin capable of displaying the list of the dependencies of a Maven project.我正在开发一个能够显示 Maven 项目依赖项列表的插件。 To make this possible, I read the pom.xml file and print data on the console for debugging.为了使这成为可能,我阅读了 pom.xml 文件并在控制台上打印数据以进行调试。 This worked just fine, until I tried it from the JAR file : I got a FileNotFoundException (for the pom.xml file)这工作得很好,直到我从 JAR 文件中尝试过:我得到了 FileNotFoundException(对于 pom.xml 文件)

  • Is there a better way to list dependencies of a Maven project ?有没有更好的方法来列出 Maven 项目的依赖项?
  • How to read pom.xml when we are inside the JAR file ?当我们在 JAR 文件中时如何读取 pom.xml?

Here is my original question 这是我原来的问题

As suggested JF Meier , I tried to read the pom.xml file as a resource and it worked.正如JF Meier所建议的那样,我尝试将 pom.xml 文件作为资源读取并且它有效。

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现在唯一的问题是每个项目的文件路径将不同,因为它取决于groupIdartifactId

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 unzip ping is jar -xf foo.jar另一个对我不起作用而不是unzip ping 的想法是jar -xf foo.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM