`

扩展ant功能,自动分析eclipse文件制作war包

阅读更多

以前都是用maven2做项目管理,但最近又用回了ant脚本执行打包,发现每次改变工程得jar依赖后打war包很麻烦,还得手工改变build.xml中的引用,所以为了偷懒就的相隔一劳永逸的办法,于是产生了如下偷懒的代码(可能技术进步都是因为我们想从无意义的手工劳动中解脱出来

1.扩展的ant类,分析classpath文件,替换变量M2_REPO以获得依赖jar的全路径,并执行copy

java 代码
 
  1. import  java.io.FileInputStream;  
  2. import  java.io.FileNotFoundException;  
  3. import  java.io.IOException;  
  4. import  java.util.ArrayList;  
  5. import  java.util.Iterator;  
  6. import  java.util.List;  
  7.   
  8. import  org.apache.tools.ant.BuildException;  
  9. import  org.apache.tools.ant.Project;  
  10. import  org.apache.tools.ant.Task;  
  11. import  org.apache.tools.ant.util.FileUtils;  
  12. import  org.dom4j.Document;  
  13. import  org.dom4j.Element;  
  14. import  org.dom4j.io.SAXReader;  
  15.   
  16.   
  17. /**  
  18.  * 类说明: 分析.classpath文件的jar引用,并copy到war
     
  19.  * 创建时间: 2007-10-31 下午02:23:42
     
  20.  * @author Seraph
     
  21.  * @email: seraph115@gmail.com
     
  22.  */   
  23. public   class  AntPathTask  extends  Task {  
  24.       
  25.     private   static   final  String M2_REPO =  "M2_REPO" ;  
  26.       
  27.     private   static   final  String ECLIPSE_DOM =  "/classpath/classpathentry" ;  
  28.   
  29.     private  String libdir;  
  30.       
  31.     private  String repository;  
  32.       
  33.     private  String classpath;  
  34.   
  35.     public  String getLibdir() {  
  36.         return  libdir;  
  37.     }  
  38.   
  39.     public   void  setLibdir(String libdir) {  
  40.         this .libdir = libdir;  
  41.     }  
  42.   
  43.     public  String getRepository() {  
  44.         return  repository;  
  45.     }  
  46.   
  47.     public   void  setRepository(String repository) {  
  48.         this .repository = repository;  
  49.     }  
  50.   
  51.     public  String getClasspath() {  
  52.         return  classpath;  
  53.     }  
  54.   
  55.     public   void  setClasspath(String classpath) {  
  56.         this .classpath = classpath;  
  57.     }  
  58.       
  59.     public  Project getProject() {  
  60.         return   super .getProject();  
  61.     }  
  62.   
  63.     public   void  execute()  throws  BuildException {  
  64.           
  65.         List list = getPathFromEclipseFile();  
  66.         copyJar(list);  
  67.         showPath(list);  
  68.     }  
  69.       
  70.     public  List getPathFromEclipseFile() {  
  71.           
  72.         SAXReader saxReader = new  SAXReader();  
  73.         Document document = null ;  
  74.         try  {  
  75.             document = saxReader.read(new  FileInputStream(classpath));  
  76.         } catch  (Exception e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.   
  80.         List list = document.selectNodes(ECLIPSE_DOM);  
  81.         List pathList = new  ArrayList();  
  82.           
  83.         int  i =  0 ;  
  84.         for  (Iterator iterator = list.iterator(); iterator.hasNext();) {  
  85.              Element element= (Element) iterator.next();   
  86.                
  87.              String kind = element.attributeValue("kind" );  
  88.              if ( "var" .equals(kind)){  
  89.                  String path = element.attributeValue("path" );  
  90.                  path = path.replaceAll(M2_REPO, repository);  
  91.                  pathList.add(path);  
  92.                  i++;  
  93.              }  
  94.         }  
  95.         return  pathList;  
  96.     }  
  97.       
  98.     public   void  copyJar(List list) {  
  99.           
  100.         FileUtils fileUtils = FileUtils.newFileUtils();  
  101.         for  (Iterator iterator = list.iterator(); iterator.hasNext();) {  
  102.             String fileName = (String) iterator.next();  
  103.             String[] strs = null ;  
  104.             try  {  
  105.                 strs = fileName.split("/" );  
  106.                 String destFile = libdir + "/"  + strs[strs.length -  1 ];  
  107.                 fileUtils.copyFile(fileName, destFile, null true );  
  108.             } catch  (FileNotFoundException e1) {  
  109.                 log("指定的变量[M2_REPO]路径下找不到所需Jar,请配置正确路径。所需文件:"   
  110.                         + strs[strs.length - 1 ], Project.MSG_ERR);  
  111.             } catch  (IOException e2) {  
  112.                 e2.printStackTrace();  
  113.             }  
  114.         }  
  115.     }  
  116.   
  117.     public   void  showPath(List list) {  
  118.       
  119.         log("Find .classpath file path of eclipse is: \""  + classpath +  "\"" );  
  120.         log("CRMS Project include jar files: " );  
  121.   
  122.         for  (Iterator iterator = list.iterator(); iterator.hasNext();) {  
  123.             String path = (String) iterator.next();  
  124.             log(path);  
  125.         }  
  126.         log("Total of jar files: "  + list.size());  
  127.     }  
  128. }  


2.ant的build.xml文件,使用扩展的ant任务taskdef,其中M2_REPO是eclipse的变量,而工程所引用的依赖都是通过此变量添加到工程中的

 

<?xml version="1.0" encoding="UTF-8"?>
<project name="crms" default="clean-jar" basedir=".">

	<property name="M2_REPO" value="D:/repository" />
	<property name="abator.dir" value="${M2_REPO}/abator/abator/1.0.0" />
	<property name="ant-extend.dir" value="${M2_REPO}/ant-extending/ant-extending/1.0.0" />
	<property name="build.dir" location="target/classes" />
	<property name="webapp.dir" value="src/main/webapp" />
	<property name="resources.dir" value="src/main/resources" />
	<property name="classpath.dir" location=".classpath" />
	<property name="lib.dir" value="${webapp.dir}/WEB-INF/lib" />
	<property name="generated.source.dir" value="${basedir}" />

	<!-- - - - - - - - - - - - - - - - - - 
          import: Ant> Runtime> Global Entries> dom4j1.6; jaxen1.1;
         - - - - - - - - - - - - - - - - - -->
	<taskdef name="createPath" classname="com.seraph.extending.AntPathTask" classpath="${ant-extend.dir}/ant-extending-1.0.0.jar" />

	<!-- - - - - - - - - - - - - - - - - - 
          target: 通过自动分析.classpath文件中的引用,拷贝jar包
         - - - - - - - - - - - - - - - - - -->
	<target name="copy-jar">
		<createPath libdir="${lib.dir}" repository="${M2_REPO}" classpath="${classpath.dir}" />
	</target>

	<target name="create-war" depends="copy-jar">
		<war destfile="crms.war" webxml="${webapp.dir}/WEB-INF/web.xml">
			<fileset dir="${webapp.dir}" />
			<classes dir="${build.dir}" />
		</war>
	</target>

	<target name="clean-jar" depends="create-war">
		<delete dir="${lib.dir}" />
	</target>

	<!-- - - - - - - - - - - - - - - - - - 
          target: Abator ORM 生成器
         - - - - - - - - - - - - - - - - - -->
	<target name="abator" description="Generate the ibatis files">
		<taskdef name="abator"
	                classname="org.apache.ibatis.abator.ant.AbatorAntTask"
	                classpath="${abator.dir}/abator-1.0.0.jar" />
		<abator overwrite="flase" configfile="${resources.dir}/abatorConfig.xml" verbose="false" >
			<propertyset>
				<propertyref name="generated.source.dir"/>
			</propertyset>
		</abator>
	</target>

</project>
 

 

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.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics