由于好玩,本人写了一个增加打包程序,读者都知道,增量打包时,需要把修改的文件挑选出来,并按目录创建好,如果修改的文件还较少,还可以忍受,如果修改的文件超过一定量,那么确实是一件令人无聊,重复的工作。所以本人就写了一个增量打包程序,根据文件的最后修改时间提取修改的class文件,配置文件,属性文件等,并在根目录生成一个自述文件,记录提取的文件,以便核对,现在发布出来,希望能帮到你,配置文件Extract.properties,读者根据可以项目相关的情况修改。暂不支持内部类,如果有次需要,请自己修改程序。
PackgaeUtil类:
public class PackgaeUtil {
private Logger logger = Logger.getLogger(this.getClass());
private static final String EXTRACT_HOUR = "extract.hour";
private static final String EXTRACT_ITEM_DIR = "extract.item.dir";
private static final String EXTRACT_ITEM_PARENT = "extract.item.parent";
private static final String EXTRACT_SOURCE_PATH = "extract.source.path";
private static final String EXTRACT_SRC_PATH = "extract.src.path";
private static final String EXTRACT_CLASS_PATH = "extract.class.path";
private static final String EXTRACT_PACKAGE_PATH = "extract.package.dir";
private Config config = Config.getInstance();
private List<String> readmeList = new ArrayList<String>();
public static void main(String[] args) throws Exception {
PackgaeUtil packageUtil = new PackgaeUtil();
packageUtil.action();
}
/**
*
*/
private void action() {
boolean parent = Boolean.valueOf(config
.getProperty(EXTRACT_ITEM_PARENT));
File itemDir = new File(config.getProperty(EXTRACT_ITEM_DIR));
if (parent) {
File[] itemFiles = itemDir.listFiles(new ItemDirectoryFilter());
for (int i = 0; i < itemFiles.length; i++) {
// 提取需要打包的文件
extractPackageFiles(itemFiles[i]);
}
} else {
extractPackageFiles(itemDir);
}
writeReadmeFile();// 记录自述文件
}
/**
* 记录提取的文件至自述文件
*/
private void writeReadmeFile() {
String readmeFile = getPackagedDirectory() + File.separator
+ "readme.txt";
try {
Collections.sort(readmeList);
FileUtils.writeLines(new File(readmeFile), readmeList);
} catch (IOException e) {
logger.error(e.getMessage(),e);
}
}
/**
* 提取指定时间内被修改的文件,包括类文件,资源,配置文件,另外还有JSP,FTL文件
*
* @param itemFile
*/
private void extractPackageFiles(File itemFile) {
try {
File sourceFileDir = new File(itemFile.getAbsoluteFile()
+ File.separator + config.getProperty(EXTRACT_SOURCE_PATH));
List<String> sourceFilenameList = new ArrayList<String>();
String hour = config.getProperty(EXTRACT_HOUR);
getChangeSourceFile(sourceFilenameList, sourceFileDir, Integer
.valueOf(hour));
sourceFilenameList = getNeedPackageClassFile(sourceFilenameList);
packageSourceFile(itemFile.getName(), sourceFilenameList);
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
}
/**
* 打包源文件,包括类文件,资源,配置文件,另外还有JSP,FTL文件
*
* @param itemName
* @param sourceFilenameList
* @throws IOException
*/
private void packageSourceFile(String itemName,
List<String> sourceFilenameList) throws IOException {
if (sourceFilenameList.size() == 0) {
return;
}
String rootDir = getPackagedDirectory() + File.separator + itemName;
for (String sourceFilename : sourceFilenameList) {
String replaceStr = "";
if (sourceFilename.endsWith(".class")) {
replaceStr = "/target".replace("/", File.separator);
} else {
replaceStr = config.getProperty(EXTRACT_SOURCE_PATH).replace(
"/", File.separator);
}
int beginIndex = sourceFilename.indexOf(replaceStr)
+ replaceStr.length();
String destFile = rootDir + sourceFilename.substring(beginIndex);
readmeList.add(sourceFilename.substring(beginIndex));
createDestDirectory(destFile);
FileUtils.copyFile(new File(sourceFilename), new File(destFile));
}
}
/**
* 获取打包路径
*
* @param itemName
* @return
*/
private String getPackagedDirectory() {
StringBuffer packagedDir = new StringBuffer();
packagedDir.append(config.getProperty(EXTRACT_PACKAGE_PATH));
packagedDir.append(File.separator);
packagedDir.append(new File(config.getProperty(EXTRACT_ITEM_DIR))
.getName());
return packagedDir.toString().replace("/", File.separator);
}
/**
* 创建目标目录
*
* @param destFile
*/
private void createDestDirectory(String destFile) {
File destDir = new File(destFile).getParentFile();
if (!destDir.exists()) {
destDir.mkdirs();
}
}
/**
* 获取需要打包的类文件
*
* @param sourceFilenameList
* @return
*/
private List<String> getNeedPackageClassFile(List<String> sourceFilenameList) {
String sourceDir = config.getProperty(EXTRACT_SRC_PATH).replace("/",
File.separator);
String classDir = config.getProperty(EXTRACT_CLASS_PATH).replace("/",
File.separator);
List<String> classFilenameList = new ArrayList<String>();
for (String sourceFilename : sourceFilenameList) {
// 只有JAVA文件,才替换成class路径
if (sourceFilename.endsWith(".java")) {
// TODO 还解决内部类的问题
String classFileName = sourceFilename.replace(sourceDir,
classDir).replace(".java", ".class");
classFilenameList.add(classFileName);
continue;
}
classFilenameList.add(sourceFilename);
}
return classFilenameList;
}
/**
* 获取在指定时间之内发生变更的源文件
*
* @param sourceFilenameList
* @param sourceFileDir
* @param changeHours
* @throws IOException
*/
private void getChangeSourceFile(List<String> sourceFilenameList,
File sourceFileDir, int changeHours) throws IOException {
File[] sourceFiles = sourceFileDir.listFiles();
for (int i = 0; i < sourceFiles.length; i++) {
File sourceFile = sourceFiles[i];
// 如果是目录,递归寻找文件
if (sourceFile.isDirectory()) {
getChangeSourceFile(sourceFilenameList, sourceFile, changeHours);
}
long lastModified = sourceFile.lastModified();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(lastModified);
cal.add(Calendar.HOUR_OF_DAY, changeHours);
// 是否是文件,且最后修改日期是否在指定范围之内
if (sourceFile.isFile() && cal.getTime().after(new Date())) {
sourceFilenameList.add(sourceFile.getCanonicalPath());
}
}
}
private class ItemDirectoryFilter implements java.io.FileFilter {
@Override
public boolean accept(File pathname) {
if (!pathname.isDirectory()) {
return false;
}
if (pathname.getName().startsWith(".")) {
return false;
}
return true;
}
}
}
Config类:
public class Config {
private Logger logger = Logger.getLogger(this.getClass());
private Map<String, String> configs = new HashMap<String, String>();
private static Config config;
private Config() {
initConfig();
}
public static Config getInstance() {
if (config == null) {
config = new Config();
}
return config;
}
@SuppressWarnings("unchecked")
public void initConfig() {
Properties pro = new Properties();
try {
pro
.load(this.getClass().getResourceAsStream(
"/extract.properties"));
logger.debug("load extract.properties...");
Enumeration keys = pro.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = (String) pro.get(key);
logger.debug(key + ":" + value);
configs.put(key, (String) pro.get(key));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
public String getProperty(String key) {
String value = configs.get(key);
if (StringUtils.isEmpty(value)) {
return "";
}
return value;
}
}
属性文件extract.properties:
#提取多少小时内修改的文件
extract.hour = 30
#提取的项目目录
extract.item.dir = d:/work/mallplatform
#指定的项目目录是否是一个父项目
extract.item.parent = true
#源文件,资源文件,配置文件相对项目的路径
extract.source.path = /src/main
#java文件相对项目的路径
extract.src.path = /src/main/java
#class文件相对项目的路径
extract.class.path = /target/classes
#打包的路径
extract.package.dir = d:/package
maven依赖:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> </dependency>