使用ant构建有依赖库的项目

    xiaoxiao2021-12-14  27

    我们来看看一个build脚本

    <project xmlns:ivy="antlib:org.apache.ivy.ant" name="dateUtilsProject" default="main" basedir="."> <description> Create a Java Project (JAR) with Ant build script </description> <property name="projectName" value="DateUtils" /> <property name="src.dir" location="src" /> <property name="build.dir" location="bin" /> <property name="dist.dir" location="dist" /> <property name="dist.lib.dir" location="dist/lib" /> <property name="lib.dir" value="lib" /> <property name="main-class" value="com.mkyong.core.utils.DateUtils" /> <!-- ivy start --> <!-- ivy to get dependencies and copy to project lib folder automatically --> <target name="resolve" description="retrieve dependencies with ivy"> <ivy:retrieve /> </target> <!-- install ivy --> <target name="ivy" description="Install ivy"> <mkdir dir="${user.home}/.ant/lib" /> <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0-rc1/ivy-2.4.0-rc1.jar" /> </target> <!-- ivy end --> <target name="init"> <mkdir dir="${build.dir}" /> </target> <!-- external libraries classpath, we don't need sources and javadoc --> <path id="classpath"> <fileset dir="${basedir}/"> <include name="${lib.dir}/*.jar" /> <exclude name="${lib.dir}/*sources.jar"/> <exclude name="${lib.dir}/*javadoc.jar"/> </fileset> </path> <!-- To work with external libraries, need classpath to compile --> <target name="compile" depends="init" description="compile the source "> <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" /> </target> <!-- constructs the external libraries classpath name --> <pathconvert property="classpath.name" pathsep=" "> <path refid="classpath" /> <mapper> <chainedmapper> <flattenmapper /> <globmapper from="*.jar" to="lib/*.jar" /> </chainedmapper> </mapper> </pathconvert> <target name="copy-dependencies"> <copy todir="${dist.lib.dir}"> <fileset dir="${lib.dir}" includes="**/*.jar" excludes="**/*sources.jar, **/*javadoc.jar" /> </copy> </target> <!-- jar it, and declares the ext libraries in manifest.mf file --> <target name="jar" depends="compile, copy-dependencies" description="package, output to JAR"> <echo message="classpath.name : ${classpath.name} " /> <mkdir dir="${dist.dir}" /> <mkdir dir="${dist.lib.dir}" /> <jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}" /> <attribute name="Class-Path" value="${classpath.name}" /> </manifest> </jar> </target> <target name="clean" description="clean up"> <delete dir="${build.dir}" /> <delete dir="${dist.dir}" /> </target> <!-- Default, run this --> <target name="main" depends="clean, compile, jar" /> </project> 看起来很复杂,但还是比较简单的, 虽然在此之前我没学过ant, 现在也是看着build脚本学习的, 以后真正在项目使用ant还是需要去看doc的

    首先我们看看

    <!-- ivy start --> <!-- ivy to get dependencies and copy to project lib folder automatically --> <target name="resolve" description="retrieve dependencies with ivy"> <ivy:retrieve /> </target>我们在resolve中添加了ivy的检索, ivy就会

    <!-- install ivy --> <target name="ivy" description="Install ivy"> <mkdir dir="${user.home}/.ant/lib" /> <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0-rc1/ivy-2.4.0-rc1.jar" /> </target> <!-- ivy end --> 接着执行clean, compile <!-- To work with external libraries, need classpath to compile --> <target name="compile" depends="init" description="compile the source "> <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" /> </target> 需要理解的是 classpathref="classpath" classpath指向的是ivy帮我们下载的lib目录

    <!-- external libraries classpath, we don't need sources and javadoc --> <path id="classpath"> <fileset dir="${basedir}/"> <include name="${lib.dir}/*.jar" /> <exclude name="${lib.dir}/*sources.jar"/> <exclude name="${lib.dir}/*javadoc.jar"/> </fileset> </path> 上面中我们的classpath指向ivy下载lib,是没问题的,当是我们程序打包时是需要这些文件的, 所以我们需要把这些文件copy到dist中 <target name="copy-dependencies"> <copy todir="${dist.lib.dir}"> <fileset dir="${lib.dir}" includes="**/*.jar" excludes="**/*sources.jar, **/*javadoc.jar" /> </copy> </target> 最后打包时我们需要在清单文件( MANIFEST.MF)添加lib的路径 <jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}" /> <attribute name="Class-Path" value="${classpath.name}" /> </manifest> </jar>

    转载请注明原文地址: https://ju.6miu.com/read-968998.html

    最新回复(0)