2013-04-01 50 views
8

每個pom.xml有:包括應用程序的版本號從pom.xml的應用程序包

<groupId>com.vendor</groupId> 
<artifactId>product</artifactId> 
<version>1.0.13.0-dev</version> <!-- THIS FIELD --> 
<packaging>war</packaging> 

version一塊是非常有用的一堆與應用程序,使用戶可以看到並報告他們。

但我不喜歡重複代碼,並尋找一種方式,以避免將屬性文件與版本信息到VCS(GIT/SVN),因爲每一個我碰到的版本時,我必須提交到兩個地方(pom.xmlversion.properties)。

我可以製作哪種方式版本屬性pom.xml以編程方式在Java應用程序中可用?

+1

您可以保存到表現jar或war文件,然後利用它在你的應用程序信息。 –

+1

感謝您的建議+1。目前我嘗試使用技術從http://stackoverflow.com/questions/7144620/reading-properties-file-from-maven-pom-file – gavenkoa

回答

5

看看Maven Resources Plugin。在您的version.properties(應位於resources文件夾中)中,定義一條線如version=${project.version}。確保打開插件的過濾器(全部在上面的鏈接中詳細描述),你應該很好地去!

只需運行mvn resources:resources(或mvn compile)並檢查生成的文件。

+0

我找到了一個代碼[this](http://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime)不改變Maven配置。 – Wendel

2

你可以簡單地配置maven-war-plugin, maven-jar-plugin, maven-ear-plugin類似如下:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>${maven-war-plugin.version}</version> 
    <configuration> 
    <archive> 
     <addMavenDescriptor>true</addMavenDescriptor> 
     <index>true</index> 
     <manifest> 
     <addDefaultImplementationEntries>true</addDefaultImplementationEntries> 
     <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> 
     </manifest> 
     <manifestEntries> 
      <artifactId>${project.artifactId}</artifactId> 
      <groupId>${project.groupId}</groupId> 
      <version>${project.version}</version> 
     </manifestEntries> 
    </archive> 
    </configuration> 
</plugin> 

讀取信息從manifest.mf can be done using jacabi.

+0

謝謝! +1我使用資源方法,因爲一個'.property'文件已經有了Spring'@ Value'。 – gavenkoa

6

我個人生產下方解決方案。

的pom.xml

<?xml version="1.0" encoding="utf-8"?> 
<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"> 

    <version>1.0.13.0-dev</version> 

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

base.jsp

<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <a href='<c:url value="/"/>'> 
    <spring:eval expression="@props.getProperty('version')"/> 
    </a> 
    <jsp:include page="${contentPage}" /> 
</body> 
</html> 

proj.properties

version=${project.version} 

的applicationContext.xml

<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean" autowire="byName" > 
    <property name="location" value="classpath:proj.properties"/> 
</bean> 

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="classpath:proj.properties"/> 
</bean> 

或只是

<util:properties id="props" location="classpath:proj.properties"/> 
+0

@Atharva感謝您的編輯! – gavenkoa

5

找到了解決使用maven-war-plugin更雅緻。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.3</version> 
    <configuration> 
     <webResources> 
      <resource> 
       <targetPath>WEB-INF/views/jsp</targetPath> 
       <directory>${basedir}/src/main/webapp/WEB-INF/views/jsp</directory> 
       <includes> 
        <include>footer.jsp</include> 
       </includes> 
       <filtering>true</filtering> 
      </resource> 
     </webResources> 
    </configuration> 
</plugin> 

footer.jsp中然後有:

Application Version: ${project.version} 
+0

不錯!而不是從JSP文件中直接嵌入資源文件來評估它! +1 – gavenkoa