maven打包将SVN版本号打入系统。

一、实现目标:
目标一、生成的war包名称根据[项目名称]_[系统版本号]_[SVN版本号]_[打包日期]格式自动生成war包。
目标二、系统主页可以展示当前系统的最新版本信息(以上格式的版本信息)。

二、实现思路:
对于目标一:[项目名称]、[系统版本号]根据pom.xml文件中参数配置;
[SVN版本号]从SVN服务器获取;
[打包日期]时间戳;
根据以上信息,自动生成自定格式的war包文件。

对于目标二:将目标一生成的最终版本号以自定义属性的方式追加到META-INF/MANIFEST.MF文件下;并在系统启动的时候加载META-INF/MANIFEST.MF文件,取出并显示到指定页面文件。

三、实现过程:
1、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>
<groupId>com.sinowel</groupId>
<artifactId>UFM</artifactId>
<version>1.0.20</version>
<packaging>war</packaging>
<name>Unified Flow Management</name>
<url>http://http://10.180.50.30:8099/scpnf</url>

<properties>
<maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
</properties>


<scm>
<connection>scm:svn:https://192.168.22.34/svn/UMPlatform4J/SCPNF/banches/UFM</connection>
<tag>HEAD</tag>
<url>https://192.168.22.34/svn/UMPlatform4J/SCPNF/banches/UFM</url>
</scm>

<dependencies>
<!-- 略 -->
</dependencies>


<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>


<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>true</doUpdate>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.maven-scm-provider-svnjava</groupId>
<artifactId>maven-scm-provider-svnjava</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</plugin>


<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<encoding>UTF-8</encoding>
<warName>${project.artifactId}_v${project.version}r${buildNumber}b${maven.build.timestamp}</warName>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<svn-version>${buildNumber}</svn-version>
<deploy-version>${project.artifactId}_v${project.version}r${buildNumber}b${maven.build.timestamp}</deploy-version>
</manifestEntries>
</archive>
</configuration>
</plugin>

</plugins>
</build>

</project>


2、创建ManifestUtils.java文件,用来读取META-INF/MANIFEST.MF文件。

public class ManifestUtils {

private static final String MANIFEST_DIRECTORY_LOCATION = "META-INF" + File.separator + "MANIFEST.MF";

private static final String MANIFEST_ENTRY = "META-INF/MANIFEST.MF";

/**
*
* Creates a {@link Reader} for the manifest in the supplied exploded JAR
* directory.
*
*
*
* @param directory
* the exploded JAR directory.
*
* @return the <code>Reader</code> or <code>null</code> if the manifest
* cannot be found.
*/
public static final Reader manifestReaderFromExplodedDirectory(File directory) {
if (directory == null || !directory.isDirectory()) {
throw new IllegalArgumentException("Must supply a valid directory");
}
try {
File manifestFile = new File(directory.getAbsolutePath() + File.separator + MANIFEST_DIRECTORY_LOCATION);
if (manifestFile.exists()) {
return new FileReader(manifestFile);
} else {
return null;
}
} catch (IOException e) {
throw new RuntimeException(
"Unable to read MANIFEST for exploded directory '" + directory.getAbsolutePath() + "'.", e);
}
}

/**
*
* Creates a {@link Reader} for the manifest in the supplied JAR file.
*
*
*
* @param file
* the JAR file.
*
* @return the <code>Reader</code> or <code>null</code> if the manifest
* cannot be found.
*/
public static final Reader manifestReaderFromJar(File file) {
JarFile jar = null;
try {
jar = new JarFile(file);
JarEntry entry = jar.getJarEntry(MANIFEST_ENTRY);
if (entry != null) {
StringWriter writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(jar.getInputStream(entry)), writer);
jar.close();
return new StringReader(writer.toString());
} else {
return null;
}
} catch (Exception e) {
throw new RuntimeException("Cannot read MANIFEST.MF from jar '" + file.getAbsolutePath() + "'.", e);
} finally {
if (jar != null) {
try {
jar.close();
} catch (IOException ioe) {
throw new RuntimeException("Failed to close jar '" + file.getAbsolutePath() + "'.", ioe);
}
}
}
}

/**
* 从META-INF/MANIFEST.MF文件中读取“deploy-version”发布版本号信息。
*
* @param realPath
* web发布真实路径
* @return
*/
public static String getDeployVersion(String realPath) {
File file = new File(realPath);
Reader reader = manifestReaderFromExplodedDirectory(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String lineTxt = null;
String deployVersion = "";
try {
while ((lineTxt = bufferedReader.readLine()) != null) {
if (lineTxt.startsWith("deploy-version:")) {
deployVersion = lineTxt.replace("deploy-version:", "");
return deployVersion;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
}
}
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return deployVersion;
}

}


3、在系统启动位置如ContextLoaderListener中调用ManifestUtils.getDeployVersion方法取得发布版本,并存放。例如:

String deployVersion = "";
deployVersion = ManifestUtils.getDeployVersion(servletContext.getRealPath(""));
servletContext.setAttribute("deployVersion", deployVersion);


4、在指定页面获取存入版本号:

<div id="deployVersion ">版本:${deployVersion }</div>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值