Madz
Madz

Reputation: 199

Running an ant task only when a file has changed

In Ant, I am trying to achieve a simple task: If couple of files are modified a compiler should run. I have seen many solutions using OutOfDate, UpToDate and Modified. I dont want to use OutOfDate and UpToDate because I wont be able to use the task if the files have been modified on the same day. I can use modified but there is no way to call another task - my compiler task, from the modifier task. Is there any other solution apart from these?

Upvotes: 2

Views: 2995

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

Using <uptodate> with a following <antcall> to a conditional <target> will give you what you're looking for:

<project name="ant-uptodate" default="run-tests">
    <tstamp>
        <format property="ten.seconds.ago" offset="-10" unit="second" 
            pattern="MM/dd/yyyy hh:mm aa"/>
    </tstamp>

    <target name="uptodate-test">
        <uptodate property="build.notRequired" targetfile="target-file.txt">
            <srcfiles dir= "." includes="source-file.txt"/>
        </uptodate>

        <antcall target="do-compiler-conditionally"/>
    </target>

    <target name="do-compiler-conditionally" unless="build.notRequired">
        <echo>Call compiler here.</echo>
    </target>

    <target name="source-older-than-target-test">
        <touch file="source-file.txt" datetime="${ten.seconds.ago}"/>
        <touch file="target-file.txt" datetime="now"/>
        <antcall target="uptodate-test"/>
    </target>

    <target name="source-newer-than-target-test">
        <touch file="target-file.txt" datetime="${ten.seconds.ago}"/>
        <touch file="source-file.txt" datetime="now"/>
        <antcall target="uptodate-test"/>
    </target>

    <target name="run-tests" 
        depends="source-older-than-target-test,source-newer-than-target-test"/>
</project>

Upvotes: 8

Related Questions