问题
I would like to add dependency to my Ant project; for example I want to add hibernate dependency to my project.
I'm new to Ant. Before I used maven tool to build project.
in maven it is very easy to add dependency to pom.xml
file.
My build.xml file
<?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>
I want to add dependency to above Ant xml file.
回答1:
First of all Ant is older than Maven and therefore does not include core support for dependency management.
Adding ivy
Ivy is a dependency management framework for Ant
http://ant.apache.org/ivy/
To enable it you need to do two things. First include the ivy task namespace to the top of your build file:
<project .... xmlns:ivy="antlib:org.apache.ivy.ant">
A secondly you'll need to install the ivy jar into one of the standard locations that ANT uses for it's 3rd party extensions:
- $ANT_HOME/lib
- $HOME/.ant/lib
I like to make my builds standalone so include a target that does this for me automatically:
<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>
Using ivy
This is a very extensive subject, the following is a simple example to download the hibernate jar and it's dependencies:
<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>
Produces the following output:
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 |
---------------------------------------------------------------------
This ivy managed classpath can then be used in your javac task
<javac includeantruntime="false" srcdir="./src" destdir="staging" classpathref="compile.path"/>
回答2:
Or simply put everything in one ant target ?
<target name="update-lib-dir">
<property name="ivy.install.version" value="2.4.0"/>
<property name="ivy.jar.file" value="lib-ant/ivy-${ivy.install.version}.jar"/>
<property name="lib-ivy.dir" value="lib-ivy"/>
<get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}" usetimestamp="true" />
<taskdef resource="org/apache/ivy/ant/antlib.xml">
<classpath>
<pathelement location="${ivy.jar.file}"/>
</classpath>
</taskdef>
<!-- define your destination directory here -->
<retrieve pattern="${lib-ivy.dir}/[type]/[artifact]-[revision].[ext]" sync="true">
<!-- Add all your maven dependencies here -->
<dependency org="com.twelvemonkeys.imageio" name="imageio-jpeg" rev="3.4.2"/>
<dependency org="com.twelvemonkeys.imageio" name="imageio-tiff" rev="3.4.2"/>
</retrieve>
</target>
Note : Create your ivy library directory before. In this example, you should have at least a directory named lib-ant
.
Hope it helps :o)
来源:https://stackoverflow.com/questions/26650590/how-to-add-dependency-to-ant-project