Return-Path:
Mailing-List: contact ant-dev-help@jakarta.apache.org; run by ezmlm
Delivered-To: mailing list ant-dev@jakarta.apache.org
Received: (qmail 33877 invoked from network); 30 May 2000 15:21:26 -0000
Received: from smtp01ffm.de.uu.net (192.76.144.150)
by locus.apache.org with SMTP; 30 May 2000 15:21:26 -0000
Received: from sbodewig.bost.de ([195.127.75.69])
by smtp01ffm.de.uu.net (5.5.5/5.5.5) with ESMTP id RAA12842
for ; Tue, 30 May 2000 17:21:15 +0200 (MET DST)
Received: (from bodewig@localhost)
by sbodewig.bost.de (8.9.3/8.9.3) id RAA03082;
Tue, 30 May 2000 17:21:08 +0200
X-Authentication-Warning: sbodewig.bost.de: bodewig set sender to
bodewig@bost.de using -f
To: ant-dev@jakarta.apache.org
Subject: [PATCH] added unless attribute to target
From: Stefan Bodewig
Date: 30 May 2000 17:21:08 +0200
Message-ID:
Lines: 50
User-Agent: Gnus/5.0807 (Gnus v5.8.7) XEmacs/21.1 (Canyonlands)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N
--=-=-=
The appended patch adds an unless attribute to targets. The value of
this attribute is the name of a property. If this property is set the
target will be skipped.
Rationale:
1. Sometimes it easier to check for the absence instead of the presence
of something. For example
...
would execute target test only on non Microsoft systems (silly
example, I know).
2. Sometimes you have two alternative ways to do something:
...
...
...
3. You want to produce different things depending on your build
environment.
Let's assume I want to build a JDBC driver and I have both the 1.0
version and the 2.0 version in the same source tree - I still want to
support JDBC 1.0. The 1.0 version won't compile on JDK 1.2 and the 2.0
version not on JDK 1.1, so:
...
...
Stefan
--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=ant.target_unless.patch
Index: docs/index.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/index.html,v
retrieving revision 1.20
diff -u -r1.20 index.html
--- docs/index.html 2000/05/24 14:35:20 1.20
+++ docs/index.html 2000/05/30 15:03:10
@@ -241,20 +241,24 @@
on B, and B depends on A, so first A is executed, then B, then C, and finally D.
A target gets executed only once. Even when more targets depend on it (see
the previous example).
-A target has also the ability to perform its execution if a property has been
-set. This allows, for example, better control on the building process depending
-on the state of the system (java version, OS, command line properties, etc...).
-To make target sense this property you should add the if attribute
-with the name of the property that the target should react to, for example
+A target has also the ability to perform its execution if (or
+unless) a property has been set. This allows, for example, better
+control on the building process depending on the state of the system
+(java version, OS, command line properties, etc...). To make target
+sense this property you should add the if (or
+unless) attribute with the name of the property that the target
+should react to, for example
<target name="build-module-A" if="module-A-present"/>
+ <target name="build-own-fake-module-A" unless="module-A-present"/>
-If no if attribute is present, the target will always be executed.
-It is a good practice to place your property and tstamp tasks in a so called initialization target, on which
-all other targets depend. Make sure that that target is always the first one in
-the depends list of the other targets. In this manual, most initialization targets
-have the name "init".
+If no if and no unless attribute is present, the
+target will always be executed.
+It is a good practice to place your tstamp
+tasks in a so called initialization target, on which all other targets
+depend. Make sure that that target is always the first one in the
+depends list of the other targets. In this manual, most initialization
+targets have the name "init".
A target has the following attributes:
@@ -276,6 +280,12 @@
if |
the name of the property that must be set in order for this
+ target to execute. |
+ No |
+
+
+ unless |
+ the name of the property that must not be set in order for this
target to execute. |
No |
Index: src/main/org/apache/tools/ant/ProjectHelper.java
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java,v
retrieving revision 1.12
diff -u -r1.12 ProjectHelper.java
--- src/main/org/apache/tools/ant/ProjectHelper.java 2000/04/26 19:09:17 1.12
+++ src/main/org/apache/tools/ant/ProjectHelper.java 2000/05/30 15:03:12
@@ -264,7 +264,8 @@
public void init(String tag, AttributeList attrs) throws SAXParseException {
String name = null;
String depends = "";
- String cond = null;
+ String ifCond = null;
+ String unlessCond = null;
String id = null;
for (int i = 0; i < attrs.getLength(); i++) {
@@ -276,7 +277,9 @@
} else if (key.equals("depends")) {
depends = value;
} else if (key.equals("if")) {
- cond = value;
+ ifCond = value;
+ } else if (key.equals("unless")) {
+ unlessCond = value;
} else if (key.equals("id")) {
id = value;
} else {
@@ -290,7 +293,8 @@
target = new Target();
target.setName(name);
- target.setCondition(cond);
+ target.setIf(ifCond);
+ target.setUnless(unlessCond);
project.addTarget(name, target);
if (id != null && !id.equals(""))
Index: src/main/org/apache/tools/ant/Target.java
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/Target.java,v
retrieving revision 1.4
diff -u -r1.4 Target.java
--- src/main/org/apache/tools/ant/Target.java 2000/04/26 19:09:17 1.4
+++ src/main/org/apache/tools/ant/Target.java 2000/05/30 15:03:12
@@ -65,7 +65,8 @@
public class Target {
private String name;
- private String condition = "";
+ private String ifCondition = "";
+ private String unlessCondition = "";
private Vector dependencies = new Vector(2);
private Vector tasks = new Vector(5);
private Project project;
@@ -108,12 +109,16 @@
return dependencies.elements();
}
- public void setCondition(String property) {
- this.condition = (property == null) ? "" : property;
+ public void setIf(String property) {
+ this.ifCondition = (property == null) ? "" : property;
}
+ public void setUnless(String property) {
+ this.unlessCondition = (property == null) ? "" : property;
+ }
+
public void execute() throws BuildException {
- if (("".equals(this.condition)) || (project.getProperty(this.condition) != null)) {
+ if (testIfCondition() && testUnlessCondition()) {
Enumeration enum = tasks.elements();
while (enum.hasMoreElements()) {
Task task = (Task) enum.nextElement();
@@ -125,8 +130,20 @@
throw exc;
}
}
+ } else if (!testIfCondition()) {
+ project.log("Skipped because property '" + this.ifCondition + "' not set.", this.name, Project.MSG_VERBOSE);
} else {
- project.log("Skipped because property '" + this.condition + "' not set.", this.name, Project.MSG_VERBOSE);
+ project.log("Skipped because property '" + this.unlessCondition + "' set.", this.name, Project.MSG_VERBOSE);
}
+ }
+
+ private boolean testIfCondition() {
+ return "".equals(ifCondition)
+ || project.getProperty(ifCondition) != null;
+ }
+
+ private boolean testUnlessCondition() {
+ return "".equals(unlessCondition)
+ || project.getProperty(unlessCondition) == null;
}
}
--=-=-=--