如何向Ant项目添加依赖项 [英] How to add dependency to Ant project

查看:774
本文介绍了如何向Ant项目添加依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Ant项目中添加依赖项;例如,我想将休眠依赖项添加到我的项目中.

I would like to add dependency to my Ant project; for example I want to add hibernate dependency to my project.

我是Ant的新手.在使用maven工具构建项目之前. 在maven中,将依赖项添加到pom.xml文件非常容易.

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.

我的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文件中添加依赖项.

I want to add dependency to above Ant xml file.

推荐答案

请注意:这个问题是6年前新问和回答的.

PLEASE NOTE: This question was asked and answered newly 6 years ago.

首先,Ant早于Maven,因此不包括对依赖项管理的核心支持.

First of all Ant is older than Maven and therefore does not include core support for dependency management.

Ivy是Ant的依赖项管理框架

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">

第二,您需要将常春藤罐子安装到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>

使用常春藤

这是一个非常广泛的主题,下面是一个下载hibernate jar及其依赖项的简单示例:

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>

产生以下输出:

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   |
    ---------------------------------------------------------------------

然后可以在您的javac任务中使用该常春藤托管的类路径

This ivy managed classpath can then be used in your javac task

<javac includeantruntime="false" srcdir="./src" destdir="staging" classpathref="compile.path"/>

这篇关于如何向Ant项目添加依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆