Different targets on Different OS with Ant - ant

I have an ant target I don't want called unless I am running ant on Linux (Not called on Windows)
<target name="jar.all" depends="clean,compile.nic,jar,jar.resources"/>
The target that I don't want called on Windows is: compile.nic
How can I do this?

Insert an if property to your compile.nic target.
<target name="compile.nic" if="windowsos">
And use this conditions before running your target
<condition property="windowsos">
<os family="windows" />
</condition>
<condition property="linuxos">
<os family="unix" />
</condition>

Here is a real-world example for Windows vs. UNIX commands. The ELSE improves on previous answers.
<condition property="maven.executable" value="mvn.bat" else="mvn">
<os family="windows" />
</condition>
<target name="clean">
<exec executable="${maven.executable}">
<arg value="clean" />
</exec>
</target>

Can I make the Ant copy task OS-specific?
Additionally some tasks support os attribute. for example exec:
<exec executable="cmd" os="windows"/>

You can use the os condition to set a property, and then skip your target by adding either the if or unless attributes (depending on how you define your property)

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!

IBM Worklight app-builder ant task creates two xcode projects instead of one

my app-builder ant task is like that:
<target name="build.app" depends="eval.dev.params, prepare.app, install.plugin.pay">
<exec executable="/usr/sbin/ipconfig" outputproperty="ip.addr" osfamily="mac">
<arg value="getifaddr"/>
<arg value="en0"/>
</exec>
<condition property="current.ip" value="${server.path}">
<not>
<equals arg1="${build.env}" arg2="dev"/>
</not>
</condition>
<condition property="current.ip" value="http://${ip.addr}:${server.port}">
<equals arg1="${build.env}" arg2="dev"/>
</condition>
<echo message="${current.ip}"/>
<app-builder applicationFolder="${build.path}/${context.root}"
nativeProjectPrefix="${context.root}" outputFolder="${build.path}"
worklightserverhost="${worklight.server.host}"/>
</target>
The problem is that after this step I get two xcodeprojects instead of one and the ios build then fails and I don't know how to inspect "app-builder" work.
The two resulting xcode projects are named this way:
/workspace/kWallet/build/myAppEnv/iphone/native/myAppMyAppIphone.xcodeproj
/workspace/kWallet/build/myAppEnv/iphone/native/myAppEnvMyAppIphone.xcodeproj
Are you sure that they both are being build by ant???
The myAppMyAppIphone.xcodeproj, by looking at name format, is the one built by the ant utility and the other myAppEnvMyAppIphone.xcodeproj was built by eclipse.
When you use eclipse to build the ios env it uses the format
<project><app><env>.xcodeproj
where as ant uses the format
<app><app><env>.xcodeproj
<app-builder applicationFolder="${build.path}/${context.root}"
nativeProjectPrefix="${context.root}" outputFolder="${build.path}"
worklightserverhost="${worklight.server.host}"/>
If you provide the nativeProjectPrefix to be the projectname then we get the xcode file name as same.

build.xml ant script condition

Im trying to add a condition in my build.xml script for my deployment process. I basically need to determine the remote.host and based on the host set a variable value.
<condition property="my-prop" value="production">
<based on host=1234>
</condition>
<condition property="my-prop" value="development">
<based on host=5678>
</condition>
any ideas? of how the is suppose to be?
This should do the trick, guessing you will have to tweak a bit based on machine addresses or environment or whatever. Sets the value of my-prop to specified value based on test against the host property.
<!-- <property name="host" value="1234"/> -->
<property name="host" value="5678"/>
<target name="test">
<condition property="my-prop" value="production">
<equals arg1="1234" arg2="${host}"/>
</condition>
<condition property="my-prop" value="development">
<equals arg1="5678" arg2="${host}"/>
</condition>
<echo>${my-prop}</echo>
</target>

How check for a condition in ant and depending on its value print a message?

This is a small piece of code please give a look at it then follow the description....
<condition property="${param1}">
<or>
<istrue value="win-x86"/>
<istrue value= "win-x86-client"/>
<istrue value= "win-x64"/>
</or>
</condition>
<target name="Mytarget" if="${param1}">
<echo message="executing windows family build:::${param1}"/>
</target>
<target name="print.name" >
<antcall target="win-x86-build">
<param name="param1" value="${platform.id}"/>
</antcall>
</target>
I want that when ever platform.id contains any of the windows family name it should print the message EXECUTING WINDOWS FAMILY BUILD but the problem is that it is printing this message even when the family is unix.
I think either I am not checking the condition properly or else i am doing some other mistake.
Can someone help me out with this please?
Since ant 1.9.1 you can do this:
<project name="tryit" xmlns:if="ant:if" xmlns:unless="ant:unless">
<exec executable="java">
<arg line="-X" if:true="${showextendedparams}"/>
<arg line="-version" unless:true="${showextendedparams}"/>
</exec>
<condition property="onmac">
<os family="mac"/>
</condition>
<echo if:set="onmac">running on MacOS</echo>
<echo unless:set="onmac">not running on MacOS</echo>
</project>
Looks like you misunderstood the Condition Task:
property: The name of the property to set.
Try using the Conditionos:
Test whether the current operating system is of a given type.
Peter is trying to explain that you must explicitly specify the property name. Try the following to make your code work:
<project name="demo" default="Mytarget">
<condition property="windoze">
<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="windoze">
<echo message="executing windows family build:::${param1}"/>
</target>
</project>
A better solution would be to make use of operating system tests built into ANT's condition task.
<project name="demo" default="Mytarget">
<condition property="windoze">
<os family="windows"/>
</condition>
<target name="Mytarget" if="windoze">
<echo message="executing windows family build:::${os.name}-${os.arch}-${os.version}"/>
</target>
</project>

Can I make the Ant copy task OS-specific?

I have an Ant script that performs a copy operation using the 'copy' task. It was written for Windows, and has a hardcoded C:\ path as the 'todir' argument. I see the 'exec' task has an OS argument, is there a similar way to branch a copy based on OS?
I would recommend putting the path in a property, then setting the property conditionally based on the current OS.
<condition property="foo.path" value="C:\Foo\Dir">
<os family="windows"/>
</condition>
<condition property="foo.path" value="/home/foo/dir">
<os family="unix"/>
</condition>
<fail unless="foo.path">No foo.path set for this OS!</fail>
As a side benefit, once it is in a property you can override it without editing the Ant script.
The previously posted suggestions of an OS specific variable will work, but many times you can simply omit the "C:" prefix and use forward slashes (Unix style) file paths and it will work on both Windows and Unix systems.
So, if you want to copy files to "C:/tmp" on Windows and "/tmp" on Unix, you could use something like:
<copy todir="/tmp" overwrite="true" >
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</copy>
If you do want/need to set a conditional path based on OS, it can be simplified as:
<condition property="root.drive" value="C:/" else="/">
<os family="windows" />
</condition>
<copy todir="${root.drive}tmp" overwrite="true" >
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</copy>
You could use the condition task to branch to different copy tasks... from the ant manual:
<condition property="isMacOsButNotMacOsX">
<and>
<os family="mac"/>
<not>
<os family="unix"/>
</not>
</and>
Declare a variable that is the root folder of your operation. Prefix your folders with that variable, including in the copy task.
Set the variable based on the OS using a conditional, or pass it as an argument to the Ant script.
You can't use a variable and assign it depending on the type? You could put it in a build.properties file. Or you could assign it using a condition.
Ant-contrib has the <osfamily /> task. This will expose the family of the os to a property (that you specify the name of). This could be of some benefit.

Resources