Ant JUnit task: Is there a difference between on, yes, and true? - ant

TL;DR: To enable an Ant feature, is there a difference between on, yes, and true?
I cannot find any documentation regarding this matter. We have some Ant build scripts that seem to randomly pick between: on yes and true to enable various Ant JUnit features.
Example attributes: fork and haltonerror
Ref: http://ant.apache.org/manual/Tasks/junit.html

From http://ant.apache.org/manual/develop.html:
The most common way to write an attribute setter is to use a java.lang.String argument. In this case Ant will pass the literal value (after property expansion) to your task. But there is more! If the argument of you setter method is
boolean, your method will be passed the value true if the value specified in the build file is one of true, yes, or on and false otherwise.
So yes, on and true are the same thing.

Related

Set a system property with ant

I have an ant script which has a taskdef and the task creates an https internet connection and somethin with that SSL stuff is wrong. Thus I want to set the system property javax.net.debug=all to get some more information.
In java I would do this using the -D option, but in ant this is used for ant properties which is not the same as a system property.
If this wouldn't be a taskdef but instead a java task, I could use the sysproperty property, but it is no java-task.
Googling for this is frustratingly complicated because ant properties and system properties in ant are so similar that most search results are about the other (or about the java-task).
Obviously I am not the only one with the problem, but other people's questions that I have found (like here) are unanswered or went for hack (like here).
One way to set such a property is the ANT_OPTS system variable. You have to be very carefully to not simply skim over answers on google that state that options can be set that way, because it sounds so much like not what it does:
The documentation says:
ANT_OPTS - command-line arguments that should be passed to the JVM.
For example, you can define system properties or set the maximum Java
heap size here.
Who what have expected that? ANT_OPTS are options for the JVM and not for ant like the name suggests. The var which is used for ant options is called ANT_ARGS.
Now I can launch ant like this: ANT_OPTS="-Djavax.net.debug=all" ant myTarget and can see tons of log output.
(However this leaves the question open whether such a variable can be set using XML).
You can declare system properties in the xml with <sysproperty key="key" value="value"/>.
http://www.java2s.com/Code/Java/Ant/SetsystempropertiesinAntbuildscript.htm
You can use scripting:
<script language="javascript">
java.lang.System.setProperty('myKey', 'myValue');
</script>

Ant possible to insert cmd line input key-value pairs to match a property file?

I have a property file includes key-value pairs:
key1=value1
key2=value2
...
I have an Ant target called "compute". I would like to override the values by referring to the key like this:
ant compute -Dkey1=this is my value
How would this be possible to do? Thanks.
Try:
ant compute -Dkey1="this is my value"
There are many ways to achieve this (Asking for user input with <input/> task, setting the var in the Global Properties (under Preferences->Ant), etc), but i know only those on Eclipse :S
By the way, if what you want is to change multiple variables-per-setting (like environments settings), you can use different property files, like dev.properties, test.properties and prod.properties, and then hard-code the variable values into those files.
Then you will only need ONE parameter at the beginning, specifying which "set" (file) of variables you wanna pick...
And your parameter will not override any other, it will be straight in the command line, like
ant compute -Denv="test"
or
ant compute -Denv=test
, that must work (unless Ant guide is wrong, and i don't think so).

how to pass in ant.build.clonevm?

I'd like to pass properties(-Dname=val) down to junit&java tasks from the Ant command line. The problem is the -3rd party- build file doesn't pass those properties down.
I was thinking ant.build.clonevm could help, but the manual says
Note that this has to be a system property, so it cannot be specified
on the Ant command line.
So, can I use this for the above purpose, and how? If not, any other alternatives?
Thanks for any hints.
Use the ANT_OPTS environment variable instead.
set ANT_OPTS=-Dxxx=value
xxx is your property

Raise an error if a variable is not defined in 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?
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.

What's the difference between # and $ when accessing Ant properties?

What's the difference between #{property} and ${property} in accessing Ant properties?
Didn't see any obvious note about it in the Ant manual.
${property} is the normal way to access properties. #{property} is used inside macro definitions to access arguments passed to this macro. See the Ant manual on macrodef.
${property} is used to reference Properties.
#{attribute} is used to reference Attributes in <macrodef> Tasks.

Resources