使用ant实现自动化示例
来源:互联网 发布:天津seo公司 编辑:程序博客网 时间:2024/04/29 16:06
ant是一个自动化工具,使用Java语言编写,跨平台。
这里有两个Java工具类,现在使用ant实现自动化和可配置。
Java实现文件分割
package linchaolong.tools.file;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;// 文件分割工具public class FileSplitter { //缓冲区大小(单位:字节) private static final int BUFF_SIZE = 1024; //文件列表配置文件名称 public static final String DEFAULT_CFG_NAME = "file_list.cfg"; /** 分割文件 * @param filePath 文件路径 * @param blockSize 块大小 * @param exportDir 文件导出目录 * @return 是否分割成功 */ public static boolean split(String filePath, int blockSize, String exportDir) { File file = new File(filePath); if (!file.exists()) { throw new RuntimeException("文件不存在 : " + filePath); } if (blockSize <= 0) { throw new RuntimeException("blockSize不可以小于等于 0"); } // 初始化导出目录 File dir = new File(exportDir); if (!dir.exists()) { dir.mkdirs(); } // 文件分割个数 double count = Math.ceil(file.length() / (double) blockSize); if (count < 1) { count = 1; } BufferedInputStream bis = null; BufferedOutputStream bos = null; BufferedWriter listWriter = null; try { // 文件读取流 bis = new BufferedInputStream( new FileInputStream(file)); // 文件列表配置文件 File listFile = new File(dir, DEFAULT_CFG_NAME); if (listFile.exists()) { listFile.delete(); } listWriter = new BufferedWriter(new FileWriter( listFile)); // 缓冲区 byte buff[] = new byte[BUFF_SIZE]; int len = -1; int offset = 0; File exprotFile = null; // 读取要分割的文件,并分为count个块写到文件 for (int i = 1; i <= count; i++) { exprotFile = new File(dir, file.getName() + "-" + i); if (exprotFile.exists()) { exprotFile.delete(); } bos = new BufferedOutputStream(new FileOutputStream(exprotFile)); int total = 0; // 当前导出块的大小 while (total < blockSize) { offset = BUFF_SIZE; // 如果超出块大小 if ((total + BUFF_SIZE) > blockSize) { // 只读取不超出的部分 offset = BUFF_SIZE - ((total + BUFF_SIZE) - blockSize); } len = bis.read(buff, 0, offset); if (len != -1) { bos.write(buff, 0, len); total += len; } else { break; } } // 写配置 listWriter.write(exprotFile.getName()); listWriter.newLine(); bos.close(); bos = null; } } catch (FileNotFoundException e) { throw new RuntimeException("文件不存在 : " + filePath); } catch (IOException e) { e.printStackTrace(); return false; }finally{ // 释放资源 if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } bis = null; } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } bos = null; } if (listWriter != null) { try { listWriter.close(); } catch (IOException e) { e.printStackTrace(); } listWriter = null; } } return true; }}
Java实现文件合并
package linchaolong.tools.file;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;// 文件合并工具public class FileMerger { // 缓冲区大小 private static final int BUFF_SIZE = 4096; /** 合并文件 * @param dir 文件目录,读取该目录下默认文件列表配置,文件列表默认配置文件名称参考FileSplitter.DEFAULT_CFG_NAME常量 * @param exportPath 文件导出路径 * @return 是否合并成功 */ public static boolean merge(String dir, String exportPath) { return merge(dir, FileSplitter.DEFAULT_CFG_NAME, exportPath); } /** 合并文件 * @param dir 文件目录 * @param listCfgName 文件列表配置名称 * @param exportPath 文件导出路径 * @return 是否合并成功 */ public static boolean merge(String dir, String listCfgName, String exportPath) { try { File baseDir = new File(dir); File cfgFile = new File(baseDir, listCfgName); // 判断配置文件是否存在 if (!cfgFile.exists()) { throw new FileNotFoundException(cfgFile.getPath()); } // 读取文件列表配置 BufferedReader cfgReader = new BufferedReader(new FileReader( cfgFile)); List<String> fileList = new ArrayList<String>(); String fileName = null; while (true) { fileName = cfgReader.readLine(); if (fileName != null) { fileList.add(new File(baseDir,fileName).getPath()); } else { break; } } cfgReader.close(); return merge(fileList, exportPath); } catch (FileNotFoundException e) { throw new RuntimeException("file not exist : " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); return false; } } /** 合并文件 * @param fileList 文件列表 * @param exportPath 文件导出路径 * @return 是否合并成功 */ public static boolean merge(List<String> fileList, String exportPath) { // 判断导出文件路径是否是一个文件 File exportFile = new File(exportPath); if (exportFile.isDirectory()) { throw new RuntimeException("export path is not a file : " + exportPath); } // 合并文件 BufferedOutputStream out = null; FileInputStream in = null; try { out = new BufferedOutputStream( new FileOutputStream(exportFile)); File file = null; byte buff[] = new byte[BUFF_SIZE]; int len = 0; for (String path : fileList) { file = new File(path); if (!file.exists()) { throw new FileNotFoundException(file.getPath()); } in = new FileInputStream(file); while (true) { len = in.read(buff); if (len == -1) { break; } out.write(buff, 0, len); } in.close(); in = null; } } catch (FileNotFoundException e) { throw new RuntimeException("file not exist : " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); return false; }finally{ // 释放资源 if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } out = null; } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } in = null; } } return true; }}
使用ant实现自动化
1.自定义Task
新建一个类继承Task,并重写execute方法,该方法是自定义功能执行入口。并生成相关setter方法,用于在配置中通过反射调用setter方法把相关数据填充到对象中。
package linchaolong.ant.task;import linchaolong.tools.file.FileSplitter;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Task;// 文件分割public class FileSplit extends Task { // 要分割的 文件 private String file; // 导出目录 private String dir; // 块大小 private int size; @Override public void execute() throws BuildException { super.execute(); // 配置检查 configCheck(); // 分割 FileSplitter.split(file, size, dir); } // 检查配置是否正确 private void configCheck() { if (file == null || file.isEmpty()) { throw new BuildException("attribute file is empty"); } if (dir == null || dir.isEmpty()) { throw new BuildException("attribute exportDir is empty"); } } // setter public void setFile(String file) { this.file = file; } public void setDir(String dir) { this.dir = dir; } public void setSize(int size) { this.size = size; }}
2.配置关联,导出jar。
这一步可参考【ant使用总结(四):扩展ant库】中的导出和使用自定义库。
3.使用示例
<project name="test" default="run"> <!-- 使用自定义库 --> <typedef resource="tags.cfg"> <!-- 解析tags.cfg并建立关联 --> <classpath> <pathelement location="./linchaolong_ant.jar" /> <!-- jar文件路径 --> </classpath> </typedef> <!-- 分割文件 命令: ant split -f file.xml --> <target name="split"> <filesplit file="../AntCustomLibrary.zip" dir="./export" size="1048576"></filesplit> <echo>文件分割成功!</echo> </target> <!-- 合并文件 命令: ant merge -f file.xml --> <target name="merge"> <filemerge dir="./export" export="./merge.zip"></filemerge> <echo>文件合并成功!</echo> </target></project>
相关文章:Ant使用总结
项目地址:https://coding.net/u/linchaolong/p/AntCustomLibrary/git
0 0
- 使用ant实现自动化示例
- 使用ANT实现自动化测试
- ant实现自动化部署
- Ant使用示例
- CruiseControl于ant实现自动化
- Apache Ant 实现自动化部署
- ANT实现自动化软件部署
- android使用ant自动化打包
- ant自动化使用和技巧
- GitHub Ant-Design/ant-init 使用示例
- Ant+jmeter 实现自动化性能测试
- Ant+Jmeter+Jenkins实现接口测试自动化
- Ant+Jmeter+Jenkins实现接口测试自动化
- Ant+Jmeter+Jenkins实现接口测试自动化
- 使用Ant与批处理命令实现java程序自动化版本构建
- 使用 Ant 编写自动化的 telnet 脚本
- 使用Ant与Junit进行自动化测试
- Apache Ant自动化工具的使用
- 关于虚拟主机类网站优化的一些测试实验
- Maven实战——Maven入门
- C编写Windows服务程序学习
- JavaScript-JavaScript 语句\
- ZjDroid--脱壳神器介绍
- 使用ant实现自动化示例
- 矩阵中求子矩形
- popupWindow焦点问题以及实现根据输入内容筛选列表功能
- 【PAT 乙级(Basic Level)】A+B和C
- nodejs npm包管理常用命令介绍
- SpringMVC REST ful API
- ViewDragHelper详解
- Unite 2015 大会总结
- Json字符串转JsonNode