From fa52b460c15f968c16188d6d6241627eaa68afe7 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 4 Jun 2008 10:58:23 +0000 Subject: [PATCH] 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 --- WHATSNEW | 4 ++++ docs/manual/using.html | 5 +++++ src/main/org/apache/tools/ant/MagicNames.java | 9 ++++++++ src/main/org/apache/tools/ant/Project.java | 5 ++++- .../tools/ant/util/CollectionUtils.java | 21 +++++++++++++++++++ src/tests/antunit/core/magic-names-test.xml | 17 +++++++++++++++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/WHATSNEW b/WHATSNEW index 6b2233a81..6d4211f88 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -74,6 +74,10 @@ Other changes: * a new property ant.project.default-target holds the value of the current '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 task ...) when invoking the current project. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/using.html b/docs/manual/using.html index 3ceb05378..551fd1ae8 100644 --- a/docs/manual/using.html +++ b/docs/manual/using.html @@ -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 <project>. +ant.project.invoked-targets + 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. ant.java.version the JVM version Ant detected; currently it can hold the values "1.2", "1.3", "1.4" and "1.5". diff --git a/src/main/org/apache/tools/ant/MagicNames.java b/src/main/org/apache/tools/ant/MagicNames.java index 86a791828..36efb3570 100644 --- a/src/main/org/apache/tools/ant/MagicNames.java +++ b/src/main/org/apache/tools/ant/MagicNames.java @@ -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"; } diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index f2f8ffa53..9f69a1d72 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -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()]))); } diff --git a/src/main/org/apache/tools/ant/util/CollectionUtils.java b/src/main/org/apache/tools/ant/util/CollectionUtils.java index 7a8cf1f68..375548357 100644 --- a/src/main/org/apache/tools/ant/util/CollectionUtils.java +++ b/src/main/org/apache/tools/ant/util/CollectionUtils.java @@ -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. diff --git a/src/tests/antunit/core/magic-names-test.xml b/src/tests/antunit/core/magic-names-test.xml index cc82088ad..88d8b2ea2 100644 --- a/src/tests/antunit/core/magic-names-test.xml +++ b/src/tests/antunit/core/magic-names-test.xml @@ -21,6 +21,9 @@ + + @@ -31,4 +34,18 @@ name="ant.project.default-target" value="default target"/> + + + + + + + + + + + +