git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274575 13f79535-47bb-0310-9956-ffa450edef68master
@@ -306,8 +306,8 @@ Other changes: | |||||
defaultexcludes task. Bugzilla Report 12700. | defaultexcludes task. Bugzilla Report 12700. | ||||
* There is a new data type <propertyset> that can be used to collect | * There is a new data type <propertyset> that can be used to collect | ||||
properties. It is supported by <ant>, <antcall>, <subant>, <java> | |||||
and <junit>. | |||||
properties. It is supported by <ant>, <antcall>, <subant>, <java>, | |||||
<echoproperties> and <junit>. | |||||
* <concat> can now control the encoding of the output as well and optionally | * <concat> can now control the encoding of the output as well and optionally | ||||
add new-line characters at the end of files that get concatenated but | add new-line characters at the end of files that get concatenated but | ||||
@@ -9,12 +9,14 @@ | |||||
<h2><a name="echoproperties">echoproperties</a></h2> | <h2><a name="echoproperties">echoproperties</a></h2> | ||||
<h3>Description</h3> | <h3>Description</h3> | ||||
<p>Displays all the current properties in the project. The output can be | |||||
sent to a file if desired. You can also specify a subset of properties | |||||
to save by naming a prefix: only properties starting with this | |||||
prefix will be saved. This task can be used as a somewhat contrived | |||||
means of returning data from an <tt><ant></tt> invocation, | |||||
but is really for debugging build files.</p> | |||||
<p>Displays all the current properties (or a subset of them specified | |||||
by a nested <code><propertyset></code>) in the project. The | |||||
output can be sent to a file if desired. This task can be used as a | |||||
somewhat contrived means of returning data from an | |||||
<tt><ant></tt> invocation, but is really for debugging build | |||||
files.</p> | |||||
<h3>Parameters</h3> | <h3>Parameters</h3> | ||||
<table border="1" cellpadding="2" cellspacing="0"> | <table border="1" cellpadding="2" cellspacing="0"> | ||||
<tr> | <tr> | ||||
@@ -59,6 +61,16 @@ but is really for debugging build files.</p> | |||||
<td valign="top" align="center">No</td> | <td valign="top" align="center">No</td> | ||||
</tr> | </tr> | ||||
</table> | </table> | ||||
<h3>Parameters specified as nested elements</h3> | |||||
<h4>propertyset</h4> | |||||
<p>You can specify subsets of properties to be echoed with <a | |||||
href="../CoreTypes/propertyset.html">propertyset</a>s.</p> | |||||
<p><em>since Ant 1.6</em>.</p> | |||||
<h3>Examples</h3> | <h3>Examples</h3> | ||||
<blockquote><pre> | <blockquote><pre> | ||||
<echoproperties/> | <echoproperties/> | ||||
@@ -79,6 +91,14 @@ allow the build to continue.</p> | |||||
<echoproperties prefix="java."/> | <echoproperties prefix="java."/> | ||||
</pre></blockquote> | </pre></blockquote> | ||||
<p>List all properties beginning with "java."</p> | <p>List all properties beginning with "java."</p> | ||||
<blockquote><pre> | |||||
<echoproperties> | |||||
<propertyset> | |||||
<propertyref prefix="java."/> | |||||
</propertyset> | |||||
</echoproperties> | |||||
</pre></blockquote> | |||||
<p>List all properties beginning with "java."</p> | |||||
<hr> | <hr> | ||||
<p align="center">Copyright © 2002-2003 Apache Software Foundation. All rights | <p align="center">Copyright © 2002-2003 Apache Software Foundation. All rights | ||||
@@ -64,6 +64,14 @@ | |||||
<echoproperties destfile="test-prefix.properties" prefix="a." /> | <echoproperties destfile="test-prefix.properties" prefix="a." /> | ||||
</target> | </target> | ||||
<target name="testEchoPrefixAsPropertyset" depends="setup"> | |||||
<echoproperties destfile="test-prefix.properties"> | |||||
<propertyset> | |||||
<propertyref prefix="a."/> | |||||
</propertyset> | |||||
</echoproperties> | |||||
</target> | |||||
<target name="cleanup"> | <target name="cleanup"> | ||||
<delete file="test.properties" failonerror="no" /> | <delete file="test.properties" failonerror="no" /> | ||||
<delete file="test-prefix.properties" failonerror="no" /> | <delete file="test-prefix.properties" failonerror="no" /> | ||||
@@ -1,7 +1,7 @@ | |||||
/* | /* | ||||
* The Apache Software License, Version 1.1 | * The Apache Software License, Version 1.1 | ||||
* | * | ||||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||||
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights | |||||
* reserved. | * reserved. | ||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
@@ -65,12 +65,14 @@ import java.io.Writer; | |||||
import java.util.Enumeration; | import java.util.Enumeration; | ||||
import java.util.Hashtable; | import java.util.Hashtable; | ||||
import java.util.Properties; | import java.util.Properties; | ||||
import java.util.Vector; | |||||
import javax.xml.parsers.DocumentBuilder; | import javax.xml.parsers.DocumentBuilder; | ||||
import javax.xml.parsers.DocumentBuilderFactory; | import javax.xml.parsers.DocumentBuilderFactory; | ||||
import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
import org.apache.tools.ant.Task; | import org.apache.tools.ant.Task; | ||||
import org.apache.tools.ant.types.EnumeratedAttribute; | import org.apache.tools.ant.types.EnumeratedAttribute; | ||||
import org.apache.tools.ant.types.PropertySet; | |||||
import org.apache.tools.ant.util.CollectionUtils; | import org.apache.tools.ant.util.CollectionUtils; | ||||
import org.apache.tools.ant.util.DOMElementWriter; | import org.apache.tools.ant.util.DOMElementWriter; | ||||
import org.w3c.dom.Document; | import org.w3c.dom.Document; | ||||
@@ -153,11 +155,7 @@ public class EchoProperties extends Task { | |||||
*/ | */ | ||||
private boolean failonerror = true; | private boolean failonerror = true; | ||||
/** | |||||
* Prefix string controls which properties to save. | |||||
*/ | |||||
private String prefix = null; | |||||
private Vector propertySets = new Vector(); | |||||
private String format = "text"; | private String format = "text"; | ||||
@@ -207,9 +205,20 @@ public class EchoProperties extends Task { | |||||
*@param prefix The new prefix value | *@param prefix The new prefix value | ||||
*/ | */ | ||||
public void setPrefix(String prefix) { | public void setPrefix(String prefix) { | ||||
this.prefix = prefix; | |||||
PropertySet ps = new PropertySet(); | |||||
ps.setProject(getProject()); | |||||
ps.appendPrefix(prefix); | |||||
addPropertyset(ps); | |||||
} | } | ||||
/** | |||||
* A set of properties to write. | |||||
* | |||||
* @since Ant 1.6 | |||||
*/ | |||||
public void addPropertyset(PropertySet ps) { | |||||
propertySets.addElement(ps); | |||||
} | |||||
public void setFormat(FormatAttribute ea) { | public void setFormat(FormatAttribute ea) { | ||||
format = ea.getValue(); | format = ea.getValue(); | ||||
@@ -234,10 +243,10 @@ public class EchoProperties extends Task { | |||||
/* load properties from file if specified, otherwise | /* load properties from file if specified, otherwise | ||||
use Ant's properties */ | use Ant's properties */ | ||||
if(inFile == null) { | |||||
if (inFile == null && propertySets.size() == 0) { | |||||
// add ant properties | // add ant properties | ||||
CollectionUtils.putAll(allProps, getProject().getProperties()); | CollectionUtils.putAll(allProps, getProject().getProperties()); | ||||
} else { | |||||
} else if (inFile != null) { | |||||
if (inFile.exists() && inFile.isDirectory()) { | if (inFile.exists() && inFile.isDirectory()) { | ||||
String message = "srcfile is a directory!"; | String message = "srcfile is a directory!"; | ||||
if (failonerror) { | if (failonerror) { | ||||
@@ -291,6 +300,12 @@ public class EchoProperties extends Task { | |||||
} | } | ||||
} | } | ||||
Enumeration enum = propertySets.elements(); | |||||
while (enum.hasMoreElements()) { | |||||
PropertySet ps = (PropertySet) enum.nextElement(); | |||||
CollectionUtils.putAll(allProps, ps.getProperties()); | |||||
} | |||||
OutputStream os = null; | OutputStream os = null; | ||||
try { | try { | ||||
if (destfile == null) { | if (destfile == null) { | ||||
@@ -355,9 +370,7 @@ public class EchoProperties extends Task { | |||||
while (enum.hasMoreElements()) { | while (enum.hasMoreElements()) { | ||||
String name = enum.nextElement().toString(); | String name = enum.nextElement().toString(); | ||||
String value = allProps.get(name).toString(); | String value = allProps.get(name).toString(); | ||||
if (prefix == null || name.indexOf(prefix) == 0) { | |||||
props.put(name, value); | |||||
} | |||||
props.put(name, value); | |||||
} | } | ||||
if ("text".equals(format)) { | if ("text".equals(format)) { | ||||
@@ -1,7 +1,7 @@ | |||||
/* | /* | ||||
* The Apache Software License, Version 1.1 | * The Apache Software License, Version 1.1 | ||||
* | * | ||||
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights | |||||
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights | |||||
* reserved. | * reserved. | ||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
@@ -188,6 +188,14 @@ public class EchoPropertiesTest extends BuildFileTest { | |||||
props.getProperty("b.set")); | props.getProperty("b.set")); | ||||
} | } | ||||
public void testEchoPrefixAsPropertyset() throws Exception { | |||||
executeTarget( "testEchoPrefixAsPropertyset" ); | |||||
Properties props=loadPropFile(PREFIX_OUTFILE); | |||||
assertEquals("prefix didn't include 'a.set' property","true",props.getProperty("a.set")); | |||||
assertNull("prefix failed to filter out property 'b.set'", | |||||
props.getProperty("b.set")); | |||||
} | |||||
protected Properties loadPropFile(String relativeFilename) | protected Properties loadPropFile(String relativeFilename) | ||||
throws IOException { | throws IOException { | ||||
File f = createRelativeFile( relativeFilename ); | File f = createRelativeFile( relativeFilename ); | ||||