Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 包括从pom.xml到应用程序包的应用程序版本号_Java_Maven_Build_Version - Fatal编程技术网

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

Java 包括从pom.xml到应用程序包的应用程序版本号,java,maven,build,version,Java,Maven,Build,Version,每个pom.xml都有: <groupId>com.vendor</groupId> <artifactId>product</artifactId> <version>1.0.13.0-dev</version> <!-- THIS FIELD --> <packaging>war</packaging> com.vendor 产品 1.0.13.0-dev 战争 versionp

每个
pom.xml
都有:

<groupId>com.vendor</groupId>
<artifactId>product</artifactId>
<version>1.0.13.0-dev</version>  <!-- THIS FIELD -->
<packaging>war</packaging>
com.vendor
产品
1.0.13.0-dev
战争
version
piece与应用程序捆绑在一起非常有用,用户可以查看并报告它们

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

通过哪种方式可以使
pom.xml
中的version属性在Java应用程序中以编程方式可用?

查看。在
version.properties
(应该在
resources
文件夹中)定义一行,如
version=${project.version}
。确保打开插件的过滤功能(在上面的链接中详细描述了所有内容),你就可以开始了


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

您可以简单地配置以下内容:

 <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>

org.apache.maven.plugins
下面是我的个人生产解决方案

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>
     ...
<%@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>
version=${project.version}
<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>
applicationContext.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>
     ...
<%@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>
version=${project.version}
<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"/>

找到了一个解决方案,可以使用更优雅的

<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>

您可以将此信息保存到jar或war文件的清单中,然后在应用程序中使用它。感谢您的建议+1。目前我试着使用技术,谢谢+1我使用资源方法,因为已经为一个
.property
文件使用了Spring
@Value
。很好!而不是从资源文件中计算它,只是内联到JSP文件中+1我发现一个代码没有更改Maven配置。