Browse Source

Add a magic property that lists the targets that have been specified in order to run the current project. Based on patch by Colm Smyth (just like rev663051 was). PR 44980

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@663061 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
fa52b460c1
6 changed files with 60 additions and 1 deletions
  1. +4
    -0
      WHATSNEW
  2. +5
    -0
      docs/manual/using.html
  3. +9
    -0
      src/main/org/apache/tools/ant/MagicNames.java
  4. +4
    -1
      src/main/org/apache/tools/ant/Project.java
  5. +21
    -0
      src/main/org/apache/tools/ant/util/CollectionUtils.java
  6. +17
    -0
      src/tests/antunit/core/magic-names-test.xml

+ 4
- 0
WHATSNEW View File

@@ -74,6 +74,10 @@ Other changes:
* a new property ant.project.default-target holds the value of the
current <project>'s default attribute.

* a new property ant.project.invoked-targets holds a comma separated
list of the targets that have been specified on the command line
(the IDE, an <ant> task ...) when invoking the current project.

Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================



+ 5
- 0
docs/manual/using.html View File

@@ -311,6 +311,11 @@ ant.project.default-target
the name of the currently executing project's
default target; it is set via the default
attribute of &lt;project&gt;.
ant.project.invoked-targets
a comma separated list of the targets that have
been specified on the command line (the IDE,
an &lt;ant&gt; task ...) when invoking the current
project.
ant.java.version the JVM version Ant detected; currently it can hold
the values &quot;1.2&quot;, &quot;1.3&quot;, &quot;1.4&quot; and &quot;1.5&quot;.
</pre>


+ 9
- 0
src/main/org/apache/tools/ant/MagicNames.java View File

@@ -212,5 +212,14 @@ public final class MagicNames {
public static final String PROJECT_DEFAULT_TARGET
= "ant.project.default-target";

/**
* Name of the property holding a comma separated list of targets
* that have been invoked (from the command line).
*
* Value: {@value}
* @since Ant 1.8.0
*/
public static final String PROJECT_INVOKED_TARGETS
= "ant.project.invoked-targets";
}


+ 4
- 1
src/main/org/apache/tools/ant/Project.java View File

@@ -45,6 +45,7 @@ import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceFactory;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.util.CollectionUtils;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.JavaEnvUtils;
import org.apache.tools.ant.util.StringUtils;
@@ -694,7 +695,7 @@ public class Project implements ResourceFactory {
* no default target.
*/
public void setDefault(String defaultTarget) {
setUserProperty(MagicNames.PROJECT_DEFAULT_TARGET, defaultTarget);
setUserProperty(MagicNames.PROJECT_DEFAULT_TARGET, defaultTarget);
this.defaultTarget = defaultTarget;
}

@@ -1174,6 +1175,8 @@ public class Project implements ResourceFactory {
* @exception BuildException if the build failed.
*/
public void executeTargets(Vector names) throws BuildException {
setUserProperty(MagicNames.PROJECT_INVOKED_TARGETS,
CollectionUtils.flattenToString(names));
getExecutor().executeTargets(this,
(String[]) (names.toArray(new String[names.size()])));
}


+ 21
- 0
src/main/org/apache/tools/ant/util/CollectionUtils.java View File

@@ -17,6 +17,7 @@
*/
package org.apache.tools.ant.util;

import java.util.Collection;
import java.util.Vector;
import java.util.Iterator;
import java.util.Dictionary;
@@ -92,6 +93,26 @@ public class CollectionUtils {
return true;
}

/**
* Creates a comma separated list of all values held in the given
* collection.
*
* @since Ant 1.8.0
*/
public static String flattenToString(Collection c) {
Iterator iter = c.iterator();
boolean first = true;
StringBuffer sb = new StringBuffer();
while (iter.hasNext()) {
if (!first) {
sb.append(",");
}
sb.append(String.valueOf(iter.next()));
first = false;
}
return sb.toString();
}

/**
* Dictionary does not know the putAll method. Please use Map.putAll().
* @param m1 the to directory.


+ 17
- 0
src/tests/antunit/core/magic-names-test.xml View File

@@ -21,6 +21,9 @@

<target name="default target"/>

<target name="setUp"
description="only here to force a second target into testInvokedTargets' list"/>

<target name="testProjectName">
<au:assertPropertyEquals
name="ant.project.name" value="magicnames-test"/>
@@ -31,4 +34,18 @@
name="ant.project.default-target" value="default target"/>
</target>

<target name="testInvokedTargets">
<au:assertPropertyEquals
name="ant.project.invoked-targets" value="setUp,testInvokedTargets"/>
</target>

<target name="nested">
<au:assertPropertyEquals
name="ant.project.invoked-targets" value="nested"/>
</target>

<target name="testInvokedTargetsWithNestedAntcall">
<antcall target="nested"/>
</target>

</project>

Loading…
Cancel
Save