Repository: ant Updated Branches: refs/heads/master 529e86bfa -> 9953e3cbc
Bug 44241: 'activate' Steves <javaconstant> resource Project: http://git-wip-us.apache.org/repos/asf/ant/repo Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/9953e3cb Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/9953e3cb Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/9953e3cb Branch: refs/heads/master Commit: 9953e3cbc0d07a2766bdad509c05958425586c37 Parents: 529e86b Author: Jan Matèrne <j...@apache.org> Authored: Wed Jun 10 15:33:33 2015 +0200 Committer: Jan Matèrne <j...@apache.org> Committed: Wed Jun 10 15:33:33 2015 +0200 ---------------------------------------------------------------------- WHATSNEW | 2 + manual/Types/resources.html | 44 ++++++++++++++- .../apache/tools/ant/types/defaults.properties | 1 + .../types/resources/JavaConstantResource.java | 3 ++ src/tests/antunit/types/resources/test.xml | 57 ++++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ant/blob/9953e3cb/WHATSNEW ---------------------------------------------------------------------- diff --git a/WHATSNEW b/WHATSNEW index 6db5548..9a6d478 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -13,6 +13,8 @@ Fixed bugs: Other changes: -------------- + * Hidden <javaconstant> resource is published now. It reads the + value of a specified java constant. Changes from Ant 1.9.4 TO Ant 1.9.5 http://git-wip-us.apache.org/repos/asf/ant/blob/9953e3cb/manual/Types/resources.html ---------------------------------------------------------------------- diff --git a/manual/Types/resources.html b/manual/Types/resources.html index ebbeff1..58cefde 100644 --- a/manual/Types/resources.html +++ b/manual/Types/resources.html @@ -43,6 +43,8 @@ explicit use beginning in <b>Ant 1.7</b>. <li><a href="#gzipresource">gzipresource</a> - a GZip compressed resource.</li> <li><a href="#javaresource">javaresource</a> - a resource loadable via a Java classloader.</li> + <li><a href="#javaconstant">javaconstant</a> - a constant in a class loadable + via a Java classloader.</li> <li><a href="#propertyresource">propertyresource</a> - an Ant property.</li> <li><a href="#string">string</a> - a text string.</li> <li><a href="#tarentry">tarentry</a> - an entry in a tar file.</li> @@ -164,9 +166,49 @@ implementations are also usable as single-element where <b><classpath></b> is a <a href="../using.html#path">path-like structure</a>.</p> + +<h4><a name="javaconstant">javaconstant</a></h4> +<p>Loads the value of a java constant. As a specialisation of +<a href="#javaresource">javaresource</a> all of its attributes and nested elements are +supported. A constant must be specified as <tt>public static</tt> otherwise it could not be loaded.</p> + +<table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">name</td> + <td valign="top">The name of the resource. Must be specified as full qualified + field name. + </td> + <td align="center" valign="top">Yes</td> + </tr> +</table> +<h5>Examples</h5> +This loads the value of the constant <tt>VERSION</tt> of the <tt>org.acme.Main</tt> class +into the <tt>version</tt>-property. The classpath for finding that class is provided via +nested classpath element. +<pre><code><loadresource property="version"> + <javaconstant name="org.acme.Main.VERSION"> + <classpath> + <pathelement location="${acme.lib.dir}"/> + </classpath> + </javaconstant> + </loadresource> +</code></pre> + +Create a new file <tt>c:/temp/org.apache.tools.ant.Main.DEFAULT_BUILD_FILENAME</tt> with the content +of that constant (<tt>build.xml</tt>). +<pre><code><copy todir="c:/temp"> + <javaconstant name="org.apache.tools.ant.Main.DEFAULT_BUILD_FILENAME"/> +</copy></code></pre> + + <h4><a name="zipentry">zipentry</a></h4> -<p>Represents an entry in a ZIP archive. The archive can be specified +<p>Represents an entry in a ZIP archive. The archive can be specified using the archive attribute or a nested single-element resource collection. <code>zipentry</code> only supports file system resources as nested elements.</p> http://git-wip-us.apache.org/repos/asf/ant/blob/9953e3cb/src/main/org/apache/tools/ant/types/defaults.properties ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index 1837ec7..29771db 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -93,6 +93,7 @@ gzipresource=org.apache.tools.ant.types.resources.GZipResource bzip2resource=org.apache.tools.ant.types.resources.BZip2Resource javaresource=org.apache.tools.ant.types.resources.JavaResource multirootfileset=org.apache.tools.ant.types.resources.MultiRootFileSet +javaconstant=org.apache.tools.ant.types.resources.JavaConstantResource #tokenizer implementations linetokenizer=org.apache.tools.ant.util.LineTokenizer http://git-wip-us.apache.org/repos/asf/ant/blob/9953e3cb/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java b/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java index fe6ed69..a947c9a 100644 --- a/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java +++ b/src/main/org/apache/tools/ant/types/resources/JavaConstantResource.java @@ -39,6 +39,9 @@ public class JavaConstantResource extends AbstractClasspathResource { protected InputStream openInputStream(ClassLoader cl) throws IOException { Class<?> clazz; String constant = getName(); + if (constant == null) { + throw new IOException("Attribute 'name' must be set."); + } int index1 = constant.lastIndexOf('.'); if (index1 < 0) { throw new IOException("No class name in " + constant); http://git-wip-us.apache.org/repos/asf/ant/blob/9953e3cb/src/tests/antunit/types/resources/test.xml ---------------------------------------------------------------------- diff --git a/src/tests/antunit/types/resources/test.xml b/src/tests/antunit/types/resources/test.xml index fca9b56..5af37b9 100644 --- a/src/tests/antunit/types/resources/test.xml +++ b/src/tests/antunit/types/resources/test.xml @@ -400,4 +400,61 @@ </au:assertTrue> </target> + <target name="testJavaConstant"> + <property name="test.tmp.dir" value="${antunit.tmpdir}/testJavaConstant"/> + <mkdir dir="${test.tmp.dir}"/> + <echo file="${test.tmp.dir}/SomeClass.java"> + public class SomeClass { + public static final String CONSTANT = "constant"; + public final String NOT_STATIC = "not-static"; + private static final String PRIVATE = "private"; + } + </echo> + <javac srcdir="${test.tmp.dir}" destdir="${test.tmp.dir}"/> + <path id="tmp.cp"> + <pathelement location="${test.tmp.dir}"/> + </path> + + <loadresource property="actual"> + <javaconstant name="org.apache.tools.ant.Main.DEFAULT_BUILD_FILENAME"/> + </loadresource> + <au:assertEquals message="could not read java constant" expected="build.xml" actual="${actual}" /> + + <!-- + We can't test for special error messages using built-in tasks + because they catch these messages + --> + <au:expectfailure> + <loadresource property="p"> + <javaconstant/> + </loadresource> + </au:expectfailure> + <au:expectfailure> + <loadresource property="p"> + <javaconstant name="org.apache.tools.ant.MissingClass"/> + </loadresource> + </au:expectfailure> + <au:expectfailure> + <loadresource property="p"> + <javaconstant name="SomeClass.CONSTANT2" classpathref="tmp.cp"/> + </loadresource> + </au:expectfailure> + <au:expectfailure> + <loadresource property="p"> + <javaconstant name="SomeClass.PRIVATE"> + <classpath> + <pathelement location="${test.tmp.dir}"/> + </classpath> + </javaconstant> + </loadresource> + </au:expectfailure> + <au:expectfailure> + <loadresource property="p"> + <javaconstant name="SomeClass.NOT_STATIC"/> + </loadresource> + </au:expectfailure> + + <delete dir="${test.tmp.dir}"/> + </target> + </project>