使用 Ivy 在 Ant 构建中设置路径的最低配置?

Minimum config to use Ivy to set a path in an Ant build?

我想使用在工件中定义的 Ant 任务。该工件存在于主要的 Maven 存储库中并且具有一些依赖性。

我想使用 Ivy 和 Ant 来:

  1. 声明对该工件的依赖及其传递依赖,以便在脚本 运行.
  2. 时解析它们
  3. 检索所有 jar 文件作为 Ant 路径,我可以将其输入 Ant 的 taskdef。
  4. 在构建脚本的其他部分引用那组已解析的 jar 文件。

到目前为止,我找到的文档并未针对此用例进行优化。相反,它建议写入文件 ivy.xml、ivysettings.xml;我不喜欢这样,依赖关系很小,我想将所有内容都放在一个构建脚本中。

有什么想法吗?

ivy cachepath task 是一个解析任务,可用于创建 ANT 路径。可能不为人知的是,这个解析任务也可以内联使用,换句话说,你可以直接指定依赖而不需要ivy文件。

<ivy:cachepath pathid="tasks.path">
    <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
</ivy:cachepath>

有关利用 ivy 文件管理多个类路径的相关答案:

例子

下面的例子有点做作,演示了 ivy 下载与 groovy 任务相关的 jar。我还包含了一个实用目标,我也用它来安装 ivy jar。

build.xml

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <available classname="org.apache.ivy.Main" property="ivy.installed"/> 

    <!--
    ============
    Main targets 
    ============
    -->
    <target name="resolve" depends="install-ivy">
        <ivy:cachepath pathid="tasks.path">
            <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
        </ivy:cachepath>
    </target>

    <target name="build" depends="resolve">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="tasks.path"/>

        <groovy>
          ant.echo "Hello world"
        </groovy>
    </target>

    <!--
    ==================
    Supporting targets
    ==================
    -->
    <target name="install-ivy" description="Install ivy" unless="ivy.installed">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
        <fail message="Ivy has been installed. Run the build again"/>
    </target>

    <target name="clean" description="Cleanup build files">
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-all" depends="clean" description="Additionally purge ivy cache">
        <ivy:cleancache/>
    </target>

</project>