0

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.

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
ktraos
  • 127
  • 3
  • 8

1 Answers1

2

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".

Dante WWWW
  • 2,729
  • 1
  • 17
  • 33
  • Thanks coolcfan for your answer.But it did not work for me.I know to read the documents ok.I have seen this piece of code some where in additional ant tasks.My requirement is as follows – ktraos May 23 '13 at 10:03
  • My requirement is as follows If the name and value pair for JavaProjectName is there then it has to assign the value for importJavaProject ,else it should not throw error.It has to assign the remaining values to the names.If the importJavaProject value is not assigned then it should not import the projects.This is my requirement.If you can please help me out. – ktraos May 23 '13 at 10:11
  • @Thiru what do u mean by "it has to assign the remaining values to the names"? Also, if you don't need `else` handling, if you just want to skip setting the property `importJavaProject` when `JavaProjectName` hasn't been set, then the correct usage of `` above is enough. – Dante WWWW May 24 '13 at 10:26
  • Changed my code to support above scenario.my problem is resolved now.Thank you very much for your support. – ktraos May 29 '13 at 03:54