批量生成dependencies (maven   pom.xml) 源代碼

/****************************************************************

本類旨在批量生成pom.xml中dependencies 構件, 這些構件內代表的jar都是本項目lib下的jar包

*****************************************************************/

package com.gaopengju.maven;


import java.io.File;

import java.io.FileWriter;

import java.io.FilenameFilter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


import org.apache.log4j.Logger;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.dom.DOMElement;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;



public class ReadJARForMaven {


public final static Logger logger = Logger.getLogger(ReadJARForMaven.class);


public final static String COMMON_PATH= "${project.basedir}/WebRoot/WEB-INF/lib/";


public final static String FLAG = "/fglib/";


public final static String VERISION = "1.0.0";


public final static String SYSTEM = "system";





/*<groupId>com.sk.basecode</groupId>

<artifactId>portal-basecode</artifactId>

<version>1.0</version>

<scope>system</scope>

<systemPath>${project.basedir}/WebRoot/WEB-INF/lib/portalbasecode.jar</systemPath> */


/**

* 內容

* @param list

* @return

*/

public Document getNewDc(List<String> list, String path){


//創建新document

Document dc = DocumentHelper.createDocument();

//Element root = dc.getRootElement();

Element root = new DOMElement("dependencies");

dc.add(root);


for (int i = 0; i < list.size(); i++) {


Element el = new DOMElement("dependency");


Element groupId = new DOMElement("groupId");

groupId.addText(list.get(i));

Element artifactId = new DOMElement("artifactId");

artifactId.addText(list.get(i));

Element version = new DOMElement("version");

version.addText(VERISION);

Element scope = new DOMElement("scope");

scope.addText(SYSTEM);

Element systemPath = new DOMElement("systemPath");

systemPath.addText(COMMON_PATH+list.get(i));


//添加節點

el.add(groupId);

el.add(artifactId);

el.add(version);

el.add(scope);

el.add(systemPath);


root.add(el);


//logger.info(el.getData());

}


//將document 寫到文件中取

try {

//寫入之前檢查是否存在該文件,存在則刪除

if(new File(path).exists()){

new File(path).delete();

}


XMLWriter xmlw = new XMLWriter(new FileWriter(new File(path)));

xmlw.write(dc);

xmlw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



return dc;

}


/**

* xml 源

* @param dc

* @return

*/

public List<String> contentList(Document dc){


List<String> list = new ArrayList<String>();


Element root = dc.getRootElement();

for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {

Element child = (Element) iterator.next();

Attribute attribute = child.attribute("path");

String value = attribute.getValue();

if(value != null && value.startsWith(FLAG)){

list.add(value.substring(FLAG.length()));

}else{

continue;

}

}


return list;

}



/**

* 通過路徑獲取源

* @param path

* @return

*/

public Document getSrcDc(String path){


File file = new File(path);

if(!file.exists()){

try {

file.createNewFile();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

logger.info(path+"文件不存在");

try {

throw new Exception();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


SAXReader reader = new SAXReader();

Document dc = null;

try {

dc = reader.read(file);

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return dc;

}


//直接讀取lib路徑

public List<String> getJarNameByPath(String path){

List<String> tempList = new ArrayList<String>();

File file = new File(path);

if(file.isDirectory()){

String[] list = file.list();

for (int i = 0; i < list.length; i++) {

if(list[i].endsWith("jar")){

System.out.println(list[i]);

tempList.add(list[i]);

}

}

}else{

System.err.println("指定文件路徑錯誤,該路徑對應的不是一個文件目錄");

}

return tempList;

}


public static void main(String[] args) {

ReadJARForMaven rx = new ReadJARForMaven();


//第一種:讀取項目的.classpath 文件

Document dc = rx.getSrcDc("D:\\Workspaces\\gpj_workspace1\\hzcart\\.classpath");


List<String> contentList = rx.contentList(dc);

//第二種: 讀取項目lib目錄

//List<String> contentList = rx.getJarNameByPath("D:\\Workspaces\\gpj_workspace1\\hzcart\\WebRoot\\WEB-INF\\lib");

//生成文件路徑

String newFilePath = "D:\\Workspaces\\gpj_workspace1\\mavenUtil\\maven.xml";

System.out.println("開始時間:"+System.currentTimeMillis());

rx.getNewDc(contentList, newFilePath);

System.out.println("結束時間:"+System.currentTimeMillis());


}


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章