包括从pom.xml到应用程序包的应用程序版本号

gav*_*koa 11 java build version maven

每个人pom.xml都有:

<groupId>com.vendor</groupId>
<artifactId>product</artifactId>
<version>1.0.13.0-dev</version>  <!-- THIS FIELD -->
<packaging>war</packaging>
Run Code Online (Sandbox Code Playgroud)

version piece对于捆绑应用程序非常有用,因此用户可以查看和报告它们.

但我不喜欢代码重复,并寻找一种方法来避免将带有版本信息的属性文件放到VCS(Git/SVN),因为每次我碰到版本时我必须提交到两个地方(pom.xmlversion.properties).

我可以通过哪种方式在Java应用程序中以编程方式提供版本属性pom.xml

小智 9

找到一个使用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>
Run Code Online (Sandbox Code Playgroud)

footer.jsp然后有:

Application Version: ${project.version}
Run Code Online (Sandbox Code Playgroud)


gav*_*koa 7

我的个人生产解决方案如下

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>
     ...
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

proj.properties:

version=${project.version}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

要不就

<util:properties id="props" location="classpath:proj.properties"/>
Run Code Online (Sandbox Code Playgroud)


ski*_*sch 6

看看Maven Resources Plugin.在你的version.properties(应该在resources文件夹中),定义一个像这样的行version=${project.version}.确保打开插件的过滤(所有这些都在上面的链接中详细描述),你应该好好去!

只需运行mvn resources:resources(或mvn compile)并检查生成的文件.