Bugzilla 40019. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@467828 13f79535-47bb-0310-9956-ffa450edef68master
@@ -24,6 +24,9 @@ Other changes: | |||
* removed dependence on sun.misc.UUEncoder for UUMailer. | |||
* added regex attribute to the echoproperties task. | |||
Bugzilla 40019. | |||
Changes from Ant 1.7.0Beta2 to Ant 1.7.0Beta3 | |||
============================================= | |||
@@ -58,9 +58,15 @@ files.</p> | |||
</td> | |||
<td valign="top" align="center">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">regex</td> | |||
<td valign="top"> | |||
a regular expression which is used to filter the | |||
properties | |||
only those properties whose names match it will be echoed. | |||
</td> | |||
<td valign="top" align="center">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">failonerror</td> | |||
<td valign="top">By default, the "failonerror" attribute is enabled. | |||
@@ -70,7 +76,6 @@ files.</p> | |||
statement, and the build will continue without failure from this task.</td> | |||
<td valign="top" align="center">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">format</td> | |||
<td valign="top">One of <code>text</code> or <code>xml</code>. | |||
@@ -84,7 +89,11 @@ files.</p> | |||
<h4>propertyset</h4> | |||
<p>You can specify subsets of properties to be echoed with <a | |||
href="../CoreTypes/propertyset.html">propertyset</a>s.</p> | |||
href="../CoreTypes/propertyset.html">propertyset</a>s. Using | |||
<tt>propertyset</tt>s gives more control on which properties will be | |||
picked up. The attributes <tt>prefix</tt> and <tt>regex</tt> are just | |||
shorcuts that use <tt>propertyset</tt>s internally. | |||
</p> | |||
<p><em>since Ant 1.6</em>.</p> | |||
@@ -115,7 +124,20 @@ allow the build to continue.</p> | |||
</propertyset> | |||
</echoproperties> | |||
</pre></blockquote> | |||
<p>List all properties beginning with "java."</p> | |||
<p>This again lists all properties beginning with "java." using a nested | |||
<tt></propertyset></tt> which is an equivalent but longer way.</p> | |||
<blockquote><pre> | |||
<echoproperties regex=".*ant.*"/> | |||
</pre></blockquote> | |||
<p>Lists all properties that contain "ant" in their names. | |||
The equivalent snippet with <tt></propertyset></tt> is:</p> | |||
<blockquote><pre> | |||
<echoproperties> | |||
<propertyset> | |||
<propertyref regex=".*ant.*"/> | |||
</propertyset> | |||
</echoproperties> | |||
</pre></blockquote> | |||
@@ -94,6 +94,18 @@ | |||
</echoproperties> | |||
</target> | |||
<target name="testWithPrefixAndRegex" depends="setup"> | |||
<echoproperties prefix="ant." regex=".*ant.*"/> | |||
</target> | |||
<target name="testWithEmptyPrefixAndRegex" depends="setup"> | |||
<echoproperties prefix="" regex=""/> | |||
</target> | |||
<target name="testWithRegex" depends="setup"> | |||
<echoproperties regex=".*ant.*"/> | |||
</target> | |||
<target name="cleanup"> | |||
<delete file="test.properties" failonerror="no" /> | |||
<delete file="test-prefix.properties" failonerror="no" /> | |||
@@ -129,6 +129,13 @@ public class EchoProperties extends Task { | |||
private String format = "text"; | |||
private String prefix; | |||
/** | |||
* @since Ant 1.7 | |||
*/ | |||
private String regex; | |||
/** | |||
* Sets the input file. | |||
* | |||
@@ -163,19 +170,20 @@ public class EchoProperties extends Task { | |||
/** | |||
* If the prefix is set, then only properties which start with this | |||
* prefix string will be recorded. If this is never set, or it is set | |||
* to an empty string or <tt>null</tt>, then all properties will be | |||
* recorded. <P> | |||
* prefix string will be recorded. If regex is not set and if this | |||
* is never set, or it is set to an empty string or <tt>null</tt>, | |||
* then all properties will be recorded. <P> | |||
* | |||
* For example, if the property is set as: | |||
* For example, if the attribute is set as: | |||
* <PRE><echoproperties prefix="ant." /></PRE> | |||
* then the property "ant.home" will be recorded, but "ant-example" | |||
* will not. | |||
* | |||
*@param prefix The new prefix value | |||
* @param prefix The new prefix value | |||
*/ | |||
public void setPrefix(String prefix) { | |||
if (prefix != null && prefix.length() != 0) { | |||
this.prefix = prefix; | |||
PropertySet ps = new PropertySet(); | |||
ps.setProject(getProject()); | |||
ps.appendPrefix(prefix); | |||
@@ -183,6 +191,31 @@ public class EchoProperties extends Task { | |||
} | |||
} | |||
/** | |||
* If the regex is set, then only properties whose names match it | |||
* will be recorded. If prefix is not set and if this is never set, | |||
* or it is set to an empty string or <tt>null</tt>, then all | |||
* properties will be recorded.<P> | |||
* | |||
* For example, if the attribute is set as: | |||
* <PRE><echoproperties prefix=".*ant.*" /></PRE> | |||
* then the properties "ant.home" and "user.variant" will be recorded, | |||
* but "ant-example" will not. | |||
* | |||
* @param regex The new regex value | |||
* | |||
* @since Ant 1.7 | |||
*/ | |||
public void setRegex(String regex) { | |||
if (regex != null && regex.length() != 0) { | |||
this.regex = regex; | |||
PropertySet ps = new PropertySet(); | |||
ps.setProject(getProject()); | |||
ps.appendRegex(regex); | |||
addPropertyset(ps); | |||
} | |||
} | |||
/** | |||
* A set of properties to write. | |||
* @param ps the property set to write | |||
@@ -209,6 +242,7 @@ public class EchoProperties extends Task { | |||
/** | |||
* @see EnumeratedAttribute#getValues() | |||
* @return accepted values | |||
*/ | |||
public String[] getValues() { | |||
return formats; | |||
@@ -221,6 +255,10 @@ public class EchoProperties extends Task { | |||
*@exception BuildException trouble, probably file IO | |||
*/ | |||
public void execute() throws BuildException { | |||
if (prefix != null && regex != null) { | |||
throw new BuildException("Please specify either prefix" | |||
+ " or regex, but not both", getLocation()); | |||
} | |||
//copy the properties file | |||
Hashtable allProps = new Hashtable(); | |||
@@ -161,6 +161,22 @@ public class EchoPropertiesTest extends BuildFileTest { | |||
testEchoPrefixVarious("testEchoPrefixAsDoublyNegatedPropertyset"); | |||
} | |||
public void testWithPrefixAndRegex() throws Exception { | |||
expectSpecificBuildException("testWithPrefixAndRegex", | |||
"The target must fail with prefix and regex attributes set", | |||
"Please specify either prefix or regex, but not both"); | |||
} | |||
public void testWithEmptyPrefixAndRegex() throws Exception { | |||
expectLogContaining("testEchoWithEmptyPrefixToLog", "test.property="+TEST_VALUE); | |||
} | |||
public void testWithRegex() throws Exception { | |||
executeTarget("testWithRegex"); | |||
assertDebuglogContaining("ant.home="); | |||
assertDebuglogContaining("user.variant="); | |||
} | |||
private void testEchoPrefixVarious(String target) throws Exception { | |||
executeTarget(target); | |||
Properties props = loadPropFile(PREFIX_OUTFILE); | |||