一、实现目标:
目标一、生成的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>
分享到:
相关推荐
maven 简单实用的配置,打包,获取svn版本号、仓库等等实用
NULL 博文链接:https://shansun123.iteye.com/blog/983763
maven打包 maven打jar包详细步骤
包含了企业常用SVN插件,Maven插件等
Maven打包,指定classes路径打包到WebContent/WEB-INF中
NULL 博文链接:https://acooly.iteye.com/blog/1788811
Windows下实现的,git的自动拉取推送,svn的自动拉取和推送,maven自动编译,angular的自动打包发布,bat脚本
sprintboot maven 打包分离lib jar 资源文件 properties xml yml 详细信息查看我的博客 https://mp.csdn.net/postedit/80274087 java -jar -cp 启动
自己整理的maven打包出现错误的解决办法、含有截图等 附带maven搭建私服等资料
idea新建maven web项目.zip Jetbrains IntelliJ IDEA创建基于maven打包工具的WEB网站项目 本项目使用的是SSM框架spring mvc,spring, mybatis.用maven打包成jar
这是一个springboot+maven写的demo,主要演示springboot项目通过maven插件,将依赖与配置文件打包到jar包外部,实现灵活的项目管理.仅供参考学习
Maven配置SVN的方法和SVN包全套,测试后整理的
maven打包源码配置
jenkins+maven+svn+tomcat 持续集成环境快捷部署,实现自动编译打包部署
当前案例中包含一整套的代码和word文档,非常适合新手... 主要是通过maven打包加载不同环境的properties文件 然后将对于的属性绑定到指定的实体对象中;然后通过调用接口可以看到加载不同环境控制台打印的内容是不一样的
maven+bat打包,通过在maven中配置插件执行bat脚本进行目录下的打包。
工程运行环境有研发,测试和生产,不同的运行环境配置不同,maven 打包时指定文件目录,使用指定的文件夹下的配置文件。
java视频教程之SVN Maven搭建自动化构建教程 Eclipse中使用Maven视频教程云盘下载链接
maven-svn-revision-number-plugin-1.7.jar 资源共享,有需要其他jar包的可以在评论留言,看到后我会陆续上传。
jasperreport 用maven打包后找不到字体解决方案 net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font