2012-09-11 69 views
14

我目前有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如何配置Ivy for Ant版本

然后我复制/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中)都可以使这些任务正常工作。提前致谢!

回答

56

首先,您必须定义<taskdef>以指向常春藤任务。

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

现在,当你定义常春藤任务,您可以使用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"/> 

这就是你如何获取你的常春藤Ant任务。现在

,您可以访问您的常青藤Ant任务,你需要定义一个ivysettings.xml文件并使用<ivy:settings/>任务点有:

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

有嵌入常春藤默认ivysettings.xml文件,该文件将指向你到世界各地的Maven仓库系统。如果你没有一个公司范围内的Maven仓库,那么你可以使用默认ivysettings.xml文件:

<ivy:settings/> 

这很简单。

完成之后,您需要阅读resolve文件,该文件通常位于项目的根目录中,与您的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配置(说我只是想罐,它可能依赖于其他任何广口瓶的映射。

在哪里的呢我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配置的d被广泛使用。

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

<ivy:resolve/> 

所以,我们有以下几点:

  1. 如何使用<taskdef>build.xml将常青藤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文件的compile配置有罐子。这在大部分时间被使用。

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

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

在这种情况下,它会创建一个文件集与compile.fileset一个REFID。

有时你必须把罐子放进你的项目中。例如,如果你创建一个战争或耳朵文件,你想要附上你的罐子。在这种情况下,你可以使用这个:

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

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

对不起,很长的回答,但有很多步骤来覆盖。我强烈推荐Manning的书Ant in Action,其中有关于常春藤的全章。

+3

这是迄今为止最全面的答案,任何常春藤相关的问题,我问过或已到来对,我希望我能更多地赞扬它。感谢您花时间帮助我,这非常完美。一个快速后续问题(如果我可以):我打算拥有自己的托管存储库,可能由Artifactory管理。每当我和Ivy一起工作时,我都看到我的同事们将'conf'属性定义为'conf =“* - > *”'或'conf = compile-> default',但似乎没有人清楚理解这些做什么,以及为什么...... – IAmYourFaja

+0

你提到它是本地Ivy配置('ivy.xml')和它解决的存储库之间的映射,而且你甚至足以显示什么是“编译”配置可能看起来像(与'测试'配置)。但是在存储库端(“箭头”运算符的右侧)呢? “'* - > *'”映射到什么?那么“default”呢?只是好奇,因为无论我读了多少次常春藤文档,我似乎都无法将它包围。再次感谢你的帮助! – IAmYourFaja

+0

常春藤没有标准配置。你必须在你的'ivy.xml'文件中定义你的配置。我的帖子中提到的标准版本映射到Maven _scopes_。我可以在我的'ivy.xml'中定义一个配置,或者我可以添加上面没有提到的配置。例如,有些人在'ivy.xml中为axis2和WSDL文件添加了'generated-sources'配置。事情是你必须将你的配置映射到repos的配置。我从来没有试过'* - > *'。我想它会将'ivy.xml'中的所有配置映射到所有存储库配置。我必须试一试。 –

12

David给出了非常好的答案,但我想指出taskdef不是必需的。 倘若ivy.jar是在预期位置在ANT文件的顶部空间声明是不够的:

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

更多的细节我建议你阅读有关如何ANT libs工作。

以下的答案提供了更多的一些“建立常青藤”的建议:

+1

最全面和最清晰的教程这是正确和简单的答案,如果ivy.jar是在ant_home/lib – oers