使用ant构建有依赖库的项目
来源:互联网 发布:我国网络直播行业现状 编辑:程序博客网 时间:2024/05/22 06:50
我们来看看一个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>
需要理解的是
classpath指向的是ivy帮我们下载的lib目录classpathref="classpath"
<!-- 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>
1 0
- 使用ant构建有依赖库的项目
- ant 构建的依赖项目 kettle项目 源码导入eclipse
- 使用Ant构建项目
- 使用Ant构建简单的java项目
- 使用Ant构建简单项目
- 使用ant构建web项目
- 使用Ant构建java项目
- 使用Ant构建简单项目
- 用Ant实现Java项目的自动构建和部署/Eclipse下使用Ant
- Ant构建工具的使用
- 使用Ant编译和构建项目指南
- 图解使用Ant构建一个Java项目
- 使用ant自动化构建android项目
- Ant—使用Ant构建简单Java项目(一)
- Ant—使用Ant构建简单Java项目(二)
- Ant—使用Ant构建简单Java项目(三)
- 使用maven构建项目和管理依赖
- 用Ant来构建你的项目
- Python编程之禅-- import this
- Android 项目结构说明
- 科大讯飞杯题目
- 02-makefile隐含规则
- 不满足
- 使用ant构建有依赖库的项目
- 我的caffe学习之路<第十三天>
- 一个快速获取/更新 Let's encrypt 证书的 shell script
- JavaScript重点总结
- Linux 精准获取进程pid
- mysql必知必会 - 进入mysql后了解全局(2)
- 【android源码】编译android M源码、刷机,开启源码学习的First Step
- springMvc&Mysql上传到远程服务器连接超时问题
- opencv矩阵的基本运算