在代码中从maven pom.xml中检索版本 [英] Retrieve version from maven pom.xml in code

查看:445
本文介绍了在代码中从maven pom.xml中检索版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中从maven的pom.xml中检索版本号的最简单方法是什么,即以编程方式检索?

What is the simplest way to retrieve version number from maven's pom.xml in code, i.e., programatically?

推荐答案

假设你正在使用Java,你可以

Assuming you're using Java, you can


  1. 创建一个 .properties 在(最常见的)你的 src / main / resources 目录中存档(但在步骤4中,你可以告诉它在其他地方查找)。

  1. Create a .properties file in (most commonly) your src/main/resources directory (but in step 4 you could tell it to look elsewhere).

使用项目版本的标准Maven属性设置 .properties 文件中某些属性的值: foo.bar = $ {project.version}

Set the value of some property in your .properties file using the standard Maven property for project version: foo.bar=${project.version}

在Java代码中,从类路径中将属性文件中的值作为资源加载(google有关如何执行此操作的大量示例,但此处是初学者的示例。)

In your Java code, load the value from the properties file as a resource from the classpath (google for copious examples of how to do this, but here's an example for starters).

在Maven中,启用资源过滤 - 这将导致Maven将该文件复制到输出类中并在该co期间转换资源py,解释财产。您可以在此处找到一些信息,但您大多只是在你的pom:

In Maven, enable resource filtering - this will cause Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property. You can find some info here but you mostly just do this in your pom:




    <build>
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </resources>   
    </build>

您还可以访问其他标准属性,例如 project.name project.description ,甚至是放在你的pom中的任意属性< properties> 等。资源过滤,结合Maven配置文件,可以在构建时为您提供变量构建行为。使用 -PmyProfile 在运行时指定配置文件时,可以启用随后可以显示在构建中的属性。

You can also get to other standard properties like project.name, project.description, or even arbitrary properties you put in your pom <properties>, etc. Resource filtering, combined with Maven profiles, can give you variable build behavior at build time. When you specify a profile at runtime with -PmyProfile, that can enable properties that then can show up in your build.

这篇关于在代码中从maven pom.xml中检索版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆