How to conditionally specify a compiler argument in the javac task - ant

I'm trying to write a target like so
<!-- Properties that must be set when invoking via antcall:
DestinationDir Directory to compile classes to.
ReleaseVer Java version this is being compiled for (i.e. 8, 9, etc). Must be at least 8.
Debug Whether to compile with debug information. Either 'on' or 'off'.
-->
<target name="java-support-compile" description="compile JavaSupport source">
<mkdir dir="${DestinationDir}"/>
<condition property="ModulesSupported">
<not>
<equals arg1="${ReleaseVer}" arg2="8" />
</not>
</condition>
<!-- Compile GSSUtilWrapper separately, since we can't use the 'release' option when referencing the sun.security.jgss.GSSUtil package,
and we need to add an --add-exports option to access the java.security.jgss module for ${ReleaseVer} > 9 -->
<javac srcdir="${javasupport.src.dir}" source="1.${ReleaseVer}" target="1.${ReleaseVer}" debug="${Debug}" destdir="${DestinationDir}">
<include name="${GSSUtilWrapperFile}"/>
<compilerarg if="${ModulesSupported}" line="--add-exports java.security.jgss/sun.security.jgss=ALL-UNNAMED" />
</javac>
<javac srcdir="${javasupport.src.dir}" release="${ReleaseVer}" debug="${Debug}" destdir="${DestinationDir}">
<exclude name="${GSSUtilWrapperFile}"/>
</javac>
</target>
The error I'm getting is compilerarg doesn't support the "if" attribute. I need it to be conditional as if I pass in ReleaseVer=8, I get the error error: option --add-exports not allowed with target 8
I got that syntax from http://ant-contrib.sourceforge.net/compilerarg.html, but I didn't realize this wasn't in core ant (and I don't want to install anything else if possible).
How can I do this in standard ant?

One option, perhaps not the cleanest, would be to modify your <condition> task to set the text of the compiler arg, rather than a boolean, and use that. Something like this:
<condition property="javac_arg"
value="--add-exports java.security.jgss/sun.security.jgss=ALL-UNNAMED"
else="">
<not>
<equals arg1="${ReleaseVer}" arg2="8" />
</not>
</condition>
<javac compiler="modern" srcdir="." includeantruntime="no">
<compilerarg line="${javac_arg}" />
</javac>
Note the else parameter, which ensures the arg passed to javac is empty when the condition is false.

Related

Getting condition doesn't support the nested "then" element in <condition>

I'm new to Ant/Apache. When I tried to use <condition> tag in XML it's throwing an error. condition doesn't support the nested "then" element. Here is my code
<target name="determine-ae-build">
<condition property="ApplicationName">
<equals arg1="${ApplicationName}" arg2="new"/>
<then>
<echo>3.9 Robots Config Copied</echo>
</then>
<else>
<condition property="ApplicationName">
<equals arg1="${ApplicationName}" arg2="old"/>
<then>
<echo>3.8 Robots Config Copied</echo>
</then>
<else>
<echo>3.9 Robots Config Copied</echo>
</else>
</condition>
</else>
</condition>
</target>
I've tried with IF also but since my Ant version is not supporting to do this. Can someone help to resolve this issue. Thanks! in advance
The condition task simply sets a property; it doesn't contain nested build logic. The property that it sets can later be used to control which targets are executed.
While you can use antcontrib's extra if, then, and else tasks to accomplish something like what you showed in your example, I'd recommend sticking to the native Ant approach, which relies on target dependencies and uses separate targets to control build logic:
<project name="build" basedir="." default="build">
<target name="build" depends="copy-3.8,copy-3.9" />
<target name="copy-3.8" depends="determine-ae-build" if="copy.old">
<echo>3.8 Robots Config Copied</echo>
</target>
<target name="copy-3.9" depends="determine-ae-build" unless="copy.old">
<echo>3.9 Robots Config Copied</echo>
</target>
<target name="determine-ae-build">
<condition property="copy.old">
<equals arg1="${ApplicationName}" arg2="old"/>
</condition>
</target>
</project>
With the above script, you would run ant build (possibly with -DApplicationName=old). The build target depends on both copy targets, both of which depend on determine-ae-build. The determine-ae-build target will therefore run first. If ApplicationName is set to "old" (either from a properties file that has been loaded, or from being provided in command line with -DApplicationName=old) then the property copy.old will be set to true. Otherwise it will remain unset.
Then copy-3.8 and copy-3.9 will be called. If copy.old is is true, copy-3.8 will run. Otherwise, it will be skipped. copy-3.9 has no condition so it will run no matter what.
Lastly, the build target will execute because it was the original target called from the command line, but it contains no actual steps so the build will finish.
<target name="prepare-copy" description="copy file based on condition" depends="determine-ae-build, prepare-copy-old, prepare-copy-new, prepare-copy-default">
<sleep seconds="10"/> --To read the results
</target>
<target name="prepare-copy-old" description="copy file based on condition" if="copy.old">
<echo>Old File Copied </echo>
</target>
<target name="prepare-copy-new" description="copy file based on condition" if="copy.new">
<echo>New File Copied</echo>
</target>
<target name="prepare-copy-default" description="copy file based on false condition" if="copy.default">
<echo>Default File Coping</echo>
</target>
<target name="determine-ae-build">
<condition property="copy.old">
<equals arg1="${ApplicationName}" arg2="old"/>
</condition>
<condition property="copy.new">
<equals arg1="${ApplicationName}" arg2="new"/>
</condition>
<condition property="copy.default">
<not>
<or>
<equals arg1="${ApplicationName}" arg2="new"/>
<equals arg1="${ApplicationName}" arg2="old"/>
</or>
</not>
</condition>
</target>
Explanation: Calling way "ant -Dcopy.old = true prepare-copy". Here we are passing to copy old file hence, "Old File Copied" will copied. If you call it like "ant prepare-copy" it'll call "Default File Coping".
Kindly Accept my answer if it is answered your question.Thankyou!

How to check if Ant is executed under certain path?

How can I use Ant to verify that the current working directory is located (arbitrarily deeply nested) under a certain path? For example, I want to execute a target only if the current directory is part of /some/dir/, for example if Ant is executed in directory /some/dir/to/my/project/.
The best I could come up with is a String contains condition:
<if>
<contains string="${basedir}" substring="/some/dir/"/>
<then>
<echo>Execute!</echo>
</then>
<else>
<echo>Skip.</echo>
</else>
</if>
This works for my current purpose but I'm afraid it might break some time in the future... for example when a build is executed in path /not/some/dir/ which is also contains the specified directory string.
Are there any more robust solutions like a startsWith comparison or even better a file system based check...?
There is no specific startswith condition in native Ant, but there is a matches condition that takes regular expressions.
As a side note, ant-contrib is rarely necessary for most build scripts, and will often lead to unreliable code. I would strongly recommend avoiding it.
Here's a sample script to illustrate how you can use the matches condition with native Ant. The test target is, of course, just for demonstration.
<property name="pattern" value="^/some/dir" />
<target name="init">
<condition property="basedir.starts.with">
<matches pattern="${pattern}" string="${basedir}" />
</condition>
</target>
<target name="execute" depends="init" if="basedir.starts.with">
<echo message="Executing" />
</target>
<target name="test">
<condition property="dir1.starts.with">
<matches pattern="${pattern}" string="/some/dir/" />
</condition>
<condition property="dir2.starts.with">
<matches pattern="${pattern}" string="/some/dir/to/my/project/" />
</condition>
<condition property="dir3.starts.with">
<matches pattern="${pattern}" string="/not/some/dir/" />
</condition>
<echo message="${dir1.starts.with}" />
<echo message="${dir2.starts.with}" />
<echo message="${dir3.starts.with}" />
</target>

Use pure Ant to implement if else condition (check command line input)

I am a newbie in Ant. My ant script receives a user input variable named "env" from command line:
e.g. ant doIt -Denv=test
The user input value could be "test","dev" or "prod".
I also have the "doIt" target:
<target name="doIt">
//What to do here?
</target>
In my target, I would like to create the following if else condition for my ant script:
if(env == "test")
echo "test"
else if(env == "prod")
echo "prod"
else if(env == "dev")
echo "dev"
else
echo "You have to input env"
That's to check which value user has inputted from command line, then, print a message accordingly.
I know with ant-Contrib, I can write ant script with <if> <else> . But for my project, I would like to use pure Ant to implement if else condition. Probably, I should use <condition> ?? But I am not sure how to use <condition> for my logic. Could some one help me please?
You can create few targets and use if/unless tags.
<project name="if.test" default="doIt">
<target name="doIt" depends="-doIt.init, -test, -prod, -dev, -else"></target>
<target name="-doIt.init">
<condition property="do.test">
<equals arg1="${env}" arg2="test" />
</condition>
<condition property="do.prod">
<equals arg1="${env}" arg2="prod" />
</condition>
<condition property="do.dev">
<equals arg1="${env}" arg2="dev" />
</condition>
<condition property="do.else">
<not>
<or>
<equals arg1="${env}" arg2="test" />
<equals arg1="${env}" arg2="prod" />
<equals arg1="${env}" arg2="dev" />
</or>
</not>
</condition>
</target>
<target name="-test" if="do.test">
<echo>this target will be called only when property $${do.test} is set</echo>
</target>
<target name="-prod" if="do.prod">
<echo>this target will be called only when property $${do.prod} is set</echo>
</target>
<target name="-dev" if="do.dev">
<echo>this target will be called only when property $${do.dev} is set</echo>
</target>
<target name="-else" if="do.else">
<echo>this target will be called only when property $${env} does not equal test/prod/dev</echo>
</target>
</project>
Targets with - prefix are private so user won't be able to run them from command line.
If any of you require a plain if/else condition (without elseif); then use the below:
Here I am dependent on a env variable DMAPM_BUILD_VER, but it may happen that this variable is not set in the env. So I need to have mechanism to default to a local value.
<!-- Read build.version value from env variable DMAPM_BUILD_VER. If it is not set, take default.build.version. -->
<property name="default.build.version" value="0.1.0.0" />
<condition property="build.version" value="${env.DMAPM_BUILD_VER}" else="${default.build.version}">
<isset property="env.DMAPM_BUILD_VER"/>
</condition>

Ant optional classpath element

I have some modules and a main runnable project. I have common build file, and
build.common.xml
<target name="build" >
<path id="libraries.classpath">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
<classpath refid="modules.classpath" />
</javac>
</target>
..and every module declares its own dependencies in their build.xml:
<path id="modules.classpath">
<pathelement path="../ModuleA/${build.dir}" />
...
</path>
The problem is if there is no internal dependency I get the following exception:
"Reference modules.classpath not found."
What is the solution for that? How could I declare an optional classpath element?
Note: If anybody want to suggest to create jars out of my modules, please justify this. I'm going to have 5-10 rapidly changing modules, and I don't want to do unnecesary steps in the build process.
Update: I extracted the build into two different targets and created a condition for them, but did not help (it echoes the 'false' and builds with module-dependencies):
<target name="build">
<condition property="modules.classpath.set" else="false">
<isset property="modules.classpath"/>
</condition>
<echo message="modules.classpath is set: ${modules.classpath.set} " />
<antcall target="build-with-modules" />
<antcall target="build-without-modules" />
</target>
<target name="build-with-modules" if="modules.classpath.set">
<echo message="Building with module-dependencies" />
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
<classpath refid="modules.classpath" />
</javac>
</target>
<target name="build-without-modules" unless="modules.classpath.set">
<echo message="Building with no dependent modules" />
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
</javac>
</target>
Condition isreference:
Test whether a given reference has been defined in this project and - optionally - is of an expected type.
So, try
<condition property="modules.classpath.set" else="false">
<isreference refid="modules.classpath"/>
</condition>
Also on that page, there is a link to a page that describes custom conditions. If none of the provided conditions meets your requirement, then just write one.
Update:
The logic of if and unless in <target> is to check if the property has been set -- for if, the target runs when the property has been set; for unless, the target runs when the property has NOT been set -- not the value of the property.
I have never checked the code of the condition isreference, but I think maybe the else="false" should be removed.
If removing that part still doesn't help, then you may need to use some embedded groovy or beanshell script, or write your own condition.

How to use logical operators in ant?

i have a small peice of code which prints if platform is unix or windowsas
<if>
<equals=${arg1} value="linux-86"/>
<then>
<echo message="linux"
<then>
<elseif>
<equals=${arg1} value="linux-64"/>
<then>
<echo message="linux"/>
</then>
</elseif>
<else>
<echo message="Windows">
</else>
</if>
Here we can see we are unnecessarily checking first two conditions for same message,is there any OR operator in ant like we have in c ||,so that we can write arg1=linux-64||linux-86....if there is please tell me how should i use this will save up lot of time here
<condition property="WinPlatform.Check">
<or>
<equals arg1="${param1}" arg2="win-x86"/>
<equals arg1="${param1}" arg2="win-x86-client"/>
<equals arg1="${param1}" arg2="win-x64"/>
</or>
</condition>
<target name="Mytarget" if="WinPlatform.Check">
<echo message="executing windows family build:::${param1}"/>
</target>
now call Mytarget with your parameter,it will work
The if task is part of ant-contrib. It is not available in core Ant.
Various conditions are available in core Ant and can be used, for example, to make target execution conditional.
Before 1.8, if/unless conditions evaluate "is the property set?"
<target name="test" if="foo" unless="bar"/>
From 1.8, if/unless conditions can evaluate "is the property true/false":
<target name="test" if="${foo}" unless="${bar}"/>
Why don't you have a look in the ant manual? http://ant.apache.org/manual/Tasks/conditions.html
Or google for "ant if task".

Resources