ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan.Mate...@rzf.fin-nrw.de
Subject AW: Conditionally passing property to <ant>
Date Wed, 30 Apr 2003 05:29:26 GMT
I did a quick hack. 
Extending Property for handling if/unless (copied from Target). 
But I had to modify <ant> for handling this new class.


With the little buildfile 
	<?xml version="1.0" encoding="ISO-8859-1"?>
	<project name="test" basedir="." default="main">
	    <target name="main">
	        <property name="foo" value="foo" />
	        <ant target="print">
	            <cprop name="value" value="${foo}" if="foo" />
	        </ant>
	        <ant target="print">
	            <cprop name="value" value="${bar}" if="bar" />
	        </ant>
	    </target>

	    <target name="print">
	        <echo>${value}</echo>
	    </target>
	</project>

I got

	main:

	print:
	     [echo] foo

	print:
	     [echo] ${value}

	BUILD SUCCESSFUL


which I expected.


Jan Matèrne




--8-<---- diff for Ant.java --8-<------8-<------8-<------8-<------8-<----
---
C:\seu\cvs-downloads\ant\ant\src\main\org\apache\tools\ant\taskdefs\Ant.java
.org.fixcrlf	Wed Apr 30 05:25:27 2003
+++
C:\seu\cvs-downloads\ant\ant\src\main\org\apache\tools\ant\taskdefs\Ant.java
Wed Apr 30 05:21:31 2003
@@ -677,4 +677,19 @@
         }
     }
 
+
+
+
+    public ConditionalProperty createCprop() {
+        if (newProject == null) {
+            reinit();
+        }
+        ConditionalProperty p = new ConditionalProperty(true,
getProject());
+        p.setProject(newProject);
+        p.setTaskName("property");
+        properties.addElement(p);
+        return p;
+    }
+
+
 }
--8-<------8-<-------8-<------8-<------8-<------8-<------8-<------8-<----



--8-<---- diff for taskdefs/default.properties -8-<------8-<------8-<----
---
C:\seu\cvs-downloads\ant\ant\src\main\org\apache\tools\ant\taskdefs\defaults
.properties.org.fixcrlf	Wed Apr 30 05:25:03 2003
+++
C:\seu\cvs-downloads\ant\ant\src\main\org\apache\tools\ant\taskdefs\defaults
.properties	Wed Apr 30 05:19:16 2003
@@ -196,3 +196,5 @@
 copyfile=org.apache.tools.ant.taskdefs.Copyfile
 deltree=org.apache.tools.ant.taskdefs.Deltree
 rename=org.apache.tools.ant.taskdefs.Rename
+
+cprop=org.apache.tools.ant.taskdefs.ConditionalProperty
--8-<------8-<-------8-<------8-<------8-<------8-<------8-<------8-<----



--8-<---- taskdefs/ConditionalProperty.java ----8-<------8-<------8-<----
package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;

public class ConditionalProperty extends Property {

    // same constructor as Property
    public ConditionalProperty() {
        super();
    }
    protected ConditionalProperty(boolean userProperty) {
        super(userProperty);
    }
    protected ConditionalProperty(boolean userProperty, Project fallback) {
        super(userProperty, fallback);
    }


    // if and unless copied from Target class.

    /** The "if" condition to test on execution. */
    private String ifCondition = "";
    /** The "unless" condition to test on execution. */
    private String unlessCondition = "";

    public void setIf(String property) {
        this.ifCondition = (property == null) ? "" : property;
    }

    public void setUnless(String property) {
        this.unlessCondition = (property == null) ? "" : property;
    }


    // no individual implementation necessary so call super.execute()
    // wrapps only the check of if/unless around that call
    public void execute() throws BuildException {
        if (testIfCondition() && testUnlessCondition()) {
            super.execute();
        } else if (!testIfCondition()) {
            project.log(this, "Skipped because property '"
                        + project.replaceProperties(this.ifCondition)
                        + "' not set.", Project.MSG_VERBOSE);
        } else {
            project.log(this, "Skipped because property '"
                        + project.replaceProperties(this.unlessCondition)
                        + "' set.", Project.MSG_VERBOSE);
        }
    }


    private boolean testIfCondition() {
        if ("".equals(ifCondition)) {
            return true;
        }

        String test = project.replaceProperties(ifCondition);
        return project.getProperty(test) != null;
    }

    private boolean testUnlessCondition() {
        if ("".equals(unlessCondition)) {
            return true;
        }
        String test = project.replaceProperties(unlessCondition);
        return project.getProperty(test) == null;
    }


}
--8-<------8-<-------8-<------8-<------8-<------8-<------8-<------8-<----



> -----Urspr√ľngliche Nachricht-----
> Von: Donald Strong [mailto:dstrong@agentissoftware.com]
> Gesendet am: Mittwoch, 30. April 2003 02:51
> An: Ant Users List
> Betreff: RE: Conditionally passing property to <ant>
> 
> Hi Chris,
> 
> I have had this exact problem recently.
> The solution is not pretty.
> 
> ========= build.xml =========
> 
>     <!-- Property prop1 - prop2 may or maynot be set. -->
>     <ant dir="${the.dir}" inheritAll="false" >
>       <property name="prop1.temp" value="${prop1} />
>       <property name="prop2.temp" value="${prop2} />
>     </ant>
> 
> ======= ${the.dir}/build.xml =====
> 
>   <!-- I assume "init" is invoked by your target -->
>   <target name="init" >
> 
>     <!-- prop1.temp contains "${prop1}" if prop1 was undefined. -->
>     <!-- This is a hack to produce an undefined property.       -->
>     <condition property="prop1" value="${prop1.temp}" >
>         <and>
>             <isset property="prop1.temp" />
>             <not> <equals arg1="${prop1.temp}" 
> arg2="$${prop1}" /> </not>
>         </and>
>     </condition>
>     <condition property="prop2" value="${prop2.temp}" >
>         <and>
>             <isset property="prop2.temp" />
>             <not> <equals arg1="${prop2.temp}" 
> arg2="$${prop2}" /> </not>
>         </and>
>     </condition>
>   </target>
> 
> =================================
> 
> In my opinion -
> 
>    <ant ... >
>      <property name="prop1" value="${prop1}" if="prop1" />
>    </ant>
> 
> is a simple and elegant solution to this problem.
> 
> Regards
> Donald.
> 
> 
> > -----Original Message-----
> > From: Christopher Lenz [mailto:cmlenz@gmx.de]
> > Sent: Tuesday, 29 April 2003 8:58 PM
> > To: Ant Users List
> > Subject: Conditionally passing property to <ant>
> > 
> > 
> > Hi folks,
> > 
> > I'd like to call another build file with a set of optional 
> properties. 
> > If defined, those properties point to directories. In 
> addition, I cannot 
> > use property inheritance.
> > 
> > The naive attempt
> > 
> >    <ant dir="${the.dir}" inheritAll="false">
> >      <property name="mydir" value="${mydir}"/>
> >    </ant>
> > 
> > works fine if 'mydir' has been defined, but of course fails when it 
> > hasn't -- because the property is set to the literal ${mydir}. The 
> > called script checks whether the property has been set, and only 
> > executes some tasks if it has.
> > 
> > So what I'd need would basically be:
> > 
> >    <ant dir="${the.dir}" inheritAll="false">
> >      <property name="mydir" value="${mydir}" if="mydir"/>
> >    </ant>
> > 
> > Any ideas?
> > 
> > -chris
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message