package Testlink;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/*
* 此方法主要是用于执行DOS命令
*/
public class DOSUtil {
// 定义执行的进程
private Process process;
// 定义工作目录
private File workspaceFile;
// 定义工作目录下的所有文件和文件夹
private File dirFile;
// 定义buildXML文件夹
private File buildXMLDirectory;
// 工作目录路径
private String workspacePath;
// build.xml文件路径
private String buildXMLDirectoryPath;
// ant执行build.xml文件
private String antCommand;
/**
* 该方法主要用于执行ANT的build.xml文件
*/
public void RunDOS() {
// 获取当前地址的路径
workspaceFile = new File("");
workspacePath = workspaceFile.getAbsolutePath();
// 获取路径下的所有文件
dirFile = new File(workspacePath);
File[] files = dirFile.listFiles();
// 获取BuildXML文件夹的路径
buildXMLDirectoryPath = (workspacePath + "\\BuildXML");
// 获取BuildXML文件下的所有文件
buildXMLDirectory = new File(buildXMLDirectoryPath);
File[] xmlFiles = buildXMLDirectory.listFiles();
// 定义需要执行的ant命令
antCommand = ("cmd.exe /c ant -buildfile " + this.buildXMLDirectoryPath + "\\build.xml");
// 判断工作目录是否为空,如果不为空就向下执行
if (files.length != 0) {
// 循环遍历是否存在BuildXML文件夹
File[] arrayOfFile1;
int j = (arrayOfFile1 = files).length;
for (int i = 0; i < j; i++) {
File file = arrayOfFile1[i];
if (file.getName().equals("BuildXML")) {
// 判断BuildXML文件是否为空,不为空向下执行
if (xmlFiles.length != 0) {
File[] arrayOfFile2;
int m = (arrayOfFile2 = xmlFiles).length;
for (int k = 0; k < m; k++) {
File xmlfile = arrayOfFile2[k];
if (xmlfile.getName().equals("build.xml")) {
try {
process = Runtime.getRuntime().exec(antCommand);
InputStream iStream = process.getInputStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
String str = null;
while ((str = bReader.readLine()) != null) {
System.out.println(str);
}
System.out.println("我的已经执行了");
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("build.xml文件不存在");
}
}
} else {
System.out.println("BuildXML文件夹为空");
}
}
}
} else {
System.out.println("文件内容为空");
}
}
}