我有这个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
的版本。
答案 0 :(得分:1)
来自我在Eclipse中进行的实时Wicket项目构建。 您的路径会有所不同,使用任何7-Zip或类似的ZIP工具分析您的WAR。
try {
InputStream propIs = getServletContext()
.getResourceAsStream("/META-INF/maven/MyProject/MyProject/pom.properties");
Properties properties = new Properties();
properties.load(propIs);
propIs.close();
mavenVersion = (String) properties.get("version");
} catch (Throwable t) {
mavenVersion = "unknown";
}
更新:此类代码段与Web框架(JSF / Wicket)无关。
答案 1 :(得分:1)
我会向后解释:
让我们开始显示修订号:
<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>
因为我是前一段时间创建的,所以某些部分可能(有些)过时了,但是它们仍然可以正常工作。