Search code examples
antant-contrib

ant - if doesn't support the "name" attribute


i have a requirement that as follows. I have a .properties file (with name=value pair) from which i am reading couple of properties. i want to check a particular property exist or not. i am getting the error with if doesn't support the "name" attribute for the following code. where JavaProjectName,projDir are the names getting from the .properties file.

<if name="${JavaProjectName}" exists="true">
<property name="importJavaProject" value="${projDir}/${JavaProjectName}"/>
</if>

can you please tell me where am i doing wrong.


Solution

  • Read the document of <if> task first. It doesn't support the way you wrote.

    It should be:

    <if>
        <isset property="JavaProjectName" />
        <then>
            <property name="importJavaProject" value="${projDir}/${JavaProjectName}"/>
        </then>
    </if>
    

    However, you want to set a property importJavaProject when another property JavaProjectName has been set before (in the build file or in a properties file imported). So, what if JavaProjectName has not been set?

    You should either think of an <else> part, or fail the build.

    If you just want to check for existence and fail the build when it does not exist, just use <fail>:

    <fail unless="JavaProjectName"/>
    

    Also check Condition task and "Supported conditions".


    Addition:

    Also read the question posted by ManMohan in the comment more carefully. For "check the property existence in .properties file", the accepted answer of that question checks both "whether the property has been set" and "whether its value is empty".