繁体   English   中英

如何为Ant构建配置Ivy

[英]How to configure Ivy for Ant build

我目前在/home/<myuser>/ant/1.8.4/ant-1.8.4ANT_HOME

我刚刚下载了包含其依赖项的Apache Ivy tarball 我把它/home/<myuser>/ivy/2.3.0-rc1/ivy-2.3.0-rc1压缩到/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/*.jar复制到ANT_HOME/lib 如果我理解Ant如何使用插件/扩展是正确的,那么Ant现在应该能够在运行时访问所有Ivy的任务。

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

首先,您必须定义<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>

这将使您可以访问常春藤任务。 你可以像这样使用这些任务:

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

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

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

现在,当您定义常春藤任务时,您可以使用对您的ivy命名空间的antlib:org.apache.ivy.ant引用。 与之前相同的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:你的任务名称添加前缀ivy: 而不是这个:

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

你现在可以这样做:

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

这就是你如何获得对Ivy Ant任务的访问权限。

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

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

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

<ivy:settings/>

这很简单。

完成后,您需要读入并解析 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. 如何在build.xml使用<taskdef>将Ivy Ant任务合并到构建中。
  2. 如何使用常春藤Ant任务<ivy:settings>来配置Ivy。
  3. 如何使用<ivy:resolve/>读取ivy.xml文件并解析第三方jar依赖项。

现在,您可能希望实际使用这些jar文件。 有三种方法可以做到这一点:

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

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

如果您需要文件集,可以使用:

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

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

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

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

这将把你的罐子提取到${lib.dir}目录中,这样你就可以将它们包含在战争或耳中。

对不起,答案很长,但有很多步骤需要解决。 我强烈推荐Manning的书“ Ant in Action” ,其中有一章关于Ivy。

大卫给出了一个非常好的答案,但我想指出taskdef不是必需的。 如果ivy.jar位于预期位置,则ANT文件顶部的名称空间声明就足够了:

<project ..... xmlns:ivy="antlib:org.apache.ivy.ant">

有关更多详细信息,我建议您阅读ANT库如何工作。

以下答案提供了一些“设置常春藤”的建议:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM