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?
Just add:
<fail unless="var1" message="var1 is not set"/>
If var1
is not set the build will fail.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With