避免在 Ant 中重新构建先决条件 [英] Avoiding re-building prerequisites in Ant

查看:21
本文介绍了避免在 Ant 中重新构建先决条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 Ant 项目,希望加快构建过程避免重新构建已经是最新的组件.

I have an existing Ant project and would like to speed up the build process by avoiding re-building components that are already up to date.

Ant 允许您指定一个目标依赖于另一个目标,但通过默认每个先决条件总是重建,即使它已经达到日期.(这是 Ant 和 make 之间的一个关键区别.默认情况下,make仅在需要时重新构建目标——也就是说,如果某些先决条件是较新.)

Ant permits you to specify that one target depends on another, but by default every prerequisite is always rebuilt, even if it is already up to date. (This is a key difference between Ant and make. By default, make only re-builds a target when needed -- that is, if some prerequisite is newer.)

<uptodate property="mytarget.uptodate">  // in set.mytarget.uptodate task
  ...
</uptodate>
<!-- The prerequisites are executed before the "unless" is checked. -->
<target name="mytarget" depends="set.mytarget.uptodate" unless="mytarget.uptodate">
  ...
</target>

为了让 Ant 只在必要时重新构建先决条件,似乎有两个Ant 中的一般方法.

To make Ant re-build prerequisites only if necessary, there seem to be two general approaches within Ant.

第一种方法是使用 uptodate 任务来设置属性.然后,您的任务可以测试该属性并仅在该属性是(不是)时进行构建设置.

The first approach is to use the uptodate task to set a property. Then, your task can test the property and build only if the property is (not) set.

<uptodate property="mytarget.uptodate">  // in set.mytarget.uptodate task
  ...
</uptodate>
<!-- The prerequisites are executed before the "unless" is checked. -->
<target name="mytarget" depends="set.mytarget.uptodate" unless="mytarget.uptodate">
  ...
</target>

另一种方法是使用来自 ant contrib 的 outofdate 任务.更好的是它只是一个目标而没有单独的属性定义;相比之下,过时需要单独的目标来设定和执行测试属性.

An alternate first approach is to use the outofdate task from ant contrib. It's nicer in that it is just one target without a separate property being defined; by contrast, outofdate requires separate targets to set and to test the property.

第二种方法是使用 创建一个 选择器.它计算文件的 MD5 哈希值并选择其 MD5不同于之前存储的值.可选设置

The second approach is to create a <fileset> using the <modified> selector. It calculates MD5 hashes for files and selects files whose MD5 differs from earlier stored values. It's optional to set

 <param name="cache.cachefile"     value="cache.properties"/>

在选择器内部;它默认为cache.properties".这是将所有文件从 src 复制到 dest 的示例,其内容具有改变了:

inside the selector; it defaults to "cache.properties". Here is an example that copies all files from src to dest whose content has changed:

    <copy todir="dest">
        <fileset dir="src">
            <modified/>
        </fileset>
    </copy>

这些都不是很满意,因为它需要我写Ant应该是自动的流程(避免重新构建)的代码.

Neither of these is very satisfactory, since it requires me to write Ant code for a process (avoiding re-building) that ought to be automatic.

也有常春藤,但我无法从它的文档中判断它是否提供此功能.Ivy 文档中的关键用例似乎是从互联网下载子项目而不是避免浪费通过暂存单个项目的部分来工作.Maven 提供了类似的功能,在其文档中突出显示了相同的用例.(将现有的非平凡项目迁移到 Maven 据说是一场噩梦;相比之下,使用 Maven 开始绿地开发更可口.)

There is also Ivy, but I can't tell from its documentation whether it provides this feature. The key use case in the Ivy documentation seems to be downloading subprojects from the Internet rather than avoiding wasted work by staging the parts of a single project. Maven provides similar functionality, with the same use case highlighted in its documentation. (Moving an existing non-trivila project to Maven is said to be a nightmare; by contrast, starting greenfield development with Maven is more palatable.)

有没有更好的方法?

推荐答案

这种大型构建的条件编译是 make 的一个特性,而我最初在 ANT 中忽略了这个特性.与其使用目标依赖项,我建议您将大型​​项目划分为更小的模块,每个模块都发布到一个公共共享存储库.

This conditional compilation of a large build is a feature of make that I initally missed in ANT. Rather than use target dependencies, I'd suggest dividing your large project into smaller modules, each publishing to a common shared repository.

然后可以使用 Ivy 来控制项目主模块使用的组件版本.

Ivy can then be used to control the component versions used by the main module of the project.

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="multi_module_project"/>
    <publications>
        <artifact name="main" type="jar"/>
    </publications>
    <dependencies>
        <dependency org="com.myspotontheweb" name="component1" rev="latest.integration"/>
        <dependency org="com.myspotontheweb" name="component2" rev="latest.integration"/>
        <dependency org="com.myspotontheweb" name="component3" rev="latest.integration"/>
        <dependency org="com.myspotontheweb" name="component4" rev="latest.integration"/>
    </dependencies>
</ivy-module>

ivy:retrieve 任务只会下载/复制一个子模块,如果它们发生了变化(从它们的构建文件发布)

The ivy:retrieve task will only download/copy one of the sub-modules if they have changed (published from their build files)

这听起来更复杂,但也许您已经在构建文件中细分了项目......例如,如果您的 ANT uptodate 任务依赖于构建文物.

It all sounds more complicated but maybe you're already sub-dividing the project within your build file.... For example, if your ANT uptodate task is being made dependent on one the build artifacts.

这篇关于避免在 Ant 中重新构建先决条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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