Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise an error if a variable is not defined in Ant

Tags:

ant

I need the user to define a variable at compile time, I still have to decide if it will be an environment variable or a property (ant -Dname=value).

How can I raise an error at compile-time if the variable has not been defined by the user?

like image 330
Andrea Spadaccini Avatar asked Mar 28 '11 15:03

Andrea Spadaccini


2 Answers

Just add:

<fail unless="var1" message="var1 is not set"/>

If var1 is not set the build will fail.

like image 158
dogbane Avatar answered Oct 03 '22 20:10

dogbane


For the first question:

If the variable is meant for environment-specific conditions outside of the application, then make it an environment (OS) variable. Otherwise, make it a property. Obviously, this doesn't fully answer your question since you still have to make a determination of what it means (for your app or system) to say that a variable is environment-specific.

Another guide would be to ask yourself whether you can (or will have) more than one application that depend on different values of the same variable, all possibly being deployed on the same system. In such a case, we cannot use an environment variable, with property-based variables the only way to go.

For the second question:

Use Ant's built-in Fail task to abort the build if a condition or property is not set. From Ant's documentation on the Fail task, you can get an idea on how to go about it if you use a property-based variable:

  <fail>
     <condition>
       <not>
         <isset property="thisdoesnotexist"/>
       </not>
     </condition>
   </fail>

If you decide to use an environment variable, you simply use the environment attribute of the Property task to tap into environment (OS) variables just as if they were build properties (pls refer to the applicable documentation for examples.)

Hope it helps.

like image 30
luis.espinal Avatar answered Oct 03 '22 20:10

luis.espinal