Checking two conditions before compiling using Ant build - ant

I want to check for presence of dependent files before my code is compiled. I am doing the following
<available file="XX" property="isXXAvailable"/>
<available file="YY" property="isYYAvailable"/>
For compilation I want to check whether both the properties are true. Only then go ahead with the compilation
<target name="compile" depends="init" unless="isXXAvailable" unless="isYYavailable">
Is it possible to check for both the properties during compiling

You can 'AND' two 'available' condtions together into a single one :
<condition property="files.available">
<and>
<available file="XX"/>
<available file="YY"/>
</and>
</condition>
then you can use this condition in the same way you are currently doing in your target
http://ant.apache.org/manual/Tasks/condition.html

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 conditionally specify a compiler argument in the javac task

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.

How to stop ant script execution or skip targets without fail?

I want to terminate an ant script, or skip some targets, but without the fail task. I wanted to do it like this:
`
<target name="init">
<condition property="canContinue">
<not>
<equals arg1="${isOffline}" arg2="1" />
</not>
</condition>
</target>
<target name="init-setup" depends="init" if="canContinue">
<echo message="CanContinue: ${canContinue}"/>
</target>
<target name="doIt" depends="init-setup">
</target>
`
I would like to skip all the targets which depend on init-setup, not just init-setup.
How can I do this?
Thank you!
If you're not using explicit fail tasks, you'll need to add your boolean flag to each target you want to skip.
Using your example:
<target name="doIt" depends="init-setup" if="canContinue">
</target>

How to put two conditions in ANT Target?

My need is to check two conditions in my ANT Target. If either of them is true the ant target must execute.
<target name="generateArtifacts" if="${generateABC}" or if="${generatePQR}">
...
/>
The above syntax in WRONG. How to make it correct?
Maybe there's something simpler, but the following would do, I think:
<condition property="artifactsMustBeGenerated">
<or>
<isTrue value="${generateABC}"/>
<isTrue value="${generatePQR}"/>
</or>
</condition>
<target name="generateArtifacts" if="${artifactsMustBeGenerated}">
...
/>

How to correctly setup a conditional task with Maven-Ant?

New Ant user here. I've created a conditional task, which is run inside as a Maven Ant plugin. The issue I'm facing is the condition target: "ui-test-condition" is not being found during a build.
The error returned is:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.6:run (uitests) on project myProject: An Ant BuildException has occured: Target "ui-test-condition" does not exist in the project "maven-antrun-". It is used from target "ui-test-run". -> [Help 1]
This would suggest a syntax error in the code below, however I'm unable to identify the issue. Any assistance is greatly appreciated.
<target name="ui-test" depends="ui-test-run,ui-test-skip"/>
<target name="ui-test-condition">
<condition property="ui-test-condition-run">
<and>
<istrue value="${ui.test}"/>
</and>
</condition>
</target>
<target name="ui-test-run" depends="ui-test-condition" if="ui-test-condition-run">
<echo>Running tests</echo>
<exec dir="src/main/webapp/ui" executable="src/main/webapp/ui/${some.executable}"
resolveexecutable="true" failonerror="true">
<arg value="-e" />
<arg value="foo/run" />
</exec>
</target>
<target name="ui-test-skip" depends="ui-test-condition" unless="ui-test-condition-run">
<echo>Tests are skipped</echo>
</target>
I had the exact same problem and I found the answer is that the depends property of a target is not supported by maven-antrun-plugin.
Excerpt of http://maven.apache.org/plugins/maven-antrun-plugin/usage.html
Ultimately, you could specify some Ant <target/> attributes in the <target/> tag. Only depends attribute in Ant <target/> is not wrapped.
That does not prevent the feature from working, at least not from experience; by just removing the depends property and ordering the targets properly, it works.
Moreover, only the last target is considered by maven-antrun-plugin. You therefore need to find a way to evaluate your condition directly in that target.

Resources