[英]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 文件)
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
现在唯一的问题是每个项目的文件路径将不同,因为它取决于
groupId
和artifactId
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.