IAm*_*aja 16 java ant build.xml ivy taskdef
我目前ANT_HOME找到了/home/<myuser>/ant/1.8.4/ant-1.8.4.
我刚刚下载了包含其依赖项的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/lib/*.jar到ANT_HOME/lib.如果我理解Ant如何使用插件/扩展是正确的,那么Ant现在应该能够在运行时访问所有Ivy的任务.
我的下一个问题是,如何在我的Ant构建文件中定义常春藤任务?说我想用ivy-retrieve,ivy-resolve和ivy-publish任务.当我从命令行运行Ant构建时,我需要做的所有配置(在XML中)以使这些任务正常工作(我不会通过Ant-Eclipse插件构建).提前致谢!
Dav*_* W. 60
首先,您必须定义一个<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>
Run Code Online (Sandbox Code Playgroud)
这将使您可以访问常春藤任务.你可以像这样使用这些任务:
<cachepath pathid="main.classpath" conf="compile"/>
Run Code Online (Sandbox Code Playgroud)
问题是您的常春藤任务名称可能与其他Ant任务冲突.例如,有一个常春藤任务<report>.要解决此问题,您可以创建Ivy命名空间.为此,您在<project>实体中的命名空间中放置一个引用,如下所示:
<project name="my.proj" default="package" basedir="."
xmlns:ivy="antlib:org.apache.ivy.ant"/>
Run Code Online (Sandbox Code Playgroud)
现在,在定义常春藤任务时,可以使用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>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这没什么特别的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>
Run Code Online (Sandbox Code Playgroud)
现在,您可以在任务名称前加上前缀ivy:.而不是这个:
<cachepath pathid="main.classpath" conf="compile"/>
Run Code Online (Sandbox Code Playgroud)
你现在可以这样做:
<ivy:cachepath pathid="main.classpath" conf="compile"/>
Run Code Online (Sandbox Code Playgroud)
这就是你如何获得对Ivy Ant任务的访问权限.
现在,您可以访问Ivy Ant任务,您需要定义一个ivysettings.xml文件并使用该<ivy:settings/>任务指向那里:
<ivy:settings file="${ivy.home}/ivysettings.xml"/>
Run Code Online (Sandbox Code Playgroud)
ivysettings.xmlIvy中嵌入了一个默认文件,可以指向全球范围的Maven存储库系统.如果您没有公司范围的Maven存储库,则可以使用默认ivysettings.xml文件:
<ivy:settings/>
Run Code Online (Sandbox Code Playgroud)
这很简单.
一旦你做到了这一点,你需要阅读和解决您的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>
Run Code Online (Sandbox Code Playgroud)
这就是说我需要log4j.jar(修订版1.2.17)进行编译(以及编译测试),我需要junit.jar(修订版4.1.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>
Run Code Online (Sandbox Code Playgroud)
您可以使用所需的任何配置名称,但这些名称映射到默认的Maven配置并且被广泛使用.
一旦你有你的ivy.xml文件中定义,你可以使用<ivy.resolve>解决您的依赖关系:
<ivy:resolve/>
Run Code Online (Sandbox Code Playgroud)
所以,我们有以下内容:
<taskdef>在您build.xml的构建中使用Ivy Ant任务.<ivy:settings>配置Ivy.<ivy:resolve/>来读取您的ivy.xml文件并解决您的第三方jar依赖项.现在,您可能想要实际使用这些jar文件.有三种方法可以做到这一点:
<ivy:cachepath pathid="main.classpath" conf="compile"/>
Run Code Online (Sandbox Code Playgroud)
该<ivy:cachepath/>任务将创建一个类路径(在本例中称为main.classpath),它指向ivy.xml文件compile配置中的jar .这在大多数时候都在使用.
如果您需要文件集,可以使用:
<ivy:cachefileset setid="compile.fileset" conf="compile"/>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它将创建一个refid为的文件集compile.fileset.
有时您必须将罐子带入您的项目.例如,如果您创建一个war或ear文件,则要封装您的jar.在这种情况下,您可以使用:
<property name="lib.dir" value="${target.dir}/lib"/>
<ivy:retrieve pattern="${lib.dir}/[artifact].[ext]"
conf="runtime"/>
Run Code Online (Sandbox Code Playgroud)
这会将你的罐子拿到${lib.dir}目录中,所以你可以将它们包含在战争或耳朵中.
对不起,答案很长,但有很多步骤需要解决.我强烈推荐Manning的书" Ant in Action",其中有一章关于Ivy.
Mar*_*nor 13
大卫给出了一个非常好的答案,但我想指出taskdef不是必需的.如果ivy.jar位于预期位置,则ANT文件顶部的名称空间声明就足够了:
<project ..... xmlns:ivy="antlib:org.apache.ivy.ant">
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,我建议您阅读ANT库如何工作.
以下答案提供了一些"设置常春藤"的建议: