我想向我的 Ant 项目添加依赖项;例如,我想将 hibernate 依赖项添加到我的项目中。
我是 Ant 新手。之前我使用maven工具来构建项目。
在 Maven 中,向 pom.xml
文件添加依赖项非常容易。
我的 build.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<project name="Demo ANT Project-1" default="run">
<target name="run" depends="compile">
<java classname="com.company.product.RoundTest">
<classpath path="staging"/>
</java>
</target>
<target name="compile">
<javac includeantruntime="false" srcdir="./src" destdir="staging" />
</target>
</project>
我想添加对上述 Ant xml 文件的依赖项。
最佳答案
PLEASE NOTE: This question was asked and answered newly 6 years ago.
首先,Ant 比 Maven 更老,因此不包括对依赖管理的核心支持。
添加 Ivy
ivy是Ant的依赖管理框架
要启用它,您需要做两件事。首先将 ivy 任务命名空间包含到构建文件的顶部:
<project .... xmlns:ivy="antlib:org.apache.ivy.ant">
其次,您需要将 ivy jar 安装到 ANT 用于其第 3 方扩展的标准位置之一:
- $ANT_HOME/lib
- $HOME/.ant/lib
我喜欢使我的构建独立,因此包含一个自动为我执行此操作的目标:
<available classname="org.apache.ivy.Main" property="ivy.installed"/>
<target name="install-ivy" description="Install ivy" unless="ivy.installed">
<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.3.0/ivy-2.3.0.jar"/>
<fail message="Ivy has been installed. Run the build again"/>
</target>
使用 Ivy
这是一个非常广泛的主题,以下是下载 hibernate jar 及其依赖项的简单示例:
<target name="resolve" depends="install-ivy" description="Use ivy to resolve classpaths">
<ivy:cachepath pathid="compile.path">
<dependency org="org.hibernate" name="hibernate" rev="3.2.7.ga" conf="default">
<exclude org="javax.transaction"/>
</dependency>
</ivy:cachepath>
</target>
产生以下输出:
resolve:
[ivy:cachepath] :: Apache Ivy 2.3.0 - 20130110142753 :: http://ant.apache.org/ivy/ ::
[ivy:cachepath] :: loading settings :: url = jar:file:/home/mark/.ant/lib/ivy.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:cachepath] :: resolving dependencies :: #;working@mark
[ivy:cachepath] confs: [default]
[ivy:cachepath] found org.hibernate#hibernate;3.2.7.ga in public
[ivy:cachepath] found net.sf.ehcache#ehcache;1.2.3 in public
[ivy:cachepath] found commons-logging#commons-logging;1.0.4 in public
[ivy:cachepath] found asm#asm-attrs;1.5.3 in public
[ivy:cachepath] found dom4j#dom4j;1.6.1 in public
[ivy:cachepath] found antlr#antlr;2.7.6 in public
[ivy:cachepath] found cglib#cglib;2.1_3 in public
[ivy:cachepath] found asm#asm;1.5.3 in public
[ivy:cachepath] found commons-collections#commons-collections;2.1.1 in public
[ivy:cachepath] :: resolution report :: resolve 373ms :: artifacts dl 10ms
[ivy:cachepath] :: evicted modules:
[ivy:cachepath] commons-collections#commons-collections;2.1 by [commons-collections#commons-collections;2.1.1] in [default]
---------------------------------------------------------------------
| | modules || artifacts |
| conf | number| search|dwnlded|evicted|| number|dwnlded|
---------------------------------------------------------------------
| default | 10 | 0 | 0 | 1 || 9 | 0 |
---------------------------------------------------------------------
这个ivy管理的类路径可以在你的javac任务中使用
<javac includeantruntime="false" srcdir="./src" destdir="staging" classpathref="compile.path"/>
关于ant - 如何给Ant项目添加依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26650590/