Java 从pom.xml获取项目的版本
我有这个Java 从pom.xml获取项目的版本,java,maven,Java,Maven,我有这个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.
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>
4.0.0
es.ja.xxx.yyy.aaa
命名家长
1.0.1.2-快照
我需要从Java代码中获取我的
pom.xml
版本。取自我在Eclipse中构建的live Wicket项目。
路径将在您的中不同,使用任何ZIP工具、7-ZIP或类似工具分析您的战争
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)没有任何关系。我将向后解释:
<h:outputText value="#{appInfo.revision}" />
正如您看到的(如下所示),结果显示了修订和构建日期。请宽容,以下代码使用version
作为生成日期
appInfo
是一个应用程序范围的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;
}
}
buildnumber由maven build number plugin()从pom.xml获取,清单由maven war plugin生成
在pom的构建部分,您可以
${buildNumber}
)中 <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>
org.codehaus.mojo
buildnumber maven插件
1.3
验证
创造
{0,日期,dd.MM.yyyy HH:MM}
时间戳
假的
假的
maven战争插件
org.apache.maven.plugins
2.5
网络内容
假的
真的
${project.version}
${buildNumber}
因为我在不久前创建了它,一些部件可能(有点)过时了,但它们仍然可以正常工作
<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>