[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ant-dev
Subject:    [PATCH] added unless attribute to target
From:       Stefan Bodewig <bodewig () bost ! de>
Date:       2000-05-30 15:21:08
[Download RAW message or body]

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

<available property="windows" file="c:\autoexec.bat" />
<target name="test" unless="windows">
    ...
</target>

would execute target test only on non Microsoft systems (silly
example, I know).

2. Sometimes you have two alternative ways to do something:

<target name="dowork" depends="way1,way2">
    ...
</target>
<target name="way1" if="way1ispossible">
    ...
</target>
<target name="way2" unless="way1ispossible">
    ...
</target>

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:

<available property="java2" classname="java.lang.ThreadLocal" />
<target name="build" depends="jdbc1,jdbc2" />
<target name="jdbc1" unless="java2">
    ...
</target>
<target name="jdbc2" if="java2">
    ...
</target>

Stefan


[Attachment #3 (text/x-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.</p>
 <p>A target gets executed only once. Even when more targets depend on it (see
 the previous example).</p>
-<p>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 <i>sense</i> this property you should add the <i>if</i> attribute
-with the name of the property that the target should react to, for example</p>
+<p>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
+<i>sense</i> this property you should add the <i>if</i> (or
+<i>unless</i>) attribute with the name of the property that the target
+should react to, for example</p>
 <blockquote>
   <pre>&lt;target name=&quot;build-module-A&quot; \
if=&quot;module-A-present&quot;/&gt;</pre> +  <pre>&lt;target \
name=&quot;build-own-fake-module-A&quot; \
unless=&quot;module-A-present&quot;/&gt;</pre>  </blockquote>
-<p>If no <i>if</i> attribute is present, the target will always be executed.</p>
-<p>It is a good practice to place your <a href="#property">property</a> and <a
-href="#tstamp">tstamp</a> 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".</p>
+<p>If no <i>if</i> and no <i>unless</i> attribute is present, the
+target will always be executed.</p>
+<p>It is a good practice to place your <a href="#tstamp">tstamp</a>
+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".</p>
 <p>A target has the following attributes:</p>
 <table border="1" cellpadding="2" cellspacing="0">
   <tr>
@@ -276,6 +280,12 @@
   <tr>
     <td valign="top">if</td>
     <td valign="top">the name of the property that must be set in order for this
+      target to execute.</td>
+    <td align="center" valign="top">No</td>
+  </tr>
+  <tr>
+    <td valign="top">unless</td>
+    <td valign="top">the name of the property that must not be set in order for this
       target to execute.</td>
     <td align="center" valign="top">No</td>
   </tr>
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;
     }
 }



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic