Как получить путь к файлу pom в военном проекте Maven - PullRequest
0 голосов
/ 21 сентября 2018

привет, есть ли способ получить путь к файлу pom в проекте maven.Мне нужно прочитать файл 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());
          }

Ответы [ 4 ]

0 голосов
/ 21 сентября 2018

Вы можете добавить pom в папку META-INF, как предложено выше.Вы также можете использовать файл pom.properties, который Jar и связанные плагины архивов Maven помещают в META-INF по умолчанию.В любом случае вы можете получить к нему доступ как к ресурсу, если jar находится в пути к классам.Единственный пример, который у меня есть, написан на 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
}
0 голосов
/ 21 сентября 2018

В проекте maven каталог проекта содержит pom.xml.
System.getProperty("user.dir") дает каталог проекта.
Try - Model model = reader.read(MainTest.class.getResourceAsStream( System.getProperty("user.dir")+"/pom.xml"));

0 голосов
/ 21 сентября 2018

Одним из решений является копирование эффективного pom в папку META-INF.Проверьте эту ссылку Добавьте эффективный pom.xml в каталог META-INF

0 голосов
/ 21 сентября 2018
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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...