hi is there a way to get the path of pom file in a maven project. i need to read pom file and use it to get some information like version, list of dependencies and so on ..
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());
}
One solution is to copy the effective pom to META-INF folder. Check this link Add the effective pom.xml in the META-INF directory
You can add the pom to the META-INF folder as suggested above. You can also use the pom.properties file that the Jar and related Maven archive plugins put in META-INF by default. In either case, you can then access it as a resource provided that the jar is in the classpath. The only example I have of this is written in 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
}
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
In maven project, project directory has pom.xml.
System.getProperty("user.dir")
gives project directory.
Try - Model model = reader.read(MainTest.class.getResourceAsStream( System.getProperty("user.dir")+"/pom.xml"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.