我正在尝试编写一个 Ant 构建,它不需要我将 Ant 插件添加到 Ant 的 lib 目录,或 /home/myuser/.ant/lib
,或者在我的 Eclipse 实例的 ant home 中,等等;也就是说,因为我最终将在托管的 Jenkins 服务器上构建我的项目,而我无法访问系统的 Ant 安装。
我称其为“自引导”构建,因为我使用 Ivy 在构建时下拉我的 Ant 插件,并希望通过一些适当的配置,使它们的任务动态地可供 Ant 使用。
我的构建的要点(以 ant-contrib 插件为例:
<?xml version="1.0" encoding="utf-8" ?>
<project name="myapp" default="audit" basedir="."
xmlns:ivy="antlib:org.apache.ivy.ant"
xmlns:antcontrib="antlib:net.sf.antcontrib">
<!-- Build path. -->
<path id="build.path">
<fileset dir="${lib.buildtime.dir}" includes="**/*.jar"/>
</path>
<target name="bootstrap">
<taskdef resource="org/apache/ivy/ant/antlib.xml"
uri="antlib:org.apache.ivy.ant" classpathref="build.path"/>
</target>
<target name="resolve" depends="bootstrap">
<ivy:settings url="${ivy.settings.home}"/>
<ivy:cleancache/>
<ivy:resolve file="${ivy.xml}"/>
<ivy:retrieve pattern="${gen.lib.main.dir}/[artifact]-[revision].[ext]" conf="main"/>
<ivy:retrieve pattern="${gen.lib.test.dir}/[artifact]-[revision].[ext]" conf="test"/>
<ivy:retrieve pattern="${gen.lib.buildtime.dir}/[artifact]-[revision].[ext]" conf="buildtime"/>
<ivy:report todir="${gen.staging.dir}" />
<ivy:cachepath pathid="build.path" conf="buildtime"/>
</target>
<target name="taskdefs" depends="resolve">
<taskdef resource="/net/sf/antcontrib/antlib.xml"
uri="antlib:net.sf.antcontrib" classpathref="build.path"/>
<property name="fizz" value="buzz" />
<antcontrib:if>
<antcontrib:equals arg1="${fizz}" arg2="buzz" />
<antcontrib:then>
<echo message="Fizz is buzz!" />
</antcontrib:then>
<antcontrib:else>
<echo message="Fizz is not buzz!" />
</antcontrib:else>
</antcontrib:if>
</target>
</project>
当我运行 taskdefs
目标时,我没有在 Ant 输出中看到回显的“Fizz is humb!”消息,而是收到以下错误:
BUILD FAILED
/home/myuser/eclipse/workspace/myapp/build.xml:169: Problem: failed to create task or type antlib:net.sf.antcontrib:if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet
This appears to be an antlib declaration.
Action: Check that the implementing library exists in one of:
-/home/myuser/eclipse/plugins/org.apache.ant_1.8.3.v201301120609/lib
-/home/myuser/.ant/lib
-a directory added on the command line with the -lib argument
我想要做的事情(避免必须做上述 3 件推荐的事情中的一件)是不可能的吗?如果是这样,为什么?如果不是的话,我这里的设置有什么问题吗?提前致谢!
最佳答案
我通常创建一个“boostrap”目标并使用它将ivy安装到“$HOME/.ant/lib”目录中。请参阅:
以下是一个更完整的示例,可以实现您想要执行的操作:
总之,很遗憾 ivy 没有默认与 ANT 一起打包。如果您发现托管服务阻止您将文件复制到主目录中,那么最简单的做法可能就是将 ivy jar 的副本与源一起发送(并使用 taskdef 启用它)
更新
对 ant-contrib 使用以下任务定义:
<taskdef uri="antlib:net.sf.antcontrib" classpathref="build.path"/>
关于java - 自引导 Ant 构建时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17219538/