如何在项目之间最好地共享 Ant 目标? [英] How can I best share Ant targets between projects?

查看:25
本文介绍了如何在项目之间最好地共享 Ant 目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种成熟的方法可以在项目之间共享 Ant 目标?我目前有一个解决方案,但它有点不雅.这是我目前正在做的事情.

Is there a well-established way to share Ant targets between projects? I have a solution currently, but it's a bit inelegant. Here's what I'm doing so far.

我在我们网络的服务器上托管了一个名为 ivy-tasks.xml 的文件.该文件包含用于使用 Ivy 管理项目依赖项的样板任务,以及其他目标.例如:

I've got a file called ivy-tasks.xml hosted on a server on our network. This file contains, among other targets, boilerplate tasks for managing project dependencies with Ivy. For example:

<project name="ant-ivy-tasks" default="init-ivy"
         xmlns:ivy="antlib:org.apache.ivy.ant">
  ...
  <target name="ivy-download" unless="skip.ivy.download">
    <mkdir dir="${ivy.jar.dir}"/>
    <echo message="Installing ivy..."/>
    <get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
         dest="${ivy.jar.file}" usetimestamp="true"/>
  </target>

  <target name="ivy-init" depends="ivy-download"
          description="-> Defines ivy tasks and loads global settings">
    <path id="ivy.lib.path">
      <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
    </path>
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
             uri="antlib:org.apache.ivy.ant"
             classpathref="ivy.lib.path"/>
    <ivy:settings url="http://myserver/ivy/settings/ivysettings-user.xml"/>
  </target>
  ...
</project>

托管此文件的原因是我不想:

The reason this file is hosted is because I don't want to:

  • 将文件签入每个需要它的项目 - 这将导致重复,使维护目标变得更加困难.
  • 让我的 build.xml 依赖于从源代码管理中检出一个项目 - 这将使构建在顶层有更多的 XML 来访问文件.

我在项目的 build.xmls 中对这个文件所做的事情是这样的:

What I do with this file in my projects' build.xmls is along the lines of:

<property name="download.dir" location="download"/>
<mkdir dir="${download.dir}"/>
<echo message="Downloading import files to ${download.dir}"/>

<get src="http://myserver/ivy/ivy-tasks.xml" dest="${download.dir}/ivy-tasks.xml" usetimestamp="true"/>
<import file="${download.dir}/ivy-tasks.xml"/>

关于这个的脏"部分是我必须在目标之外执行上述步骤,因为导入任务 必须在顶级.另外,我仍然需要在所有需要它的 build.xml 文件中包含这个 XML(即仍然有一些重复).

The "dirty" part about this is that I have to do the above steps outside of a target, because the import task must be at the top-level. Plus, I still have to include this XML in all of the build.xml files that need it (i.e. there's still some amount of duplication).

最重要的是,在其他情况下,我可能需要导入一些常见(非 Ivy)任务.如果我要使用 Ivy 的依赖项管理来提供这些任务,我仍然会遇到问题,因为到我已经解决了依赖项时,我将不得不在我的 build.xml 中的目标内,并且无法导入(由于到上面提到的约束).

On top of that, there might be additional situations where I might have common (non-Ivy) tasks that I'd like imported. If I were to provide these tasks using Ivy's dependency management I'd still have problems, since by the time I'd have resolved the dependencies I would have to be inside of a target in my build.xml, and unable to import (due to the constraint mentioned above).

对于我正在努力完成的工作,是否有更好的解决方案?

Is there a better solution for what I'm trying to accomplish?

推荐答案

如果您使用的是 ANT 1.8+,那么您可以直接从托管位置导入 build.xml.

If you are using ANT 1.8+, then you could just import the build.xml directly from the hosted location.

http://ant.apache.org/manual/Tasks/import.html

从 Ant 1.8.0 开始,任务也可以从 URL 导入资源或类路径资源(即 URL、真的).如果您需要知道是否当前构建文件的源有是您可以查阅的文件或 URL属性 ant.file.type.projectname(使用与上面相同的示例ant.file.type.builddocs) 要么具有值文件"或网址".

Since Ant 1.8.0 the task can also import resources from URLs or classpath resources (which are URLs, really). If you need to know whether the current build file's source has been a file or an URL you can consult the property ant.file.type.projectname (using the same example as above ant.file.type.builddocs) which either have the value "file" or "url".

<!-- importing.xml -->
<project name="importing" basedir="." default="...">
  <import file="http://myserver/ivy/ivy-tasks.xml"/>
</project>

这篇关于如何在项目之间最好地共享 Ant 目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆