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


当前回答

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

Create a .properties file in (most commonly) your src/main/resources directory (but in step 4 you could tell it to look elsewhere). Set the value of some property in your .properties file using the standard Maven property for project version: foo.bar=${project.version} 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). 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指定配置文件时,可以启用属性,然后可以在构建中显示这些属性。

其他回答

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

Create a .properties file in (most commonly) your src/main/resources directory (but in step 4 you could tell it to look elsewhere). Set the value of some property in your .properties file using the standard Maven property for project version: foo.bar=${project.version} 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). 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指定配置文件时,可以启用属性,然后可以在构建中显示这些属性。

    <build>
            <finalName>${project.artifactId}-${project.version}</finalName>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>3.2.2</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <archive>
                                <manifest>
                                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                 </plugins>
            </pluginManagement>
</build>

使用this.getClass().getPackage().getImplementationVersion()获取版本

PS别忘了补充:

<manifest>
    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>

接受的答案在步骤#2中为我工作过一次,我改变了${项目。版本}到${pom.version}

打包的工件包含META-INF/maven/${groupId}/${artifactId}/pom。属性文件,内容如下:

#Generated by Maven
#Sun Feb 21 23:38:24 GMT 2010
version=2.5
groupId=commons-lang
artifactId=commons-lang

许多应用程序使用这个文件在运行时读取应用程序/jar版本,不需要任何设置。

上述方法的唯一问题是,该文件(目前)是在包阶段生成的,因此在测试期间不会出现(有一个Jira问题可以改变这一点,请参阅MJAR-76)。如果这对您来说是一个问题,那么Alex描述的方法就是正确的方法。

如果使用Spring和Maven,这非常简单,不需要配置。 根据“使用Maven自动扩展属性”官方文档,您可以使用资源过滤自动扩展Maven项目中的属性。如果你使用spring-boot-starter-parent,你就可以用@.引用Maven的“项目属性”。@占位符,示例如下:

project.version=@project.version@
project.artifactId=@project.artifactId@

你可以在任何类中使用@Value注释来检索它:

@Value("${project.artifactId}@${project.version}")
private String RELEASE;

我希望这能有所帮助!