commit 7c6c749c952153170e82b791d97086e99fe2a3cb
Author: Stefan Bodewig
Since Ant 1.9.10 the magic
+ property ant.tstamp.now
can be used to specify a fixed
+ date value in order to create reproducible builds. Its value must be
+ a number and is interpreted as seconds since the epoch (midnight
+ 1970-01-01).
ant.tstamp.now |
+ number, seconds since the epoch (midnight 1970-01-01) | +The value to use as current time and date for <tstamp> | +
diff --git a/src/main/org/apache/tools/ant/MagicNames.java b/src/main/org/apache/tools/ant/MagicNames.java index bc39a2578..67ab8a810 100644 --- a/src/main/org/apache/tools/ant/MagicNames.java +++ b/src/main/org/apache/tools/ant/MagicNames.java @@ -289,5 +289,18 @@ public final class MagicNames { * Value {@value} */ public static final String HTTP_AGENT_PROPERTY = "ant.http.agent"; + + /** + * Magic property that can be set to contain a value for tstamp's + * "now" in order to make builds that use the task create + * reproducible results. + * + *
The value is expected to be a number representing the date + * as seconds since the epoch.
+ * + * Value: {@value} + * @since Ant 1.9.10 + */ + public static final String TSTAMP_NOW = "ant.tstamp.now"; } diff --git a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java index 805427aba..a6a35d7b4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java @@ -32,6 +32,7 @@ import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Location; +import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -68,7 +69,7 @@ public class Tstamp extends Task { */ public void execute() throws BuildException { try { - Date d = new Date(); + Date d = getNow(); Enumeration i = customFormats.elements(); while (i.hasMoreElements()) { @@ -110,6 +111,22 @@ public class Tstamp extends Task { } /** + * Return the {@link Date} instance to use as base for DSTAMP, TSTAMP and TODAY. + */ + protected Date getNow() { + String magicNow = getProject().getProperty(MagicNames.TSTAMP_NOW); + if (magicNow != null && magicNow.length() > 0) { + try { + return new Date(1000 * Long.parseLong(magicNow)); + } catch (NumberFormatException ex) { + log("magic property " + MagicNames.TSTAMP_NOW + " ignored as " + + magicNow + " is not a valid number"); + } + } + return new Date(); + } + + /** * This nested element that allows a property to be set * to the current date and time in a given format. * The date/time patterns are as defined in the diff --git a/src/tests/antunit/taskdefs/tstamp-test.xml b/src/tests/antunit/taskdefs/tstamp-test.xml new file mode 100644 index 000000000..c9bebf866 --- /dev/null +++ b/src/tests/antunit/taskdefs/tstamp-test.xml @@ -0,0 +1,27 @@ + + +