简体   繁体   English

如何为Ant构建配置Ivy

[英]How to configure Ivy for Ant build

I currently have ANT_HOME located at /home/<myuser>/ant/1.8.4/ant-1.8.4 . 我目前在/home/<myuser>/ant/1.8.4/ant-1.8.4ANT_HOME

I just downloaded the Apache Ivy tarball that includes its dependencies . 我刚刚下载了包含其依赖项的Apache Ivy tarball I extracted it to /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

I then copied /home/<myuser>/ivy/2.3.0-rc1/ivy-2.3.0-rc1/lib/*.jar to ANT_HOME/lib . 然后我将/home/<myuser>/ivy/2.3.0-rc1/ivy-2.3.0-rc1/lib/*.jar复制到ANT_HOME/lib If my understanding of how Ant works with plugins/extensions is correct, then Ant should now be able to access all of Ivy's tasks at runtime. 如果我理解Ant如何使用插件/扩展是正确的,那么Ant现在应该能够在运行时访问所有Ivy的任务。

My next question is, how do I define Ivy tasks inside my Ant buildfile? 我的下一个问题是,如何在我的Ant构建文件中定义常春藤任务? Say I want to use ivy-retrieve , ivy-resolve and ivy-publish tasks. 说我想使用ivy-retrieveivy-resolveivy-publish任务。 What are all the configurations I need to do (in the XML) to get these tasks working when I run my Ant build from the command-line (I will not be building through the Ant-Eclipse plugin). 当我从命令行运行Ant构建时,我需要做的所有配置(在XML中)以使这些任务正常工作(我不会通过Ant-Eclipse插件构建)。 Thanks in advance! 提前致谢!

First, you have to define a <taskdef> to point to the Ivy tasks. 首先,您必须定义<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>

That will give you access to the Ivy tasks. 这将使您可以访问常春藤任务。 You'd use these tasks like this: 你可以像这样使用这些任务:

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

The problem is that your Ivy tasks names might clash with other Ant tasks. 问题是您的常春藤任务名称可能与其他Ant任务冲突。 For example, there's an Ivy task <report> . 例如,有一个常春藤任务<report> To solve this, you can create an Ivy namespace. 要解决此问题,您可以创建Ivy命名空间。 To do that, you put a reference in your namespace in the <project> entity like this: 为此,您在<project>实体的命名空间中放置一个引用,如下所示:

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

Now, when you define the Ivy tasks, you can use that antlib:org.apache.ivy.ant reference to your ivy namespace. 现在,当您定义常春藤任务时,您可以使用对您的ivy命名空间的antlib:org.apache.ivy.ant引用。 Same taskdef as before, but with a uri field: 与之前相同的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>

By the way, there's nothing special about that uri . 顺便说一句,这个uri没什么特别之处。 I could have done this: 我本可以这样做的:

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

The point is now you can prefix your task names with ivy: . 关键是现在你可以用ivy:你的任务名称添加前缀ivy: Instead of this: 而不是这个:

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

You can now do this: 你现在可以这样做:

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

And that's how you gain access to your Ivy Ant tasks. 这就是你如何获得对Ivy Ant任务的访问权限。

Now, you have access to your Ivy Ant tasks, you need to define an ivysettings.xml file and use the <ivy:settings/> task to point there: 现在,您可以访问常春藤Ant任务,您需要定义一个ivysettings.xml文件并使用<ivy:settings/>任务指向:

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

There is a default ivysettings.xml file embedded in Ivy that will point you to the world wide Maven repository system. 在Ivy中嵌入了一个默认的ivysettings.xml文件,它将指向全球范围的Maven存储库系统。 If you don't have a company wide Maven repository, then you can use the default ivysettings.xml file: 如果您没有公司范围的Maven存储库,则可以使用默认的ivysettings.xml文件:

<ivy:settings/>

That's pretty simple. 这很简单。

Once you've done that, you need to read in and resolve your ivy.xml file which usually sits in the root of your project in the same directory as your build.xml file. 完成后,您需要读入并解析 ivy.xml文件,该文件通常位于项目的根目录中,与build.xml文件位于同一目录中。

Basically, your ivy.xml file contains references to the third party jars you want to bring into your project. 基本上,您的ivy.xml文件包含对要引入项目的第三方jar的引用。 For example: 例如:

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

What this is saying is that I need the log4j.jar (revision 1.2.17) for compilation (and for compiling tests too) and I need junit.jar (revision.4.10) for compilation of my test code. 这是说我需要log4j.jar (修订版1.2.17)进行编译(以及编译测试),我需要junit.jar (revision.4.10)来编译我的测试代码。

The compile->default is a mapping of my compile configuration to Maven's default configuration (which says I just want the Jar and any other jars that it might depend upon. compile->default是我的compile配置到Maven的default配置的映射(这表示我只想要Jar和它可能依赖的任何其他jar。

Where's does my compile configuration come from? 我的compile配置来自哪里? I define it in my ivy.xml . 我在我的ivy.xml定义它。 There are ten standard configurations. 有十种标准配置。 This also goes into your ivy.xml file: 这也会进入你的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>

You can use any configuration name you want, but these map to the default Maven configurations and are widely used. 您可以使用所需的任何配置名称,但这些名称映射到默认的Maven配置并且被广泛使用。

Once you have your ivy.xml file defined, you can use <ivy.resolve> to resolve your dependencies: 定义了ivy.xml文件后,可以使用<ivy.resolve>来解决依赖关系:

<ivy:resolve/>

So, we have the following: 所以,我们有以下内容:

  1. How to use <taskdef> in your build.xml to incorporate the Ivy Ant tasks into your build. 如何在build.xml使用<taskdef>将Ivy Ant任务合并到构建中。
  2. How to use the Ivy Ant task <ivy:settings> to configure Ivy. 如何使用常春藤Ant任务<ivy:settings>来配置Ivy。
  3. How to use <ivy:resolve/> to read in your ivy.xml file and resolve your third party jar dependencies. 如何使用<ivy:resolve/>读取ivy.xml文件并解析第三方jar依赖项。

Now, you probably want to actually use those jar files. 现在,您可能希望实际使用这些jar文件。 There are three ways to do this: 有三种方法可以做到这一点:

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

The <ivy:cachepath/> task will create a classpath (in this case called main.classpath ) that points to the jars you have in your ivy.xml file's compile configuration. <ivy:cachepath/>任务将创建一个类路径(在本例中称为main.classpath ),它指向您在ivy.xml文件的compile配置中拥有的jar。 This is used most of the time. 这在大多数时候都在使用。

If you need a fileset, you can use this: 如果您需要文件集,可以使用:

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

In this case, it will create a fileset with a refid of compile.fileset . 在这种情况下,它将创建一个带有compile.fileset的refid的文件集。

Sometimes you have to bring the jars into your project. 有时您必须将罐子带入您的项目。 For example, if you create a war or ear file, you want to enclose your jars. 例如,如果您创建一个war或ear文件,则要封装您的jar。 In that case, you can use this: 在这种情况下,您可以使用:

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

That will fetch your jars into the ${lib.dir} directory, so you can include them in wars or ears. 这将把你的罐子提取到${lib.dir}目录中,这样你就可以将它们包含在战争或耳中。

Sorry for the long answer, but there are a lot of steps to cover. 对不起,答案很长,但有很多步骤需要解决。 I highly recommend Manning's book Ant in Action which has a whole chapter on Ivy. 我强烈推荐Manning的书“ Ant in Action” ,其中有一章关于Ivy。

David gave a very fine answer, but I'd like to point out that the taskdef is not required. 大卫给出了一个非常好的答案,但我想指出taskdef不是必需的。 Provided the ivy.jar is in the expected location the namespace declaration at the top of the ANT file is enough: 如果ivy.jar位于预期位置,则ANT文件顶部的名称空间声明就足够了:

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

For more detail I'd recommend reading about how ANT libs work. 有关更多详细信息,我建议您阅读ANT库如何工作。

The following answer provides some more "setting up ivy" advice: 以下答案提供了一些“设置常春藤”的建议:

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

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