【发布时间】:2019-10-22 06:39:37
【问题描述】:
我想从pom.xml 访问一些信息以显示在信息对话框中。于是我google了一下,发现this post:
public class MavenModelExample {
public static void main(String[] args) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
System.out.println(model.getId());
System.out.println(model.getGroupId());
System.out.println(model.getArtifactId());
System.out.println(model.getVersion());
}
}
我在我的工具中实现了它,添加
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.3.9</version>
</dependency>
致我的 pom,很高兴当我使用 java -jar target\mytool.jar 从项目根目录运行该工具时,一切都按预期运行。
当我移动到任何其他目录时,例如直接进入target 并使用java -jar mytool.jar 执行我的工具,我得到:
java.io.FileNotFoundException: pom.xml (The system cannot find the specified file)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:110)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
这是可以理解的。代码应该如何知道pom.xml 的位置,因为它不是资源。有没有办法解决这个问题?
同时我使用this thread的方法来获取版本和artifactID。
【问题讨论】:
-
贴出代码,以便 SO 成员可以帮助您。
-
我会尝试资源过滤而不是读取 POM。您需要哪些信息?
-
目前我主要想检索我的工具的artifactID和版本。但后来我也想访问依赖版本
标签: java maven runtime pom.xml