| @@ -13,6 +13,8 @@ Fixed bugs: | |||||
| Other changes: | 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 | Changes from Ant 1.9.4 TO Ant 1.9.5 | ||||
| @@ -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="#gzipresource">gzipresource</a> - a GZip compressed resource.</li> | ||||
| <li><a href="#javaresource">javaresource</a> - a resource loadable | <li><a href="#javaresource">javaresource</a> - a resource loadable | ||||
| via a Java classloader.</li> | 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="#propertyresource">propertyresource</a> - an Ant property.</li> | ||||
| <li><a href="#string">string</a> - a text string.</li> | <li><a href="#string">string</a> - a text string.</li> | ||||
| <li><a href="#tarentry">tarentry</a> - an entry in a tar file.</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 | where <b><classpath></b> is a <a | ||||
| href="../using.html#path">path-like structure</a>.</p> | 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> | <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 | using the archive attribute or a nested single-element resource | ||||
| collection. <code>zipentry</code> only supports file system resources | collection. <code>zipentry</code> only supports file system resources | ||||
| as nested elements.</p> | as nested elements.</p> | ||||
| @@ -93,6 +93,7 @@ gzipresource=org.apache.tools.ant.types.resources.GZipResource | |||||
| bzip2resource=org.apache.tools.ant.types.resources.BZip2Resource | bzip2resource=org.apache.tools.ant.types.resources.BZip2Resource | ||||
| javaresource=org.apache.tools.ant.types.resources.JavaResource | javaresource=org.apache.tools.ant.types.resources.JavaResource | ||||
| multirootfileset=org.apache.tools.ant.types.resources.MultiRootFileSet | multirootfileset=org.apache.tools.ant.types.resources.MultiRootFileSet | ||||
| javaconstant=org.apache.tools.ant.types.resources.JavaConstantResource | |||||
| #tokenizer implementations | #tokenizer implementations | ||||
| linetokenizer=org.apache.tools.ant.util.LineTokenizer | linetokenizer=org.apache.tools.ant.util.LineTokenizer | ||||
| @@ -39,6 +39,9 @@ public class JavaConstantResource extends AbstractClasspathResource { | |||||
| protected InputStream openInputStream(ClassLoader cl) throws IOException { | protected InputStream openInputStream(ClassLoader cl) throws IOException { | ||||
| Class<?> clazz; | Class<?> clazz; | ||||
| String constant = getName(); | String constant = getName(); | ||||
| if (constant == null) { | |||||
| throw new IOException("Attribute 'name' must be set."); | |||||
| } | |||||
| int index1 = constant.lastIndexOf('.'); | int index1 = constant.lastIndexOf('.'); | ||||
| if (index1 < 0) { | if (index1 < 0) { | ||||
| throw new IOException("No class name in " + constant); | throw new IOException("No class name in " + constant); | ||||
| @@ -400,4 +400,61 @@ | |||||
| </au:assertTrue> | </au:assertTrue> | ||||
| </target> | </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> | </project> | ||||