【问题标题】:Ant - age of fileAnt - 文件年龄
【发布时间】:2014-12-27 18:44:36
【问题描述】:

有没有办法在构建过程中检查文件的年龄?

我想检查指定的文件是否超过 1 周。

类似

<olderthan property="property.name" file="checked.file" days="7"/>

我曾想过使用touchuptodate,但touch 只能使用指定日期或now

【问题讨论】:

标签: ant


【解决方案1】:

Mark 的链接没有解决我的问题,但给了我使用脚本的想法

<!-- Check if specified file is newer than age in seconds -->
<scriptdef name="isNewerThan" uri="composer.ant.mleko" language="javascript">
    <attribute name="file"/> <!-- The file to check. -->
    <attribute name="age"/> <!-- The threshold of file age in seconds. -->
    <attribute name="property"/> <!-- The name of property to set. -->
    <attribute name="value"/> <!-- The value to set the property to. Defaults to "true". -->
    <attribute name="else"/> <!-- The value to set the property to if the condition evaluates to false. By default the property will remain unset. -->
    <![CDATA[
        var fileName = attributes.get("file");
        var age = attributes.get("age");
        var property = attributes.get("property");
        var value = attributes.get("value");
        var elseValue = attributes.get("else");

        var maxAge = parseInt(age, 10);

        if(null === fileName)self.fail("`file` is required");
        if(null === age || isNaN(maxAge))self.fail("`age` is required and must be valid int string");
        if(null === property)self.fail("`property` is required");
        if(null === value)value="true";

        var file = new java.io.File(fileName);
        var ageInSeconds = (Date.now() - file.lastModified())/1000;

        if(ageInSeconds < maxAge){
            project.setProperty(property, value);
        }else if(null !== elseValue){
            project.setProperty(property, elseValue);
        }
    ]]>
</scriptdef>

【讨论】:

    【解决方案2】:

    &lt;touch&gt;&lt;uptodate&gt;&lt;tstamp&gt; 一起使用:

    <tstamp>
        <format property="one.week.ago" offset="-7" unit="day" pattern="MM/dd/yyyy hh:mm aa"/>
    </tstamp>
    
    <touch file="source-file.txt" datetime="${one.week.ago}"/>
    
    <uptodate 
        property="target-file-modified-in-previous-week" 
        targetfile="target-file.txt"
    >
        <srcfiles dir= "." includes="source-file.txt"/>
    </uptodate>
    
    <condition property="is-target-file-out-of-date" value="true" else="false">
        <isset property="target-file-modified-in-previous-week"/>
    </condition>
    
    <echo>is-target-file-out-of-date: ${is-target-file-out-of-date}</echo>
    

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 2017-03-15
      • 2015-12-05
      • 2023-03-30
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      相关资源
      最近更新 更多