以前都是用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;
- /**
- * 类说明: 分析.classpath文件的jar引用,并copy到war
- * 创建时间: 2007-10-31 下午02:23:42
- * @author Seraph
- * @email: seraph115@gmail.com
- */
- 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的变量,而工程所引用的依赖都是通过此变量添加到工程中的
<?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.