peterreilly 2003/06/18 01:10:21 Modified: docs/manual/CoreTasks property.html src/etc/testcases/taskdefs property.xml src/main/org/apache/tools/ant/taskdefs Property.java src/testcases/org/apache/tools/ant/taskdefs PropertyTest.java Log: Add url attribute to the properties task PR: 20749 Obtained from: Andrew Ferguson Revision Changes Path 1.15 +24 -15 ant/docs/manual/CoreTasks/property.html Index: property.html =================================================================== RCS file: /home/cvs/ant/docs/manual/CoreTasks/property.html,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- property.html 5 Feb 2003 11:20:17 -0000 1.14 +++ property.html 18 Jun 2003 08:10:21 -0000 1.15 @@ -12,14 +12,17 @@ <p>Sets a property (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 definately not variable. -<p>There are five ways to set properties:</p> + rest of the build; they are most definately not variable. +<p>There are six ways to set properties:</p> <ul> <li>By supplying both the <i>name</i> and <i>value</i> attribute.</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.</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. This property file has the format as defined by the file used in the class java.util.Properties.</li> @@ -70,7 +73,7 @@ <tr> <td valign="top">resource</td> <td valign="top">the resource name of the property file.</td> - <td valign="middle" align="center" rowspan="3">One of these, when + <td valign="middle" align="center" rowspan="4">One of these, when <b>not</b> using the name attribute</td> </tr> <tr> @@ -78,14 +81,18 @@ <td valign="top">the filename of the property file .</td> </tr> <tr> + <td valign="top">url</td> + <td valign="top">the url from which to read properties.</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. ie environment="myenv." will still - allow access of environment variables through "myenv.PATH" and - "myenv.TERM". This functionality is currently only implemented + 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. ie environment="myenv." will still + allow access of environment variables through "myenv.PATH" and + "myenv.TERM". This functionality is currently only implemented on select platforms. Feel free to send patches to increase the number of platforms this functionality is supported on ;).<br> Note also that properties are case sensitive, even if the @@ -94,19 +101,19 @@ </tr> <tr> - <td valign="top">classpath</td> + <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> </tr> <tr> - <td valign="top">classpathref</td> + <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 <path> defined elsewhere..</td> <td align="center" valign="top">No</td> </tr> <tr> - <td valign="top">prefix</td> + <td valign="top">prefix</td> <td valign="top">Prefix to apply to properties loaded using <code>file</code> or <code>resource</code>. A "." is appended to the prefix if not specified.</td> <td align="center" valign="top">No</td> @@ -122,6 +129,8 @@ <p>sets the property <code>foo.dist</code> to the value "dist".</p> <pre> <property file="foo.properties"/></pre> <p>reads a set of properties from a file called "foo.properties".</p> +<pre> <property url="http://www.mysite.com/bla/props/foo.properties"/></pre> +<p>reads a set of properties from the address "http://www.mysite.com/bla/props/foo.properties".</p> <pre> <property resource="foo.properties"/></pre> <p>reads a set of properties from a resource called "foo.properties".</p> <p>Note that you can reference a global properties file for all of your Ant @@ -132,7 +141,7 @@ the file system depends on the operating system version and the JVM implementation. On Unix based systems, this will map to the user's home directory. On modern Windows variants, this will most likely resolve to the user's directory in the "Documents -and Settings" folder. Older windows variants such as Windows 98/ME are less +and Settings" folder. Older windows variants such as Windows 98/ME are less predictable, as are other operating system/JVM combinations.</p> <pre> @@ -142,8 +151,8 @@ </pre> <p>reads the system environment variables and stores them in properties, prefixed with "env". Note that this only works on <em>select</em> operating systems. -Two of the values are shown being echoed. -</p> +Two of the values are shown being echoed. +</p> <hr> <p align="center">Copyright © 2000-2003 Apache Software Foundation. All rights 1.6 +8 -2 ant/src/etc/testcases/taskdefs/property.xml Index: property.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/property.xml,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- property.xml 23 Feb 2002 20:41:55 -0000 1.5 +++ property.xml 18 Jun 2003 08:10:21 -0000 1.6 @@ -21,7 +21,13 @@ <property file="property3.properties"/> <echo message="http.url is ${http.url}"/> </target> - + + <target name="test5"> + <property name="http.port" value="999" /> + <property url="file://${basedir}/property3.properties"/> + <echo message="http.url is ${http.url}"/> + </target> + <target name="prefix.success"> <property file="property3.properties" prefix="server1"/> </target> @@ -29,5 +35,5 @@ <target name="prefix.fail"> <property name="someprop" value="value" prefix="prefix"/> </target> - + </project> 1.62 +47 -4 ant/src/main/org/apache/tools/ant/taskdefs/Property.java Index: Property.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Property.java,v retrieving revision 1.61 retrieving revision 1.62 diff -u -r1.61 -r1.62 --- Property.java 25 Apr 2003 14:11:16 -0000 1.61 +++ Property.java 18 Jun 2003 08:10:21 -0000 1.62 @@ -58,6 +58,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.util.Enumeration; import java.util.Properties; import java.util.Vector; @@ -108,6 +109,7 @@ protected String name; protected String value; protected File file; + protected URL url; protected String resource; protected Path classpath; protected String env; @@ -191,6 +193,20 @@ } /** + * The url from which to load properties. + * @param url url string + * + * @ant.attribute group="noname" + */ + public void setUrl(URL url) { + this.url = url; + } + + public URL getUrl() { + return url; + } + + /** * Prefix to apply to properties loaded using <code>file</code> * or <code>resource</code>. * A "." is appended to the prefix if not specified. @@ -345,16 +361,16 @@ getLocation()); } } else { - if (file == null && resource == null && env == null) { - throw new BuildException("You must specify file, resource or " + 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", getLocation()); } } - if (file == null && resource == null && prefix != null) { + if (url == null && file == null && resource == null && prefix != null) { throw new BuildException("Prefix is only valid when loading from " - + "a file or resource", getLocation()); + + "a url, file or resource", getLocation()); } if ((name != null) && (value != null)) { @@ -365,6 +381,10 @@ loadFile(file); } + if (url != null) { + loadUrl(url); + } + if (resource != null) { loadResource(resource); } @@ -387,6 +407,29 @@ } } } + + /** + * load properties from a url + * @param url url to load from + */ + protected void loadUrl(URL url) throws BuildException { + Properties props = new Properties(); + log("Loading " + url, Project.MSG_VERBOSE); + try { + InputStream is = url.openStream(); + try { + props.load(is); + } finally { + if (is != null) { + is.close(); + } + } + addProperties(props); + } catch (IOException ex) { + throw new BuildException(ex, getLocation()); + } + } + /** * load properties from a file 1.9 +19 -15 ant/src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java Index: PropertyTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- PropertyTest.java 10 Feb 2003 14:14:45 -0000 1.8 +++ PropertyTest.java 18 Jun 2003 08:10:21 -0000 1.9 @@ -60,25 +60,25 @@ /** * @author Conor MacNeill */ -public class PropertyTest extends BuildFileTest { - - public PropertyTest(String name) { +public class PropertyTest extends BuildFileTest { + + public PropertyTest(String name) { super(name); - } - - public void setUp() { + } + + public void setUp() { configureProject("src/etc/testcases/taskdefs/property.xml"); } - public void test1() { + public void test1() { // should get no output at all expectOutputAndError("test1", "", ""); } - public void test2() { + public void test2() { expectLog("test2", "testprop1=aa, testprop3=xxyy, testprop4=aazz"); } - + public void test3() { try { executeTarget("test3"); @@ -86,15 +86,19 @@ catch (BuildException e) { assertEquals("Circular definition not detected - ", true, e.getMessage().indexOf("was circularly defined") != -1); - return; + return; } - fail("Did not throw exception on circular exception"); + fail("Did not throw exception on circular exception"); } - public void test4() { + public void test4() { expectLog("test4", "http.url is http://localhost:999"); } - + + public void test5() { + expectLog("test5", "http.url is http://localhost:999"); + } + public void testPrefixSuccess() { executeTarget("prefix.success"); assertEquals("80", project.getProperty("server1.http.port")); @@ -107,9 +111,9 @@ catch (BuildException e) { assertEquals("Prefix allowed on non-resource/file load - ", true, e.getMessage().indexOf("Prefix is only valid") != -1); - return; + return; } fail("Did not throw exception on invalid use of prefix"); } - + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]