1

During a build process using Ant, I want to update the last modified date of a generated file. I am concatenating (using concat task) several files to generate this file, and I want to set the modified date of this file to the date of the most recently modified of the source files.

I don't see any option in the touch task to use several files as the source for the date.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Óscar
  • 650
  • 1
  • 4
  • 16
  • You can first select the latest modified file using Ant's ``, ``, and `` selectors https://stackoverflow.com/questions/28216756/finding-and-using-the-latest-modified-folder-directory-using-ant The ugly part will be actually extracting the date from that file. The date format can vary depending on the user's OS, and Ant doesn't have a native way of getting this info in the first place. Ant-contrib has a `` task, you could run a command with ``, or you could ` – CAustin Nov 15 '17 at 19:15

1 Answers1

0

Here's an example solution:

<scriptdef name="filedate" language="javascript">
  <attribute name="file"/>
  <attribute name="property"/>
  <![CDATA[
    var file_name = attributes.get( "file" );
    var property_to_set = attributes.get( "property" );

    var file = new java.io.File( file_name );
    var file_date = file.lastModified();

    project.setProperty( property_to_set, file_date );
  ]]>   
</scriptdef>

<last id="last.dir">
  <sort>
    <fileset dir="folder" includes="*" />
    <date />
  </sort>
</last>

<filedate file="${ant.refid:last.dir}" property="file.ts" />

<touch file="concat.file" millis="${file.ts}" />

The scriptdef is derived from this answer by David W. and simplified as we don't need to format the date-time, we can just use the "epoch milliseconds" that Java File lastModified() provides and the Ant touch task expects.

martin clayton
  • 76,436
  • 32
  • 213
  • 198