Browse Source

Add url attribute to the properties task

PR: 20749
Obtained from: Andrew Ferguson


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274675 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 22 years ago
parent
commit
a812b0a411
4 changed files with 98 additions and 36 deletions
  1. +24
    -15
      docs/manual/CoreTasks/property.html
  2. +8
    -2
      src/etc/testcases/taskdefs/property.xml
  3. +47
    -4
      src/main/org/apache/tools/ant/taskdefs/Property.java
  4. +19
    -15
      src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java

+ 24
- 15
docs/manual/CoreTasks/property.html View File

@@ -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,22 +73,26 @@ href="../using.html#built-in-props">here</a>.</p>
<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>
<td valign="top">file</td>
<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=&quot;myenv&quot; you will be able to access OS-specific
environment variables via property names &quot;myenv.PATH&quot; or
&quot;myenv.TERM&quot;. Note that if you supply a property name with a final
&quot;.&quot; it will not be doubled. ie environment=&quot;myenv.&quot; will still
allow access of environment variables through &quot;myenv.PATH&quot; and
&quot;myenv.TERM&quot;. This functionality is currently only implemented
if you specify environment=&quot;myenv&quot; you will be able to access OS-specific
environment variables via property names &quot;myenv.PATH&quot; or
&quot;myenv.TERM&quot;. Note that if you supply a property name with a final
&quot;.&quot; it will not be doubled. ie environment=&quot;myenv.&quot; will still
allow access of environment variables through &quot;myenv.PATH&quot; and
&quot;myenv.TERM&quot;. 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 @@ href="../using.html#built-in-props">here</a>.</p>

</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 &lt;path&gt; 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 @@ href="../using.html#path">PATH like structure</a> and can also be set via a nest
<p>sets the property <code>foo.dist</code> to the value &quot;dist&quot;.</p>
<pre> &lt;property file=&quot;foo.properties&quot;/&gt;</pre>
<p>reads a set of properties from a file called &quot;foo.properties&quot;.</p>
<pre> &lt;property url=&quot;http://www.mysite.com/bla/props/foo.properties&quot;/&gt;</pre>
<p>reads a set of properties from the address &quot;http://www.mysite.com/bla/props/foo.properties&quot;.</p>
<pre> &lt;property resource=&quot;foo.properties&quot;/&gt;</pre>
<p>reads a set of properties from a resource called &quot;foo.properties&quot;.</p>
<p>Note that you can reference a global properties file for all of your Ant
@@ -132,7 +141,7 @@ to be your home directory. Where the &quot;user.home&quot; property resolves to
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 &quot;Documents
and Settings&quot; folder. Older windows variants such as Windows 98/ME are less
and Settings&quot; 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 @@ predictable, as are other operating system/JVM combinations.</p>
</pre>
<p>reads the system environment variables and stores them in properties, prefixed with &quot;env&quot;.
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 &copy; 2000-2003 Apache Software Foundation. All rights


+ 8
- 2
src/etc/testcases/taskdefs/property.xml View File

@@ -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>

+ 47
- 4
src/main/org/apache/tools/ant/taskdefs/Property.java View File

@@ -58,6 +58,7 @@ import java.io.File;
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 @@ public class Property extends Task {
protected String name;
protected String value;
protected File file;
protected URL url;
protected String resource;
protected Path classpath;
protected String env;
@@ -190,6 +192,20 @@ public class Property extends Task {
return file;
}

/**
* 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>.
@@ -345,16 +361,16 @@ public class Property extends Task {
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 @@ public class Property extends Task {
loadFile(file);
}

if (url != null) {
loadUrl(url);
}

if (resource != null) {
loadResource(resource);
}
@@ -388,6 +408,29 @@ public class Property extends Task {
}
}

/**
* 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
* @param file file to load


+ 19
- 15
src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java View File

@@ -60,25 +60,25 @@ import org.apache.tools.ant.BuildFileTest;
/**
* @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 @@ public class PropertyTest extends BuildFileTest {
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 @@ public class PropertyTest extends BuildFileTest {
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");
}
}

Loading…
Cancel
Save