hi有一种方法可以在Maven项目中获取pom文件的路径。 我需要读取pom文件,并使用它来获取一些信息,例如版本,依赖项列表等。
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(MainTest.class.getResourceAsStream( "how to get this path"+"/pom.xml"));
for(Dependency d : model.getDependencies())
{
System.out.println(d.getArtifactId());
}
答案 0 :(得分:1)
一种解决方案是将有效pom复制到META-INF文件夹。检查此链接Add the effective pom.xml in the META-INF directory
答案 1 :(得分:1)
您可以按照上述建议将pom添加到META-INF文件夹中。您还可以默认使用Jar和相关Maven存档插件将其放在META-INF中的pom.properties文件。无论哪种情况,只要罐子在类路径中,您就可以将其作为资源访问。我对此的唯一示例是用Groovy编写的:
def static String getMavenVersion(Class mainClass) {
//
// If this doesn't work, the likely reason is that we're running in development, so start with a
// reasonable default
//
def String result = "Lastest Development"
//
// Get our package - one up in the tree is the path to the properties file we want.
//
def String packagePath = mainClass.package.name
def int index = packagePath.lastIndexOf(".")
def String groupId = packagePath.substring(0, index)
def String artifact = packagePath.substring(index + 1)
//
// Get the version from the property file
//
Protect.resource
{
InputStream input = MiscUtils.class.getResourceAsStream("/META-INF/maven/${groupId}/${artifact}/pom.properties")
input
}
{ InputStream input ->
if (input != null) {
def Properties mavenProps = new Properties()
mavenProps.load(input)
result = mavenProps.getAt("version")
}
}
result
}
答案 2 :(得分:0)
example;
ı_____project files(pom.xml in this files)
I_____src
I____main
ı____java
ı___com
ı___file
ı___file2
ı___ my class
your be if class hierarchy;
....../pom.xml
or
./../../../../../pom.xml
答案 3 :(得分:0)
在maven项目中,项目目录具有pom.xml。
System.getProperty("user.dir")
给出项目目录。
尝试-
Model model = reader.read(MainTest.class.getResourceAsStream( System.getProperty("user.dir")+"/pom.xml"));