java - 如何为 Ant 构建配置 Ivy

标签 java ant ivy build.xml taskdef

我目前有ANT_HOME位于 /home/<myuser>/ant/1.8.4/ant-1.8.4 .

我刚下载 the Apache Ivy tarball that includes its dependencies .我解压到/home/<myuser>/ivy/2.3.0-rc1/ivy-2.3.0-rc1 .

然后我复制了/home/<myuser>/ivy/2.3.0-rc1/ivy-2.3.0-rc1/lib/*.jarANT_HOME/lib .如果我对 Ant 如何与插件/扩展一起工作的理解是正确的,那么 Ant 现在应该能够在运行时访问 Ivy 的所有任务。

我的下一个问题是,如何在我的 Ant 构建文件中定义 Ivy 任务?假设我想使用 ivy-retrieve , ivy-resolveivy-publish任务。当我从命令行运行我的 Ant 构建时(我不会通过 Ant-Eclipse 插件构建),我需要做哪些配置(在 XML 中)才能使这些任务正常工作。提前致谢!

最佳答案

首先,你必须定义一个<taskdef>指向 Ivy 任务。

<property environment="env"/>
<property name="ivy.home" value="${env_IVY_HOME}"/>

<taskdef resource="org/apache/ivy/ant/antlib.xml">
    <classpath>
        <fileset dir="${ivy.home}">
            <include name="*.jar"/>
        </fileset>
    </classpath>
</taskdef>

这将使您能够访问 Ivy 任务。您将像这样使用这些任务:

<cachepath pathid="main.classpath" conf="compile"/>

问题是您的 Ivy 任务名称可能会与其他 Ant 任务冲突。例如,有一个 Ivy 任务 <report> .要解决这个问题,您可以创建一个 Ivy 命名空间。为此,您在 <project> 中的命名空间中放置一个引用像这样的实体:

<project name="my.proj" default="package" basedir="."
    xmlns:ivy="antlib:org.apache.ivy.ant"/>

现在,当您定义 Ivy 任务时,您可以使用 antlib:org.apache.ivy.ant引用你的ivy命名空间。与以前相同的 taskdef,但带有 uri领域:

<property environment="env"/>
<property name="ivy.home" value="${env_IVY_HOME}"/>

<taskdef resource="org/apache/ivy/ant/antlib.xml"
    uri="antlib:org.apache.ivy.ant">
    <classpath>
        <fileset dir="${ivy.home}">
            <include name="*.jar"/>
        </fileset>
    </classpath>
</taskdef>

顺便说一句,这没有什么特别的uri .我本可以这样做:

<project name="my.proj" default="package" basename="."
   xmlns:ivy="pastrami:with.mustard">

[...]
<taskdef resource="org/apache/ivy/ant/antlib.xml"
    uri="pastrami:with.mustard">
    <classpath>
        <fileset dir="${ivy.home}">
            <include name="*.jar"/>
        </fileset>
    </classpath>
</taskdef>

重点是现在您可以在任务名称前加上 ivy: 前缀.而不是这个:

<cachepath pathid="main.classpath" conf="compile"/>

您现在可以这样做:

<ivy:cachepath pathid="main.classpath" conf="compile"/>

这就是您访问 Ivy Ant 任务的方式。

现在,您可以访问您的 Ivy Ant 任务,您需要定义一个 ivysettings.xml文件并使用 <ivy:settings/>指向那里的任务:

 <ivy:settings file="${ivy.home}/ivysettings.xml"/>

有一个默认的ivysettings.xml嵌入在 Ivy 中的文件将指向世界范围的 Maven 存储库系统。如果您没有公司范围的 Maven 存储库,那么您可以使用默认的 ivysettings.xml文件:

<ivy:settings/>

这很简单。

完成后,您需要读入 resolve你的ivy.xml文件通常位于项目的根目录中,与 build.xml 位于同一目录中文件。

基本上,您的 ivy.xml文件包含对要引入项目的第三方 jar 的引用。例如:

<dependencies>
    <dependency org="log4j"  name="log4j" rev="1.2.17" conf="compile->default"/>
    <dependency org="junit"  name="junit" rev="4.10" conf="test->default"/>
</dependencies>

这就是说我需要 log4j.jar (修订版 1.2.17)用于编译(以及编译测试),我需要 junit.jar (revision.4.10) 用于编译我的测试代码。

compile->default是我的 compile 的映射配置到 Maven 的 default配置(它说我只想要 Jar 和它可能依赖的任何其他 jar。

我的 compile 在哪里?配置从何而来?我在我的 ivy.xml 中定义了它.有十种标准配置。这也会进入您的 ivy.xml文件:

<configurations>
  <conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/>
  <conf name="master" visibility="public" description="contains only the artifact published by this module itself, with no transitive dependencies"/>
  <conf name="compile" visibility="public" description="this is the default scope, used if none is specified. Compile dependencies are available in all classpaths."/>
  <conf name="provided" visibility="public" description="this is much like compile, but indicates you expect the JDK or a container to provide it. It is only available on the compilation classpath, and is not transitive."/>
  <conf name="runtime" visibility="public" description="this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath." extends="compile"/>
  <conf name="test" visibility="private" description="this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases." extends="runtime"/>
  <conf name="system" visibility="public" description="this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository."/>
  <conf name="sources" visibility="public" description="this configuration contains the source artifact of this module, if any."/>
  <conf name="javadoc" visibility="public" description="this configuration contains the javadoc artifact of this module, if any."/>
   <conf name="optional" visibility="public" description="contains all optional dependencies"/>
 </configurations>

您可以使用任何您想要的配置名称,但这些映射到默认的 Maven 配置并被广泛使用。

一旦你有了你的 ivy.xml文件定义,你可以使用<ivy.resolve>解决您的依赖关系:

<ivy:resolve/>

因此,我们有以下内容:

  1. 如何使用<taskdef>在你的build.xml将 Ivy Ant 任务合并到您的构建中。
  2. 如何使用 Ivy Ant 任务 <ivy:settings>配置 Ivy 。
  3. 如何使用<ivy:resolve/>阅读你的ivy.xml归档并解决您的第三方 jar 依赖项。

现在,您可能想要实际使用这些 jar 文件。可以通过三种方式做到这一点:

 <ivy:cachepath pathid="main.classpath" conf="compile"/>

<ivy:cachepath/>任务将创建一个类路径(在本例中称为 main.classpath),它指向您在 ivy.xml 中的 jar。文件的 compile配置。大多数时候都使用这种方法。

如果你需要一个文件集,你可以使用这个:

 <ivy:cachefileset setid="compile.fileset" conf="compile"/>

在这种情况下,它将创建一个 refid 为 compile.fileset 的文件集.

有时您必须将 jars 带入您的项目中。例如,如果您创建一个 war 或 ear 文件,您想要封装您的 jars。在这种情况下,您可以使用这个:

<property name="lib.dir" value="${target.dir}/lib"/>
<ivy:retrieve pattern="${lib.dir}/[artifact].[ext]"
     conf="runtime"/>

这会将您的 jar 提取到 ${lib.dir} 中目录,因此您可以将它们包含在 war 或耳朵中。

很抱歉回答很长,但是有很多步骤要涵盖。我强烈推荐曼宁的书 Ant in Action其中有一整章是关于 Ivy 的。

关于java - 如何为 Ant 构建配置 Ivy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12378892/

相关文章:

java - Ant 不可变属性变为可变

android - 如果存储库缺少任何标记,如何将 git 存储库声明为 gradle 依赖项?

java - Jasperreports 引擎 JRRuntimeException on report().show

java - 如何在java中动态更改jasper报告文本字段标记属性值

java - 如何在Android中创建音量流控件

java - 使用 ant 和 ivy 管理重复的 jar 文件

Ivy :<ivy:settings> vs. <ivy:configure>

java - 如何在java中从H2数据库映射id列

java - Ant 与 Mercurial

java - 将当前目录添加到 JAR 文件的类路径