本文共 3753 字,大约阅读时间需要 12 分钟。
以前都是用maven2做项目管理,但最近又用回了ant脚本执行打包,发现每次改变工程得jar依赖后打war包很麻烦,还得手工改变build.xml中的引用,所以为了偷懒就的相隔一劳永逸的办法,于是产生了如下偷懒的代码(可能技术进步都是因为我们想从无意义的手工劳动中解脱出来 )
1.扩展的ant类,分析classpath文件,替换变量M2_REPO以获得依赖jar的全路径,并执行copy
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import org.apache.tools.ant.BuildException;
- import org.apache.tools.ant.Project;
- import org.apache.tools.ant.Task;
- import org.apache.tools.ant.util.FileUtils;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
-
-
-
-
-
-
-
-
- public class AntPathTask extends Task {
-
- private static final String M2_REPO = "M2_REPO" ;
-
- private static final String ECLIPSE_DOM = "/classpath/classpathentry" ;
-
- private String libdir;
-
- private String repository;
-
- private String classpath;
-
- public String getLibdir() {
- return libdir;
- }
-
- public void setLibdir(String libdir) {
- this .libdir = libdir;
- }
-
- public String getRepository() {
- return repository;
- }
-
- public void setRepository(String repository) {
- this .repository = repository;
- }
-
- public String getClasspath() {
- return classpath;
- }
-
- public void setClasspath(String classpath) {
- this .classpath = classpath;
- }
-
- public Project getProject() {
- return super .getProject();
- }
-
- public void execute() throws BuildException {
-
- List list = getPathFromEclipseFile();
- copyJar(list);
- showPath(list);
- }
-
- public List getPathFromEclipseFile() {
-
- SAXReader saxReader = new SAXReader();
- Document document = null ;
- try {
- document = saxReader.read(new FileInputStream(classpath));
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- List list = document.selectNodes(ECLIPSE_DOM);
- List pathList = new ArrayList();
-
- int i = 0 ;
- for (Iterator iterator = list.iterator(); iterator.hasNext();) {
- Element element= (Element) iterator.next();
-
- String kind = element.attributeValue("kind" );
- if ( "var" .equals(kind)){
- String path = element.attributeValue("path" );
- path = path.replaceAll(M2_REPO, repository);
- pathList.add(path);
- i++;
- }
- }
- return pathList;
- }
-
- public void copyJar(List list) {
-
- FileUtils fileUtils = FileUtils.newFileUtils();
- for (Iterator iterator = list.iterator(); iterator.hasNext();) {
- String fileName = (String) iterator.next();
- String[] strs = null ;
- try {
- strs = fileName.split("/" );
- String destFile = libdir + "/" + strs[strs.length - 1 ];
- fileUtils.copyFile(fileName, destFile, null , true );
- } catch (FileNotFoundException e1) {
- log("指定的变量[M2_REPO]路径下找不到所需Jar,请配置正确路径。所需文件:"
- + strs[strs.length - 1 ], Project.MSG_ERR);
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- }
- }
-
- public void showPath(List list) {
-
- log("Find .classpath file path of eclipse is: \"" + classpath + "\"" );
- log("CRMS Project include jar files: " );
-
- for (Iterator iterator = list.iterator(); iterator.hasNext();) {
- String path = (String) iterator.next();
- log(path);
- }
- log("Total of jar files: " + list.size());
- }
- }
2.ant的build.xml文件,使用扩展的ant任务taskdef,其中M2_REPO是eclipse的变量,而工程所引用的依赖都是通过此变量添加到工程中的
3.在运行时需在eclipse的ant运行环境中,加入dom4j,saxpath及jaxen包的引用
4.好了可以看看屏幕上的输出了,war很快就打好了
PS: 开发Ant扩展时在Eclipse中的调试方法:
Run Configurations>Java Application
创建一个java application,Main选项卡中的Project项中选自己扩展Ant所在的项目,Main Class项中添入“org.apache.tools.ant.Main ”
在Arguments选项卡中的Project Arguments中添入“-f "[your build.xml path] [your task name] " ”
如:
-f "/Users/seraph/Workshop/workspace/ant-extending/build.xml" copy-jar”
最后:Run
God bless you.
转载地址:http://ufhdi.baihongyu.com/