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

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

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

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

三、实现过程:
1、pom.xml文件配置:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.sinowel
UFM
1.0.20
war
Unified Flow Management
http://http://10.180.50.30:8099/scpnf


yyyyMMdd




scm:svn:https://192.168.22.34/svn/UMPlatform4J/SCPNF/banches/UFM
HEAD
https://192.168.22.34/svn/UMPlatform4J/SCPNF/banches/UFM










maven-compiler-plugin
3.1

1.7
1.7
UTF-8





org.codehaus.mojo
buildnumber-maven-plugin
1.4


validate

create




false
true



com.google.code.maven-scm-provider-svnjava
maven-scm-provider-svnjava
2.1.1


org.tmatesoft.svnkit
svnkit
1.8.10






maven-war-plugin
2.4

false
UTF-8
${project.artifactId}_v${project.version}r${buildNumber}b${maven.build.timestamp}


true


${buildNumber}
${project.artifactId}_v${project.version}r${buildNumber}b${maven.build.timestamp}











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 Reader or null 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 Reader or null 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、在指定页面获取存入版本号:

版本:${deployVersion }

你可能感兴趣的:(maven打包将SVN版本号打入系统。)