【问题标题】:How to add dependency to Ant project如何为 Ant 项目添加依赖项
【发布时间】:2020-02-26 17:47:51
【问题描述】:

我想为我的 Ant 项目添加依赖项;例如我想在我的项目中添加休眠依赖。

我是 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 文件添加依赖。

【问题讨论】:

标签: ant


【解决方案1】:

请注意:这个问题是 6 年前提出并新回答的。

首先,Ant 比 Maven 更老,因此不包括对依赖管理的核心支持。

添加常春藤

Ivy 是 Ant 的依赖管理框架

http://ant.apache.org/ivy/

要启用它,您需要做两件事。首先将 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>

使用常春藤

这是一个非常广泛的主题,下面是一个下载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"/>

【讨论】:

  • 关于这个错误的任何想法.. D:\jijesh\workspace\Demo\build.xml:4: 问题:未能创建任务或键入 antlib:org.apache.ivy.ant:resolve 原因: 名称未定义。行动:检查拼写。行动:检查是否已声明任何自定义任务/类型。行动:检查任何 / 声明已经发生。此命名空间中尚未定义任何类型或任务
  • @jijeshAj ANT 缺少常春藤罐。这就是我的“install-ivy”目标的目的。如果常春藤类不存在,它会自动下载。
  • 我已经下载并复制到 ant/bin 和 users/.ant/lib 文件夹。仍然显示错误。
  • @jijeshAj ant/bin 不好。需要是 $ANT_HOME/lib。或 $HOME/.ant/lib。再次尝试运行我上面提供的“install-ivy”任务。 Ivy 就像任何其他 ANT 扩展一样,它的 jar 必须位于 ANT 在启动时可以找到的位置
  • 这里的代码示例使用的是 Ivy 2.3.0 版本。如果您今天使用该版本,您可能会收到错误消息“服务器错误:需要 HTTPS”。
【解决方案2】:

或者简单地将所有东西放在一个蚂蚁目标中?

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

注意:之前创建您的常春藤库目录。在此示例中,您至少应该有一个名为 lib-ant 的目录。

希望对你有帮助:o)

【讨论】:

    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2011-07-11
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多