Browse Source

add regex attribute to echoproperties task.

Bugzilla 40019.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@467828 13f79535-47bb-0310-9956-ffa450edef68
master
Antoine Levy-Lambert 18 years ago
parent
commit
74f550099b
5 changed files with 102 additions and 11 deletions
  1. +3
    -0
      WHATSNEW
  2. +28
    -6
      docs/manual/OptionalTasks/echoproperties.html
  3. +12
    -0
      src/etc/testcases/taskdefs/optional/echoproperties.xml
  4. +43
    -5
      src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java
  5. +16
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java

+ 3
- 0
WHATSNEW View File

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



+ 28
- 6
docs/manual/OptionalTasks/echoproperties.html View File

@@ -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>
&lt;/propertyset&gt;
&lt;/echoproperties&gt;
</pre></blockquote>
<p>List all properties beginning with "java."</p>
<p>This again lists all properties beginning with "java." using a nested
<tt>&lt;/propertyset&gt;</tt> which is an equivalent but longer way.</p>
<blockquote><pre>
&lt;echoproperties regex=".*ant.*"/&gt;
</pre></blockquote>
<p>Lists all properties that contain "ant" in their names.
The equivalent snippet with <tt>&lt;/propertyset&gt;</tt> is:</p>
<blockquote><pre>
&lt;echoproperties&gt;
&lt;propertyset&gt;
&lt;propertyref regex=".*ant.*"/&gt;
&lt;/propertyset&gt;
&lt;/echoproperties&gt;
</pre></blockquote>





+ 12
- 0
src/etc/testcases/taskdefs/optional/echoproperties.xml View File

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


+ 43
- 5
src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java View File

@@ -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>&lt;echoproperties prefix="ant." /&gt;</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>&lt;echoproperties prefix=".*ant.*" /&gt;</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();



+ 16
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java View File

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


Loading…
Cancel
Save