Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为Ant build配置Ivy_Java_Ant_Ivy_Build.xml_Taskdef - Fatal编程技术网

Java 如何为Ant build配置Ivy

Java 如何为Ant build配置Ivy,java,ant,ivy,build.xml,taskdef,Java,Ant,Ivy,Build.xml,Taskdef,我目前有ANT\u HOME位于/HOME//ANT/1.8.4/ANT-1.8.4 我刚下载。我把它提取到/home//ivy/2.3.0-rc1/ivy-2.3.0-rc1 然后我将/home//ivy/2.3.0-rc1/ivy-2.3.0-rc1/lib/*.jar复制到ANT_home/lib。如果我对Ant如何使用插件/扩展的理解是正确的,那么Ant现在应该能够在运行时访问Ivy的所有任务 我的下一个问题是,如何在Ant构建文件中定义Ivy任务?假设我想使用ivy检索,ivy解析和i

我目前有
ANT\u HOME
位于
/HOME//ANT/1.8.4/ANT-1.8.4

我刚下载。我把它提取到
/home//ivy/2.3.0-rc1/ivy-2.3.0-rc1

然后我将
/home//ivy/2.3.0-rc1/ivy-2.3.0-rc1/lib/*.jar
复制到
ANT_home/lib
。如果我对Ant如何使用插件/扩展的理解是正确的,那么Ant现在应该能够在运行时访问Ivy的所有任务


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

首先,您必须定义一个指向常春藤任务的

<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>
顺便说一下,这个
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中嵌入了一个默认的
ivysettings.xml
文件,该文件将指向world wide Maven存储库系统。如果您没有公司范围的Maven存储库,则可以使用默认的
ivysettings.xml
文件:

<ivy:settings/>
<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>
这意味着我需要
log4j.jar
(修订版1.2.17)来编译(以及编译测试),我需要
junit.jar
(修订版4.10)来编译测试代码

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

我的
编译
配置来自哪里?我在我的
ivy.xml
中定义了它。有十种标准配置。这也会进入你的
ivy.xml
文件:

<ivy:settings/>
<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>

因此,我们有以下几点:

  • 如何在
    build.xml
    中使用
    将Ivy Ant任务合并到构建中
  • 如何使用Ivy Ant任务配置Ivy
  • 如何使用
    读入
    ivy.xml
    文件并解析第三方jar依赖关系
  • 现在,您可能想实际使用这些jar文件。有三种方法可以做到这一点:

     <ivy:cachepath pathid="main.classpath" conf="compile"/>
    
    在本例中,它将创建一个refid为
    compile.fileset
    的文件集

    有时您必须将JAR带到项目中。例如,如果您创建war或ear文件,您希望将JAR封装起来。在这种情况下,您可以使用以下方法:

     <ivy:cachefileset setid="compile.fileset" conf="compile"/>
    
    <property name="lib.dir" value="${target.dir}/lib"/>
    <ivy:retrieve pattern="${lib.dir}/[artifact].[ext]"
         conf="runtime"/>
    
    
    
    这将把JAR提取到
    ${lib.dir}
    目录中,因此您可以将它们包含在WAR或EAR中


    很抱歉回答得太长了,但有很多步骤要讲。我强烈推荐曼宁的书,这本书有一整章都是关于常春藤的。

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

    <project ..... xmlns:ivy="antlib:org.apache.ivy.ant">
    
    
    
    要了解更多细节,我建议阅读有关如何工作的文章

    下面的答案提供了更多的“设置常春藤”建议:


    这是迄今为止我问过或遇到的任何与常春藤有关的问题的最全面的答案,我希望我能再多投一次票。感谢您在这里花时间帮助我,非常完美。一个快速跟进问题(如果我可以的话):我确实计划拥有自己的托管存储库,可能由Artifactory管理。每当我与Ivy合作时,我都会看到我的同事将他们的
    conf
    属性定义为
    conf=“*->*”
    conf=compile->default
    ,但似乎没有人清楚地了解它们的作用以及原因……您提到它是本地常春藤配置(
    Ivy.xml
    )与其解析所针对的存储库之间的映射,您甚至慷慨地展示了
    编译
    配置可能是什么样子(相对于
    test
    config)。但是在存储库端(“箭头”操作符的右侧)呢?“
    *->*
    ”映射到什么?关于“
    默认值”
    ”呢?只是好奇,因为无论我读了多少次Ivy文档,我似乎都无法理解。再次感谢您的帮助!Ivy中没有标准配置。您必须在
    Ivy.xml
    文件中定义配置。我的帖子中提到的标准配置映射到Maven范围。我可以在
    文件中定义单个配置>或者我可以添加上面没有提到的内容。例如,有些人在
    ivy.xml中为axis2和WSDL文件添加了一个
    生成源
    配置。问题是你必须将你的配置映射到repos的配置。我从来没有尝试过
    *->*
    。我猜它映射了你的数据库中的所有配置
    ivy.xml`适用于所有存储库配置。我必须试一试。2015年,这仍然是关于ivy ant任务的最全面、最清晰的教程。如果ivy.jar位于ant_home/lib中,这是正确而简单的答案
    <ivy:resolve/>
    
     <ivy:cachepath pathid="main.classpath" conf="compile"/>
    
     <ivy:cachefileset setid="compile.fileset" conf="compile"/>
    
    <property name="lib.dir" value="${target.dir}/lib"/>
    <ivy:retrieve pattern="${lib.dir}/[artifact].[ext]"
         conf="runtime"/>
    
    <project ..... xmlns:ivy="antlib:org.apache.ivy.ant">