【问题标题】:ant - uptodate task : regenerate only outdated filesant-uptodate 任务:只重新生成过时的文件
【发布时间】:2012-07-22 14:26:17
【问题描述】:

我是 ant 新手,习惯于 Makefile。在一个项目中,名为 Message_zh.class 等的 i18n 语言模块是在每次编译时从 zh.po 等无条件构建的,尽管它们已经存在,这会浪费很多时间。我认为这些是 build.xml 的相关部分:

<target id="msgfmt" name="msgfmt">
    <mkdir dir="po/classes" />
    <propertyregex property="filename"
              input="${file}"
              regexp="[.]*/po/([^\.]*)\.po"
              select="\1"
              casesensitive="false" />
    <exec dir="." executable="msgfmt">
        <arg line="--java2 -d po/classes -r app.i18n.Messages -l ${filename} po/${filename}.po"/>
    </exec>
</target>

<target id="build-languageclasses" name="build-languageclasses" >
    <echo message="Compiling po files." />
    <foreach target="msgfmt" param="file">
      <path>
        <fileset dir="po" casesensitive="yes">
          <include name="**/*.po"/>
        </fileset>
      </path>
    </foreach>
</target>

目标 build-languageclasses 依赖于编译目标,因此,每次编译时,都会再次对整个字符串进行 msgfmted。仅当 1. po 文件已更改或 2. 类文件不存在时,应如何编写以调用 msgfmt?如果没有其他软件也能做到这一点,我会很高兴。你能帮我举个例子吗?

第一次尝试解决方案对蚂蚁的行为没有影响:

<target id="build-languageclasses" description="compile if Messages??.class files not uptodate" name="build-languageclasses" unless="i18n.uptodate">
  <condition property="i18n.uptodate">
    <uptodate>
      <srcfiles dir="${po}" includes="**/*.po"/>
      <mapper type="glob" from="${po}/*.po" to="${po}/classes/app/i18n/Messages*.class"/>
    </uptodate>
  </condition>
  <echo message="Compiling po files." />
  <foreach target="msgfmt" param="file">
    <path>
      <fileset dir="po" casesensitive="yes">
        <include name="**/*.po"/>
      </fileset>
    </path>
  </foreach>
</target> 

这里有什么问题?

【问题讨论】:

    标签: java ant internationalization gettext


    【解决方案1】:

    问题是您在运行uptodate 任务之前正在测试属性i18n.uptodate。在您输入 build-languageclasses 目标之前,您的条件块必须运行。

    你应该像这样重新组织你的代码:

    • 删除主目标上的unless="i18n.uptodate"
    • build-languageclasses 拆分为2 个目标。
    • 第一个专用于初始化条件,仅包含 &lt;condition&gt; 块。
    • 第二个包含生成文件的代码 (&lt;foreach&gt;)

    第二个目标配置为根据第一个目标设置的属性i18n.uptodate 有条件地运行。

    EDIT - 这是更新任务的工作示例

    <property name="source" value="${basedir}/src"/>
    <property name="dist" value="${basedir}/dist"/>
    
    <target name="init">
        <condition property="that.uptodate">
            <uptodate>
                <srcfiles dir="${source}" includes="*.txt"/>
                <mapper type="glob" from="*.txt" to="${dist}/*.bat"/>
            </uptodate>
        </condition>
    </target>
    
    <target description="check that" name="dist" unless="that.uptodate" depends="init">
        <echo>we have something to do!</echo>
    </target>
    

    HIH M.

    【讨论】:

    • 谢谢。我接受了,因为您的回答包含解决问题的必要步骤。但是,我仍然有问题让它工作。 ant -verbose 现在在更新块中时说:[uptodate] (path)/po/ar.po skipped - 不知道如何处理它。为什么它只在检查时必须处理它?
    • 我没有。能发一下代码吗?当您收到此消息时,ar.po 文件是最新的还是不同步的?
    • Messages_ar.class 依赖于 ar.po 但是是最新的。
    • 我已经编辑了我的答案,向您展示它是如何工作的。必须修改 fromto 参数。 from 不得包含文件夹名称,to 仅当它不同时。更改模式,直到您不断收到 不知道如何处理 错误,这意味着在映射过程中出现错误。
    • 就是这样,非常感谢!在调整相对于 srcfiles 目录的路径后,一切正常。
    【解决方案2】:

    尝试使用 ant-contrib - OutOfDate - 如果您希望在第一封邮件中使用第二个来源中的结构 http://ant-contrib.sourceforge.net/tasks/tasks/outofdate.html

    【讨论】:

      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2011-08-13
      • 2020-04-18
      • 1970-01-01
      相关资源
      最近更新 更多