Merge branch '1.9.x'
Project: http://git-wip-us.apache.org/repos/asf/ant/repo Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/6a9d7012 Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/6a9d7012 Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/6a9d7012 Branch: refs/heads/master Commit: 6a9d701205dfe0f129bed0e740ebc1f76041c797 Parents: 22a05c5 5e99fc2 Author: Stefan Bodewig <[email protected]> Authored: Thu Apr 26 18:14:30 2018 +0200 Committer: Stefan Bodewig <[email protected]> Committed: Thu Apr 26 18:14:30 2018 +0200 ---------------------------------------------------------------------- WHATSNEW | 5 ++ manual/Tasks/property.html | 23 +++++++ src/etc/testcases/taskdefs/property.xml | 4 ++ .../org/apache/tools/ant/taskdefs/Property.java | 70 +++++++++++++++++++- .../apache/tools/ant/taskdefs/PropertyTest.java | 13 ++++ 5 files changed, 113 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ant/blob/6a9d7012/WHATSNEW ---------------------------------------------------------------------- diff --cc WHATSNEW index ae3b1c7,a87bd81..8076df1 --- a/WHATSNEW +++ b/WHATSNEW @@@ -39,7 -36,12 +39,12 @@@ Other changes finishing threads might fail. Bugzilla Report 62148 + * <property> has a new attribute runtime which can be used to set + properties with values taken as snapshots from the + availableProcessors, freeMemory, maxMemory and totalMemory methods + of the Java Runtime class. + -Changes from Ant 1.9.10 TO Ant 1.9.11 +Changes from Ant 1.10.2 TO Ant 1.10.3 ===================================== Changes that could break older environments: http://git-wip-us.apache.org/repos/asf/ant/blob/6a9d7012/manual/Tasks/property.html ---------------------------------------------------------------------- diff --cc manual/Tasks/property.html index ed2c784,e8e57bb..199bf78 --- a/manual/Tasks/property.html +++ b/manual/Tasks/property.html @@@ -24,135 -24,169 +24,158 @@@ <body> -<h2><a name="property">Property</a></h2> +<h2 id="property">Property</h2> <h3>Description</h3> -<p>Sets a <a href="../using.html#properties">property</a> -(by name and value), or set of properties (from file or -resource) in the project. Properties are case sensitive.</p> - Properties are immutable: whoever sets a property first freezes it for the - rest of the build; they are most definitely not variables. +<p>Sets a <a href="../using.html#properties">property</a> (by name and value), or set of properties +(from file or resource) in the project. Properties are case sensitive.</p> +<p>Properties are immutable: whoever sets a property first freezes it for the rest of the build; +they are most definitely not variables.</p> <p>There are seven ways to set properties:</p> <ul> - <li>By supplying both the <i>name</i> and one of <i>value</i> or <i>location</i> attribute.</li> - <li>By supplying the <i>name</i> and nested text.</li> - <li>By supplying both the <i>name</i> and <i>refid</i> attribute.</li> - <li>By setting the <i>file</i> attribute with the filename of the property - file to load. This property file has the format as defined by the file used - in the class java.util.Properties, with the same rules about how - non-ISO8859-1 characters must be escaped.</li> - <li>By setting the <i>url</i> attribute with the url from which to load the - properties. This url must be directed to a file that has the format as defined - by the file used in the class java.util.Properties.</li> - <li>By setting the <i>resource</i> attribute with the resource name of the - property file to load. A resource is a property file on the current - classpath, or on the specified classpath.</li> - <li>By setting the <i>environment</i> attribute with a prefix to use. - Properties will be defined for every environment variable by - prefixing the supplied name and a period to the name of the variable.</li> + <li>By supplying both the <var>name</var> and one of <var>value</var> or <var>location</var> + attributes.</li> + <li>By supplying the <var>name</var> and nested text.</li> + <li>By supplying both the <var>name</var> and <var>refid</var> attributes.</li> + <li>By setting the <var>file</var> attribute with the filename of the property file to load. This + property file has the format as defined by the file used in the + class <code class="code">java.util.Properties</code>, with the same rules about how + non-ISO-8859-1 characters must be escaped.</li> + <li>By setting the <var>url</var> attribute with the URL from which to load the properties. This + URL must be directed to a file that has the format as defined by the file used in the + class <code class="code">java.util.Properties</code>.</li> + <li>By setting the <var>resource</var> attribute with the resource name of the property file to + load. A resource is a property file on the current classpath, or on the specified + classpath.</li> + <li>By setting the <var>environment</var> attribute with a prefix to use. Properties will be + defined for every environment variable by prefixing the supplied name and a period to the name + of the variable.</li> + <li>By setting the <i>runtime</i> attribute with a prefix to use. + Properties <code>prefix.availableProcessors</code>, + <code>prefix.freeMemory</code>, <code>prefix.totalMemory</code> + and <code>prefix.maxMemory</code> will be defined with values that + correspond to the corresponding methods of + the <a href="https://docs.oracle.com/javase/10/docs/api/java/lang/Runtime.html">Runtime</a> + class.</li> </ul> -<p>Although combinations of these ways are possible, only one should be used -at a time. Problems might occur with the order in which properties are set, for -instance.</p> -<p>The value part of the properties being set, might contain references to other -properties. These references are resolved at the time these properties are set. -This also holds for properties loaded from a property file.</p> -<p>A list of predefined properties can be found <a -href="../properties.html#built-in-props">here</a>.</p> -<p>Since Apache Ant 1.8.0 it is possible to load properties defined in xml -according to <a href="http://java.sun.com/dtd/properties.dtd">Suns DTD</a>, -if Java5+ is present. For this the name of the file, resource or url has -to end with <tt>.xml</tt>.</p> +<p>Although combinations of these ways are possible, only one should be used at a time. Problems +might occur with the order in which properties are set, for instance.</p> +<p>The value part of the properties being set might contain references to other properties. These +references are resolved at the time these properties are set. This also holds for properties loaded +from a property file.</p> +<p>A list of predefined properties can be +found <a href="../properties.html#built-in-props">here</a>.</p> +<p><em>Since Apache Ant 1.8.0</em>, it is possible to load properties defined in XML according +to <a href="http://java.sun.com/dtd/properties.dtd" target="_top">Sun DTD</a>, when running on Java +5+. For this the name of the file, resource or url has to end with <samp>.xml</samp>.</p> <h3>Parameters</h3> -<table border="1" cellpadding="2" cellspacing="0"> +<table class="attr"> <tr> - <td valign="top"><b>Attribute</b></td> - <td valign="top"><b>Description</b></td> - <td align="center" valign="top"><b>Required</b></td> + <th>Attribute</th> + <th>Description</th> + <th>Required</th> </tr> <tr> - <td valign="top">name</td> - <td valign="top">the name of the property to set.</td> - <td valign="top" align="center">No</td> + <td>name</td> + <td>the name of the property to set.</td> + <td>No</td> </tr> <tr> - <td valign="top">value</td> - <td valign="top">the value of the property.</td> - <td valign="middle" align="center" rowspan="3">One of these or - nested text, when using the name attribute</td> + <td>value</td> + <td>the value of the property.</td> + <td rowspan="3">One of these or nested text, when the <var>name</var> attribute is set</td> </tr> <tr> - <td valign="top">location</td> - <td valign="top">Sets the property to the absolute filename of the - given file. If the value of this attribute is an absolute path, it - is left unchanged (with / and \ characters converted to the - current platforms conventions). Otherwise it is taken as a path - relative to the project's basedir and expanded.</td> + <td>location</td> + <td class="left">Sets the property to the absolute filename of the given file. If the value of + this attribute is an absolute path, it is left unchanged (with <q>/</q> and <q>\</q> + characters converted to the current platforms conventions). Otherwise it is taken as a path + relative to the project's <var>basedir</var> and expanded.</td> </tr> <tr> - <td valign="top">refid</td> - <td valign="top"><a href="../using.html#references">Reference</a> to an object - defined elsewhere. Only yields reasonable results for references - to <a href="../using.html#path">PATH like structures</a> or properties.</td> + <td>refid</td> + <td class="left"><a href="../using.html#references">Reference</a> to an object defined + elsewhere. Only yields reasonable results for references + to <a href="../using.html#path">path-like structures</a> or properties.</td> </tr> <tr> - <td valign="top">resource</td> - <td valign="top"> the name of the classpath resource containing - properties settings in properties file format.</td> - <td valign="middle" align="center" rowspan="4">One of these, when - <b>not</b> using the name attribute</td> + <td>resource</td> + <td>the name of the classpath resource containing properties settings in properties file + format.</td> + <td rowspan="4">One of these, <strong>unless</strong> the <var>name</var> attribute is set</td> </tr> <tr> - <td valign="top">file</td> - <td valign="top">the location of the properties file to load.</td> + <td>file</td> + <td class="left">the location of the properties file to load.</td> </tr> <tr> - <td valign="top">url</td> - <td valign="top">a url containing properties-format settings.</td> + <td>url</td> + <td class="left">a URL containing properties-format settings.</td> </tr> <tr> - <td valign="top">environment</td> - <td valign="top">the prefix to use when retrieving environment variables. Thus - if you specify environment="myenv" you will be able to access OS-specific - environment variables via property names "myenv.PATH" or - "myenv.TERM". Note that if you supply a property name with a final - "." it will not be doubled; i.e. environment="myenv." will still - allow access of environment variables through "myenv.PATH" and - "myenv.TERM". This functionality is currently only implemented - on <a href="#notes-env">select platforms</a>. Feel free to send patches to increase the - number of platforms on which this functionality is supported ;).<br> - Note also that properties are case-sensitive, even if the - environment variables on your operating system are not; e.g. Windows 2000's - system path variable is set to an Ant property named "env.Path" - rather than "env.PATH".</td> + <td>environment</td> + <td class="left">the prefix to use when retrieving environment variables. Thus if you + specify <var>environment</var>=<q>myenv</q> you will be able to access OS-specific environment + variables via property names <code>myenv.PATH</code> or <code>myenv.TERM</code>. Note that if + you supply a property <var>name</var> with a final <q>.</q> it will not be doubled; + i.e. <var>environment</var>=<q>myenv.</q> will still allow access of environment variables + through <code>myenv.PATH</code> and <code>myenv.TERM</code>. This functionality is currently + only implemented on <a href="#notes-env">select platforms</a>. Feel free to send patches to + increase the number of platforms on which this functionality is supported ;-).<br/>Note also + that properties are case-sensitive, even if the environment variables on your operating system + are not; e.g. Windows 2000 or later system path variable is set to an Ant property + named <code>env.Path</code> rather than <code>env.PATH</code>.</td> </tr> <tr> - <td valign="top">runtime</td> - <td valign="top">the prefix to use when retrieving Runtime properties. Thus - if you specify runtime="myrt" you will be able to access - runtime values corresponding to methods in ++ <td>runtime</td> ++ <td>the prefix to use when retrieving Runtime properties. Thus if ++ you specify <var>runtime</var>=<q>myrt</q> you will be able to ++ access runtime values corresponding to methods in + the <a href="https://docs.oracle.com/javase/10/docs/api/java/lang/Runtime.html">Runtime</a> - class via property names "myrt.availableProcessors", - "myrt.maxMemory", "myrt.totalMemory" or - "myrt.freeMemory". Note that if you supply a property name with a final - "." it will not be doubled; i.e. runtime="myrt." will still - allow access of prpperties through "myrt.maxMemory".<br> ++ class via property names <code>myrt.availableProcessors</code>, ++ <code>myrt.maxMemory</code>, <code>myrt.totalMemory</code> or ++ <code>myrt.freeMemory</code>. Note that if you supply a property name with a final ++ <code>.</code> it will not be doubled; i.e. runtime=<code>myrt.</code> will still ++ allow access of prpperties through <code>myrt.maxMemory</code>.<br> + Note also that the property values are snapshots taken at the point in time + when the <code>property</code> has been executed. - <em>Since Ant 1.9.12</em> ++ <em>Since Ant 1.10.4</em> + </td> + </tr> + <tr> - <td valign="top">classpath</td> - <td valign="top">the classpath to use when looking up a resource.</td> - <td align="center" valign="top">No</td> + <td>classpath</td> + <td>the classpath to use when looking up a resource.</td> + <td>No</td> </tr> <tr> - <td valign="top">classpathref</td> - <td valign="top">the classpath to use when looking up a resource, - given as <a href="../using.html#references">reference</a> to a <code><path></code> defined + <td>classpathref</td> + <td>the classpath to use when looking up a resource, given + as <a href="../using.html#references">reference</a> to a <code><path></code> defined elsewhere..</td> - <td align="center" valign="top">No</td> + <td>No</td> </tr> <tr> - <td valign="top">prefix</td> - <td valign="top">Prefix to apply to properties loaded using <code>file</code>, - <code>resource</code>, or <code>url</code>. - A "." is appended to the prefix if not specified.</td> - <td align="center" valign="top">No</td> + <td>prefix</td> + <td>Prefix to apply to properties loaded using <var>file</var>, <var>resource</var>, + or <var>url</var>. A <q>.</q> is appended to the prefix if not specified.</td> + <td>No</td> </tr> <tr> - <td valign="top">prefixValues</td> - <td valign="top">Whether to apply the prefix when expanding the - right hand side of properties loaded using <code>file</code>, - <code>resource</code>, or <code>url</code>. - <em>Since Ant 1.8.2</em></td> - <td align="center" valign="top">No (default=<tt>false</tt>)</td> + <td>prefixValues</td> + <td>Whether to apply the prefix when expanding the right hand side of properties loaded + using <var>file</var>, <var>resource</var>, or <var>url</var>. <em>Since Ant 1.8.2</em></td> + <td>No; default is <q>false</q></td> </tr> <tr> - <td valign="top">relative</td> - <td valign="top">If set to <tt>true</tt> the relative path - to <tt>basedir</tt> is set. <em>Since Ant 1.8.0</em></td> - <td align="center" valign="top">No (default=<tt>false</tt>)</td> + <td>relative</td> + <td>If set to <q>true</q> the relative path to <var>basedir</var> is set. <em>Since Ant + 1.8.0</em></td> + <td>No; default is <q>false</q></td> </tr> <tr> - <td valign="top">basedir</td> - <td valign="top">The basedir to calculate the relative path - from. <em>Since Ant 1.8.0</em></td> - <td align="center" valign="top">No (default=<tt>${basedir}</tt>)</td> + <td>basedir</td> + <td>The <var>basedir</var> to calculate the relative path from. <em>Since Ant 1.8.0</em></td> + <td>No; default is project's <var>basedir</var></td> </tr> </table> http://git-wip-us.apache.org/repos/asf/ant/blob/6a9d7012/src/main/org/apache/tools/ant/taskdefs/Property.java ---------------------------------------------------------------------- diff --cc src/main/org/apache/tools/ant/taskdefs/Property.java index 4240bd5,a6b51a2..1cfd268 --- a/src/main/org/apache/tools/ant/taskdefs/Property.java +++ b/src/main/org/apache/tools/ant/taskdefs/Property.java @@@ -386,6 -395,42 +393,42 @@@ public class Property extends Task } /** + * Prefix to use when retrieving Runtime properties. + * + * <p>Properties <code>prefix.availableProcessors</code>, + * <code>prefix.freeMemory</code>, <code>prefix.totalMemory</code> + * and <code>prefix.maxMemory</code> will be defined with values + * that correspond to the corresponding methods of the {@link + * Runtime} class.</p> + * + * <p>Note that if you supply a prefix name with a final + * "." it will not be doubled. ie + * runtime="myrt." will still allow access of property + * through "myrt.availableProcessors" and + * "myrt.freeMemory".</p> + * + * <p>The property values are snapshots taken at the point in time + * when the <code>property</code> has been executed.</p> + * + * @param prefix prefix + * + * @ant.attribute group="noname" - * @since Ant 1.9.12 ++ * @since Ant 1.10.4 + */ + public void setRuntime(String prefix) { + this.runtime = prefix; + } + + /** + * Get the runtime attribute. + * @return the runtime attribute - * @since Ant 1.9.12 ++ * @since Ant 1.10.4 + */ + public String getRuntime() { + return runtime; + } + + /** * The classpath to use when looking up a resource. * @param classpath to add to any existing classpath */ @@@ -462,16 -507,15 +505,16 @@@ if (name != null) { if (untypedValue == null && ref == null) { - throw new BuildException("You must specify value, location or " - + "refid with the name attribute", - getLocation()); + throw new BuildException( + "You must specify value, location or refid with the name attribute", + getLocation()); } } else { - if (url == null && file == null && resource == null && env == null && runtime == null) { - throw new BuildException("You must specify url, file, resource, " - + "environment or runtime when not using the " - + "name attribute", getLocation()); + if (url == null && file == null && resource == null + && env == null) { + throw new BuildException( - "You must specify url, file, resource or environment when not using the name attribute", ++ "You must specify url, file, resource, environment or runtime when not using the name attribute", + getLocation()); } } @@@ -515,7 -556,11 +558,11 @@@ loadEnvironment(env); } + if (runtime != null) { + loadRuntime(runtime); + } + - if ((name != null) && (ref != null)) { + if (name != null && ref != null) { try { addProperty(name, ref.getReferencedObject(getProject()).toString()); @@@ -651,6 -707,25 +698,25 @@@ } /** + * load the runtime values + * @param prefix prefix to place before them - * @since 1.9.12 ++ * @since 1.10.4 + */ + protected void loadRuntime(String prefix) { + Properties props = new Properties(); + if (!prefix.endsWith(".")) { + prefix += "."; + } + log("Loading Runtime properties " + prefix, Project.MSG_VERBOSE); + Runtime r = Runtime.getRuntime(); + props.put(prefix + "availableProcessors", String.valueOf(r.availableProcessors())); + props.put(prefix + "freeMemory", String.valueOf(r.freeMemory())); + props.put(prefix + "maxMemory", String.valueOf(r.maxMemory())); + props.put(prefix + "totalMemory", String.valueOf(r.totalMemory())); + addProperties(props); + } + + /** * iterate through a set of properties, * resolve them then assign them * @param props the properties to iterate over http://git-wip-us.apache.org/repos/asf/ant/blob/6a9d7012/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java ---------------------------------------------------------------------- diff --cc src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java index 080b63f,adb7843..4115339 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java @@@ -18,10 -18,11 +18,11 @@@ package org.apache.tools.ant.taskdefs; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; + import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; +import static org.junit.Assume.assumeNoException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileRule; @@@ -132,6 -134,19 +133,18 @@@ public class PropertyTest buildRule.executeTarget("testXmlProperty"); assertEquals("ONE", buildRule.getProject().getProperty("xml.one")); assertEquals("TWO", buildRule.getProject().getProperty("xml.two")); - } + @Test + public void testRuntime() { + // should get no output at all + buildRule.executeTarget("testRuntime"); + assertEquals(Runtime.getRuntime().availableProcessors(), + Integer.parseInt(buildRule.getProject().getProperty("testruntime.availableProcessors"))); + assertEquals(Runtime.getRuntime().maxMemory(), + Long.parseLong(buildRule.getProject().getProperty("testruntime.maxMemory"))); + assertNotNull(buildRule.getProject().getProperty("testruntime.freeMemory")); + assertNotNull(buildRule.getProject().getProperty("testruntime.totalMemory")); + } + }
