我有这个pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>es.ja.xxx.yyy.aaa</groupId>
        <artifactId>name-parent</artifactId>
        <version>1.0.1.2-SNAPSHOT</version>
    </parent>
</project>


我需要从Java代码获取pom.xml的版本。

最佳答案

我将向后解释:


在方面中显示值(OP不需要)
提供值的托管bean
pom.xml创建manifest.mf


因此,让我们开始显示修订号:

<h:outputText value="#{appInfo.revision}" />


结果如下所示,其中0.0.40是poms项目版本中的version

0.0.40 / 29.05.2018 12:42


如您所见(如下图所示),结果显示了修订版本和构建日期。为了容忍,以下代码使用version作为生成日期。

appInfo是ApplicationScoped Bean,可从清单中获取版本/修订版信息。以下是Bean的相关部分:

@Named
@ApplicationScoped
public class AppInfo {

  private String    revision; // + getter

  @PostConstruct
  public void init() {

    InputStream is = FacesContext.getCurrentInstance().getExternalContext()
                     .getResourceAsStream("/META-INF/MANIFEST.MF");
    Manifest manifest = new Manifest();
    try {
        manifest.read(is);
    } catch (IOException e) {
        // error handling
    } finally {
        try {
          is.close();
        } catch (IOException e) {
          // error handling
        }
    }
    Attributes attr = manifest.getMainAttributes();
    String implRevision = attr.getValue("Implementation-Build");
    String implVersion = attr.getValue("Implementation-Version");

    if (implRevision == null || implVersion == null)
        this.revision = "unknown";
    else
        this.revision = implVersion + " / " + implRevision;
  }
}


Maven内部版本号插件(http://www.mojohaus.org/buildnumber-maven-plugin/)从pom.xml中获取内部版本号,而清单由maven war插件生成。

在pom的构建部分中


将修订信息放入变量(称为${buildNumber})中
使用此值生成清单


pom.xml中的片段:

 <project>
   <build>
     <plugins>

        <!-- revision number to ${buildNumber} -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>create</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <format>{0,date,dd.MM.yyyy HH:mm}</format>
                <items>
                    <item>timestamp</item>
                </items>
                <doCheck>false</doCheck>
                <doUpdate>false</doUpdate>
            </configuration>
        </plugin>
        <!-- END: revision number to ${buildNumber} -->

        <!-- START: generate MANIFEST.MF -->
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <version>2.5</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                    <manifestEntries>
                        <version>${project.version}</version>
                        <Implementation-Build>${buildNumber}</Implementation-Build>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        <!-- END: generate MANIFEST.MF -->

    </plugins>
  </build>
</project>


因为我是前一段时间创建的,所以某些部分可能(有些)过时了,但是它们仍然可以正常工作。

09-28 11:59
查看更多