之前的转的帖子经过验证发现还是有点问题的,所以打算重写一遍!
==========================================================================
maven的jar包依赖管理真的很好用,但是又喜欢ant的直接和灵活,现在有了Maven Ant Tasks可以获取maven的依赖关系并用ant进行build,非常好
先介绍下maven ant task的主页:http://maven.apache.org/ant-tasks/
目前的版本是2.1.3,可支持maven3!
==========================================================================
一、配置环境
将下载的maven-ant-tasks-2.1.3.jar扔到ANT_HOME\lib下
如果想在eclipse中进行build还需如下配置
先选择自己的ANT_HOME,(我没用eclipse自带的)
再选择Add Extralnal Jars,maven-ant-tasks-2.1.3.jar所在路径
配置完后就可以在build.xml中使用Run -> Ant Build
==========================================================================
二、build.xml编写
琢磨了不少时间,成果如下
<?xml version="1.0" encoding="utf-8"?> <project name="test" basedir="." default="build" xmlns:artifact="antlib:org.apache.maven.artifact.ant"> <!-- 工程根目录 --> <property name="project" value="." /> <!-- 工程名 --> <property name="project.name" value="myproject" /> <!-- 版本 --> <property name="version" value="1.0.0" /> <property name="src" value="${project}/src/main/java" /> <property name="test" value="${project}/src/test/java" /> <property name="build" value="${project}/build" /> <property name="build.class" value="${project}/build/class" /> <property name="build.lib" value="${project}/build/lib" /> <!-- 结合maven进行编译、打包、复制依赖的jar --> <target name="maven-build"> <path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" /> <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" /> <artifact:pom id="maven.project" file="pom.xml" /> <artifact:dependencies pathId="maven.classpath" filesetid="maven.fileset"> <pom refid="maven.project" /> </artifact:dependencies> <!-- 编译 --> <javac srcdir="${src}" destdir="${build.class}" classpathref="maven.classpath" /> <!-- 打包 --> <jar jarfile="${build.lib}/${project.name}-${version}.jar" basedir="${build.class}"> <include name="org/myproject/**" /> </jar> <!-- 复制依赖的jar --> <copy todir="${build.lib}"> <fileset refid="maven.fileset" /> <mapper type="flatten" /> </copy> </target> <!-- 清理 --> <target name="clean"> <delete dir="${build}" /> <mkdir dir="${build.class}" /> <mkdir dir="${build.lib}" /> </target> <!-- 入口 --> <target name="build"> <antcall target="clean" /> <antcall target="maven-build" /> </target> </project>