java+maven⼯程实现⾃动对war包进⾏复制并修改和替换每个的配置
⽂件
在⼯作中碰到⼀个⽐较苦恼的事情,(这⾥以7条线为例⼦)同⼀个war包需要部署7条⽣产线,但是每个⽣产线的编号以及ip都不同,导致我们⼿动的每个包去替换配置⽂件和配置ip的js⽂件
并且每次发布,还需要⼿动去修改配置⽂件⾥⾯的版本号,这样⼗分的繁琐。因此该⼩程序实现对同⼀个war包进⾏⾃动替换⾥⾯的配置和js⽂件加⼊相关的版本号,
⽣成对应的7个war,可以减少很多的时间成本。
此功能配合jenkis ⾃动部署⽣成的war⽬录,即tomcat/webapps的⽬录下  xxx.war (jenkis ⾃动化部署下章在做笔记),使⽤该程序⾃动⽣成的war  1.war、2.war ...
7条线别对应的配置以及js⽂件在当前项⽬⾥⾯创建好。
原理很简单:原有的war包,复制7份并且替换包⾥⾯的配置⽂件以及js⽂件。
⼀、项⽬resources 下⾯创建⽂件夹如下⾯的7条线,每条线对应的propertie 和 js ⽂件都不⼀样(此项⽬是前后端分离的)
⼆、加⼊压缩和解压缩⼯具类
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import org.apachemonspress.archivers.ArchiveException;
import org.apachemonspress.archivers.ArchiveInputStream;
import org.apachemonspress.archivers.ArchiveOutputStream;
import org.apachemonspress.archivers.ArchiveStreamFactory;
import org.apachemonspress.archivers.jar.JarArchiveEntry;
import org.apachemonspress.archivers.zip.ZipArchiveEntry;
import org.apachemonspress.utils.IOUtils;
import org.apachemons.io.FileUtils;
/**
* 处理WAR⽂件⼯具类。可压缩或解压缩WAR⽂件。
*
* @author Xiong Shuhong(shelltea@gmail)
*/
public class DeCompressUtil {
public static void unzip(String warPath, String unzipPath) {
File warFile = new File(warPath);
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(warFile));
ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.JAR,
bufferedInputStream);
JarArchiveEntry entry = null;
while ((entry = (JarArchiveEntry) in.getNextEntry()) != null) {
new File(unzipPath, Name()).mkdir();
} else {
OutputStream out = FileUtils.openOutputStream(new File(unzipPath, Name()));
out.close();
}
}
in.close();
} catch (FileNotFoundException e) {
} catch (ArchiveException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
public static void zip(String destFile, String zipDir) {
File outFile = new File(destFile);
try {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outFile));
ArchiveOutputStream out = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR,                    buffere
dOutputStream);
if (zipDir.charAt(zipDir.length() - 1) != '/') {
zipDir += '/';
}
Iterator<File> files = FileUtils.iterateFiles(new File(zipDir), null, true);
while (files.hasNext()) {
File file = ();
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, Path().replace(
out.putArchiveEntry(zipArchiveEntry);
out.closeArchiveEntry();
}
out.finish();
out.close();
} catch (IOException e) {
失败");
} catch (ArchiveException e) {
}
}
}
三、加⼊⽂件读写⼯具类
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* java读写⽂件,复制⽂件
* 读取d:/1.txt⽂件内容,写⼊f:/⽂件中.
* @author young
*宋轶个人资料简介
*/
public class FileUtil {
// 读写⽂件
public static void copyFile(InputStream targetInputStream, String destFile){
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter(destFile, false);
br = new BufferedReader(new InputStreamReader(targetInputStream));
String line = null;
while ((line = br.readLine()) != null) {
fw.write(line + "\n");
fw.flush();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
// 读写⽂件
public static void appendFile(String appendContent, String destFile){        FileWriter fw = null;
if(appendContent != null) {
try {
fw = new FileWriter(destFile, true);
fw.write(appendContent + "\n");
fw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
读取配置文件失败
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
四、该程序的配置⽂件
配置⽂件只有源⽬录和⽬标⽬录
五、替换⽬标⽂件业务代码(代码⾥⾯有详细的注释)import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.hxzhishi.odf.pcp.utils.DeCompressUtil;
import com.hxzhishi.odf.pcp.utils.FileUtil;
public class Main {
/
/打包源⽂件路径
private static String sourceFilePath;
//打包源⽂件路径
private static String replaceFilePath;
private static void readProperties() {
Properties properties = new Properties();
// 使⽤ClassLoader加载properties配置⽂件⽣成对应的输⼊流
InputStream in = ClassLoader().getResourceAsStream("application.properties");
// 使⽤properties对象加载输⼊流
try {
properties.load(in);
/
/获取key对应的value值
sourceFilePath = Property("app.target.file.path");
replaceFilePath = Property("place.file.path");
} catch (IOException e) {
System.out.Message());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获得pc的版本号
* @param sourceFile
* @return
*/
private static String getPcVersion(String sourceFile) {
String pcVersion = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(sourceFile));
String line = null;
while((line = br.readLine()) != null) {
if(line.indexOf("version") >= 0) {  //从原始的war⾥⾯获取版本号
pcVersion = line;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("获取version信息失败.");
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}结婚年龄测试
return pcVersion;
}在梦里吻的脸是什么歌
public static void main(String[] args) {
//1. 读取配置信息
readProperties();
String sourcePath = sourceFilePath;
File file = new File(sourcePath);
//2. 获取version信息
String pcVersion = getPcVersion(sourcePath + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "application.properties");
ists()) {
if(file.isDirectory()) {
File replaceFilesFold = new File(replaceFilePath);
ists() && replaceFilesFold.isDirectory()) {
File[] companyFoldArr = replaceFilesFold.listFiles();
if(companyFoldArr != null && companyFoldArr.length > 0) {
for(File companyFold : companyFoldArr) {
if(companyFold.isDirectory() && Name().indexOf("svn") < 0) {//公司名称
File[] factoryFoldArr = companyFold.listFiles();
x战警观影顺序
if(factoryFoldArr != null && factoryFoldArr.length > 0) {
for(File factoryFold : factoryFoldArr) {
if(factoryFold.isDirectory() && Name().indexOf("svn") < 0) {//⼯⼚名称
File[] lineFoldArr = factoryFold.listFiles();
if(lineFoldArr != null && lineFoldArr.length > 0) {
for(File lineFold : lineFoldArr) {
if(lineFold.isDirectory() && Name().indexOf("svn") < 0) {//线别名称
String fileName = Name() + Name() + Name() + ".war";
File[] replaceFileArr = lineFold.listFiles();
if(replaceFileArr != null && replaceFileArr.length >= 2) {
boolean isFailure = false;
for(File replaceFile : replaceFileArr) {
if("application.properties".Name())) {
try {
InputStream inputStream = new FileInputStream(replaceFile);
String destFile = sourcePath + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "application.properties";
FileUtil.appendFile(pcVersion, destFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.AbsolutePath() + "⽂件读取失败.");
isFailure = true;
}
}
if("window-global.js".Name())) {
try {
InputStream inputStream = new FileInputStream(replaceFile);
e.printStackTrace();
System.out.AbsolutePath() + "⽂件读取失败.");
isFailure = true;
}
}
}
if(!isFailure) {//打包
generateLineWar(sourcePath, fileName);
}
}
}
}
}
}
}
}
}青衣道师
}
}
}
} else {
System.out.println("源⽬录不是⼀个⽬录.");
}
} else {
System.out.println("源⽬录不存在.");
}
System.out.println("打包结束");
}
/**
* ⽣产打包⽂件
* @param sourcePath
* @param fileName
*/
private static void generateLineWar(String sourcePath, String fileName) {
try {
DeCompressUtil.zip(fileName, sourcePath);
System.out.println(fileName + "⽂件⽣成成功.");
} catch (Exception e) {
System.out.println(fileName + "⽂件解压失败.");
e.printStackTrace();
}
}
}
六、把此程序放到tomcat下⾯当jenkis 重新编译的时候便会⽣成7个对应的war包包名就是⽣产线的名称防⽌拿错包。