importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;import java.io.*;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;/***@author*@versionMavenUtil,*/
public classMavenUtil {private static final Logger LOGGER = LoggerFactory.getLogger(MavenUtil.class);private final String WEB_INF_PATH = "./";//MavenUtil.class.getClassLoader().getResource("../").getPath();
private final String COMPILE_PATH = WEB_INF_PATH + "sourceCode/";private final String EMBEDDED_MAVEN_PATH = WEB_INF_PATH + "maven/";private final List fileList = new ArrayList();public voidmaven(String path) {
List getPoms =findPomFile(path);boolean flag =isJacoco(getPoms);
List fileList =getModuleList(getPoms);for(String pom: fileList) {
ProcessBuilder pb= newProcessBuilder();//构建命令
if (System.getProperty("os.name").contains("Windows")) {if(flag) {
pb.command("cmd", "/c", "mvn", "clean", "test", "compile", "-f", pom);
}else{
pb.command("cmd", "/c", "mvn", "-DskipTests=true", "clean", "compile", "-f",
pom);
}
}else if (System.getProperty("os.name").contains("Linux")) {if(flag) {
pb.command("mvn", "clean", "test", "compile", "-f", pom);
}else{
pb.command("mvn", "-DskipTests=true", "clean", "compile", "-f", pom);
}
}else{
LOGGER.info("Unknown System..." + System.getProperty("os.name"));
}try{//执行命令
Process process =pb.start();
InputStream inputStream=process.getInputStream();byte[] buffer = new byte[1024];intreadSize;while ((readSize = inputStream.read(buffer)) > 0) {
System.out.write(buffer,0, readSize);
}
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}/*** 查找所有pom文件
*@parampath
*@return
*/
private ListfindPomFile(String path){
File file= newFile(path);if(file.exists()) {
File[] files=file.listFiles();if (null == files || files.length == 0) {
System.out.println("文件夹是空的!");returnfileList;
}else{for(File file2 : files) {if (file2.isDirectory() && ! file2.getName().contains(".git")&& ! file2.getName().contains("src")) {//此处是文件夹
findPomFile(file2.getAbsolutePath());
}else{if (file2.getName().contains("pom.xml")) {
fileList.add(file2.getAbsolutePath());
System.out.println("文件:" +file2.getAbsolutePath());
}
}
}
}
}else{
System.out.println("文件不存在!");
}returnfileList;
}/*** 判断是否存在多模块,如果存在将会去除存在模块的pom文件
*@paramfromPoms
*@return
*/
private List getModuleList(ListfromPoms) {int count =fromPoms.size();for(String getPom: fromPoms) {final SAXReader reader = newSAXReader();
Document document= null;try{
document=reader.read(getPom);final List modules = document.getRootElement().element("modules").elements();if (modules != null) {for (finalObject module : modules) {final String moduleName =((Element) module).getText();
Iterator iterator =fromPoms.iterator();while(iterator.hasNext()) {
String pom=iterator.next();if(pom.contains(moduleName)) {
iterator.remove();
count--;
}
}
}
}if (count <= 1){break;
}
}catch (finalDocumentException e) {
e.printStackTrace();
}catch(NullPointerException e) {
System.out.print(getPom);
}
}returnfromPoms;
}/*** 读取文件,判断读取的行中是否存在jacoco字符串
*@parampoms
*@return
*/
private boolean isJacoco(Listpoms){boolean flag = false;
BufferedReader reader= null;try{for(String getPom: poms){
File pomFile= newFile(getPom);
reader= new BufferedReader(newFileReader(pomFile));
String tempString= null;while ((tempString = reader.readLine()) != null) {if (tempString.contains("org.jacoco")) {
flag= true;break;
}
}
reader.close();if (flag) { break; }
}
}catch(IOException e) {
e.printStackTrace();
}finally{if (reader != null) {try{
reader.close();
}catch(IOException e1) {
}
}
}returnflag;
}/*** 修改权限(这里粗犷的修改为777,如有精细化的权限控制,自己调整一下)
* 因为一些原因,虽然线上默认的执行用户是root,并且权限为rwx,依然会报权限不足的错误
* 如果有大神指导原因请指点一二
*@throwsException*/
public void afterPropertiesSet() throwsException {//修改maven目录权限
Process chmodMaven = new ProcessBuilder("chmod", "-R", "777", EMBEDDED_MAVEN_PATH).start();//等待完成
chmodMaven.waitFor();
LOGGER.info("修改权限完成:{}", EMBEDDED_MAVEN_PATH);//修改编译目录权限
Process chmodCompile = new ProcessBuilder("chmod", "-R", "777", COMPILE_PATH).start();
chmodCompile.waitFor();
LOGGER.info("修改权限完成:{}", COMPILE_PATH);
}