maven 版本号插件_Maven内部版本号插件–用法示例

maven 版本号插件

假设我们需要向一些工件(jar,war等)添加内部版本号。 在这里,我想演示buildnumber-maven-plugin的用法。

这篇文章基于:

我们有一些项目,需要在jar清单文件中包含不基于VCS(SVN,Git,Mercurial等)修订版本的顺序内部版本号。 让我们创建适当的pom.xml文件,并实施小型演示以验证结果。

生成Maven项目

$ mvn archetype:generate -DgroupId=org.halyph -DartifactId=buildNoTest\
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

创建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/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>org.halyph</groupid>
 <artifactid>buildNoTest</artifactid>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>buildNoTest</name>
 <url>http://maven.apache.org</url>
 
 <dependencies>
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <properties>
  <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
 </properties>
 
 <!-- 
 If you have access to scm then you can place actual url's. 
 Otherwise with <revisionOnScmFailure /> you can give some fake URLs as follows. 
 -->
 <scm>
  <connection>scm:svn:http://none</connection>
  <developerconnection>scm:svn:https://none</developerconnection>
  <url>scm:svn:https://none</url>
 </scm>
 
 <build>
  <resources>
   <resource>
    <directory>src/main/resources</directory>
   </resource>
   <resource>
    <directory>src/main/filtered-resources</directory>
    <filtering>true</filtering>
   </resource>
  </resources>
  <plugins>
   <plugin>
    <groupid>org.codehaus.mojo</groupid>
    <artifactid>buildnumber-maven-plugin</artifactid>
    <version>1.1</version>
    <executions>
     <execution>
      <phase>generate-resources</phase>
      <goals>
       <goal>create</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <!-- 
      doCheck and doUpdate actually talk to repository if it's true,
      Check would check that there are no local changes. 
      Update would update it 
     -->
     <docheck>false</docheck>
     <doupdate>false</doupdate>
     <!-- 
      This ensures that even if we are not connected to scm than also
      take the version from local .svn file 
     -->
     <revisiononscmfailure>
      
     <!--
      Generate sequence build number based on:
      build number and timestamp      
     -->
     <format>Build: #{0} ({1,date})</format>
     <items>
      <item>buildNumber\d*</item>
      <item>timestamp</item>
     </items>
    </revisiononscmfailure></configuration>
   </plugin>
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-jar-plugin</artifactid>
    <version>2.1</version>
    <configuration>
     <archive>
      <!-- will put the entries into META-INF/MANIFEST.MF file -->
      <manifestentries>
       <implementation-version>${project.version}</implementation-version>
       <implementation-build>${buildNumber}</implementation-build>
      </manifestentries>
     </archive>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

创建演示应用程序以验证结果

package org.halyph;

import java.io.IOException;
import java.util.ResourceBundle;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

public class App
{
    public static void main( String[] args ) throws IOException
    {
        System.out.println('Verify Resource bundle' );
  
 // Check filtered resources based on generated build number
        ResourceBundle bundle = ResourceBundle.getBundle( 'build' );
        String msg = bundle.getString( 'build.message' );
        System.out.println(msg);

        System.out.println('\nVerify Generated MANIFEST.MF Properties' );

 // Check Manifest file based on generated build number
        Manifest mf = new Manifest();
        mf.read(Thread.currentThread().getContextClassLoader().getResourceAsStream('META-INF/MANIFEST.MF'));

        Attributes atts = mf.getMainAttributes();

        System.out.println('Implementation-Versio: ' + atts.getValue('Implementation-Version'));
        System.out.println('Implementation-Build: ' + atts.getValue('Implementation-Build'));
    }
}

多次构建应用程序并运行

$ mvn install
$ java -cp target\buildNoTest-1.0-SNAPSHOT.jar org.halyph.App
Verify Resource bundle
Build: #3 (Jun 27, 2012)

Verify Generated MANIFEST.MF Properties
Implementation-Versio: 1.0-SNAPSHOT
Implementation-Build: Build: #3 (Jun 27, 2012)

摘要

  1. 我们应该通过将伪造的<scm>部分添加到pom.xml中并将<revisionOnScmFailure />添加到buildnumber-maven-plugin <configuration>中,通知buildnumber-maven-plugin我们将不使用版本控制修订作为内部版本号。
  2. 已实现的自定义内部版本号格式,请参阅buildnumber-maven-plugin <配置> / <格式>和<配置> / <项目>。
  3. 在jar清单中添加了内部版本号,请参阅maven-jar-plugin pom.xml部分
  4. 测试生成的内部版本号是否可以正确添加到过滤的资源中
  • 创建的src \ main \ filtered-resources \ build.properties文件
build.message=${buildNumber}

翻译自: https://www.javacodegeeks.com/2012/10/maven-build-number-plugin-sample-usage.html

maven 版本号插件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.[今日课程内容总览] Maven--***** 介绍 优点 用maven创建一个项目, 用maven创建一个ssm项目,每一层的代码分开,把一个项目拆解成多个项目 2.[今日包含笔记] maven介绍: Eclise: 缺点: Jar包: 版本管理,解决jar包冲突:删除老的jar包,保留新的jar包 不同的项目,jar包要复制一份;Users Library 羡慕yum的好处,安装软件,自动下载,自动验证,自动安装,自动测试;镜像网站 Jar包的依赖: 致命的缺点:现象: 每一个项目都有前台(广大网民)和后台(管理员看的),后台的功能远远的大于>前台的功能;迭代升级;每次升级都要重启tomcat;将tomcat做很多个备份(10个),为了nginx转发;前台:访问比较大,管理员访问量比较小; 问题: 每次升级,10个tomcat都升级一次 后台的升级频繁要比前台频繁;升级10次,9次改的是后台的代码,只有一次是前台的代码 解决方案: 是将前台(广大网民看的页面)的代码和后台(管理员)的代码要分开,将原来的一个项目分别放到两个web项目;一个web项目一个tomcat;好处:更新的时候如果更新后台(管理员),只需要重启后台的tomcat(服务); 折成两个项目以后的缺点:Service以下的代码重复; 将Service层以下的代码放到一个新创建的java项目中,在部署的时候将Service(java项目)打成一个jar包,分别放到两个web服务中;编译的时候让web项目依赖与Service(java项目)_source folder;配置麻烦,项目与项目之间的依赖头脑得相当清晰 这个缺点Maven解决了; 请听今天的内容 拆分前:图一 拆分后是图二 3. 介绍 官网:http://maven.apache.org/download.cgi 目录介绍: Bin:二进制文件 Boot:引导,启动 Conf:配置文件 Lib:jar包 README.txt:读我 3.1.修改配置文件的内容 路径:%MAVEN_HOME%\conf\settings.xml 配置文件代码: d:/repo alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central 注意: 千万不要将文档中的setting的内容全部替换到maven中的内容,要对比着修改,保留原来的; 只修改本地仓库,和下载镜像(源) 3.2.Eclipse Maven的配置 每打开一个新的工作空间,要配置一下Maven,然后再写代码 3.3.创建Maven项目 右击创建一个maven project Packing:有三种: Jar:java项目, War:web项目 Pom:文件夹,jar和war文件,mavenProject:一个项目,可以放jar和war包,通过pom,一般新创建的maven Project是POM 3.4.创建一个Maven Model 最外面的项目是MavenProject,其它的全部是Maven Model 3.4.1.创建JAR 目录介绍: Mvn-jar |-->Src:源码:java代码 |-->|-->main:代码:除测试以外的代码:junit |-->|-->main|-->java:代码 |-->|-->main|--

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值