Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

Conditional Task Execution with Ant

I'm trying to conditionally include my test jar files based on an Ant property. My problem is that the "if" attribute of the <task> element only accounts for the property being present or not. I'd love to be able to specify:

ant -Denable.cactus=false

But Ant seemingly executes my task if a property is present - so even though the value is false, it still executes. Any ideas? Here's my task:

<target name="copy-test-jars" depends="init" if="enable.cactus"
    description="Copy test-related JAR files to WEB-INF/lib">
    <echo>Copying Cactus, StrutsTestCase and JUnit JARs</echo>

    <mkdir dir="${webapp.target}/WEB-INF/lib"/>
    <!-- Copy jars -->
    <copy todir="${webapp.target}/WEB-INF/lib">
        <fileset dir="${strutstestcase.dir}" includes="*.jar"/>
        <fileset dir="${cactus.dir}">
            <include name="*.jar"/>
            <exclude name="commons-logging.jar"/>
            <exclude name="log4j-*.jar"/>
            <exclude name="servlet.jar"/>
        </fileset>
        <fileset dir="${env.ANT_HOME}/lib" includes="junit.jar"/>
        <fileset dir="${env.ANT_HOME}" includes="junit-noframes.xsl"/>
    </copy>
</target>

BTW, this XML was made web-savvy by the E2 Source Code Formatter - a must-have bookmark. I got this tip from The FuzzyBlog!.

Posted in Java at Jan 15 2003, 11:19:10 AM MST 6 Comments
Comments:

The if= and unless= attributes do not test the values of the properties, they test for the presence or absence of the attribute (respectivly). If the property has had a value set it will always trip if=, regardless of the value of the property. So I would recommend changing your target to be unless="disable.cactus" and pass in ant disable.cactus=true.

Posted by Danno Ferrin on January 15, 2003 at 04:50 PM MST #

Or, use <condition> to test the actual string value of a property to set another property which will be used with if/unless. Also, your original 'ant enable.cactus=false" is incorrect syntax. It should be 'ant -Denable.cactus=false".

Posted by Erik Hatcher on January 16, 2003 at 07:17 AM MST #

Doh - I've updated the syntax - I swear it was write when I typed it on the command line ;-) I'll look into the <condition> task - good think I have these comments e-mail enabled, or I never would've known what you were implying!

Posted by Matt Raible on January 16, 2003 at 07:34 AM MST #

Erik - your solution solved my problem. I changed to using a deploy-cactus property and then in my init task, I have the following: <code> <!-- Set true or false for enabling cactus --> <condition property="enable.cactus"> &nbsp;&nbsp;<istrue value="${deploy-cactus}"/> </condition> </code> Thanks!

Posted by Matt Raible on January 16, 2003 at 08:35 AM MST #

Check out the: http://sourceforge.net/projects/ant-contrib It gives you the if task so you can do things like: <code> <if> &nbsp;&nbsp;<equals arg1="${deploy-cactus}" arg2="true"/> &nbsp;&nbsp;<then> &nbsp;&nbsp;<echo>Copying Cactus, StrutsTestCase and JUnit JARs</echo> &nbsp;&nbsp;<mkdir dir="${webapp.target}/WEB-INF/lib"/> &nbsp;&nbsp;<!-- Copy jars --> &nbsp;&nbsp;<copy todir="${webapp.target}/WEB-INF/lib"> &nbsp;&nbsp;&nbsp;&nbsp;<fileset dir="${strutstestcase.dir}" includes="*.jar"/> &nbsp;&nbsp;&nbsp;&nbsp;<fileset dir="${cactus.dir}"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<include name="*.jar"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<exclude name="commons-logging.jar"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<exclude name="log4j-*.jar"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<exclude name="servlet.jar"/> &nbsp;&nbsp;&nbsp;&nbsp;</fileset> &nbsp;&nbsp;&nbsp;&nbsp;<fileset dir="${env.ANT_HOME}/lib" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;includes="junit.jar"/> &nbsp;&nbsp;&nbsp;&nbsp;<fileset dir="${env.ANT_HOME}" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;includes="junit-noframes.xsl"/> &nbsp;&nbsp;</copy> &nbsp;&nbsp;</then> </if> </code>

Posted by Anonymous on January 17, 2003 at 06:08 AM MST #

aaa

Posted by 155.70.39.45 on February 09, 2007 at 07:41 AM MST #

Post a Comment:
  • HTML Syntax: Allowed