How to find out the current platform using Ant - ant

I'm creating an installer using Ant and I'm facing a problem. I need to create a file with different extension depending on the platform (Unix or Windows) but I don't know how to get the current platform from Ant.
Can anyone help me? Thank you in advance,
Joshua.

Is this something you're attempting to do in a build.xml? The <condition> task supports the condition os which can help you determine the platform for your install script:
<condition property="is.windows">
<os family="windows"/>
</condition>
Then you can split tasks into Unix and Windows:
<target name="deploy"
depends="deploy.unix, deploy.windows">
<yadda/>
<yadda/>
<yadda/>
</target>
<target name="deploy.unix"
unless="is.windows">
<yadda/>
<yadda/>
<yadda/>
</target>
<target name="deploy.windows"
if="is.windows">
<yadda/>
<yadda/>
<yadda/>
</target>
The deploy target will call both deploy.unix and deploy.windows. However, the deploy.unix target will be skipped if it's a Windows system and deploy.windows will be skipped if it's not a Windows system.
And, if you're using the <exec> task, you can also specify if you're running Windows or Unix:
<exec executable="${unix.program}"
osfamily="unix"/>
<exec executable="${windows.program}"
osfamily="windows"/>

Related

How to specify lib directory for ant task?

I'm currently running an exec task in my build like this:
<target name="bar">
<exec executable="ant">
<arg value="-f"/>
<arg value="/path/to/my/build.xml"/>
<arg value="-lib"/>
<arg value="/path/to/my/libs"/>
</exec>
</target>
I don't really like it and want to replace the exec task with an ant task:
<target name="bar">
<ant antfile="/path/to/my/build.xml"/>
</target>
However, I don't know how to specify the lib directory in this case. Is this possible somehow?
What are you trying to achieve, by launching ANT from within ANT in this manner?
For example if you need custom ANT extensions, the path to these jars can be specified at runtime within the code as follows:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${lib.dir}/ant-contrib-0.3.jar"/>
</classpath>
</taskdef>
Better again, you could integrate a dependency management system like Apache ivy to manage 3rd party jar dependencies.
You can call ant script inside an ant script like below.
If you use the attribute inheritrefs="true" any Ids that are set in the parent build script will be passed to child build script as well.
Eg:
<ant antfile="subbuild.xml" inheritrefs="true"/>

Build SOA composties with Ant

I already have a build automation setup in Ant for SOA projects. It recompiles all the code even if there is no change in the code. Can anyone please suggest me the way out where I can only compile the changed SOA composites?
Use Uptodate ant.apache.org/manual/Tasks/uptodate.html
<target name="anythingChanged">
<uptodate property="xmlBuild.notRequired" targetfile="${deploy}\xmlClasses.jar">
<srcfiles dir= "${src}/xml" includes="**/*.dtd"/>
</uptodate>
</target>
<target name="xmlBuild" depends="anythingChanged" unless="xmlBuild.notRequired">
...
</target>

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.

Is it possible to launch Gradle tasks from Ant?

I am researching replacements for Ant. I've looked at Gant and Gradle.
Is it possible to kick off a Gradle task from Ant? This is possible in Gant with a taskdef.
<taskdef
name = "gant"
classname = "org.codehaus.gant.ant.Gant"
classpathref = "classpath"
/>
<gant />
Is there something similar Gradle? I'm eager to start migrating from Ant to Gradle, but we have a large Ant infrastructure and any Gradle build scripts I create need to be callable from Ant.
Thanks!
Create a macrodef for gradle, call it just like any other task. Here is the setup and an example...
<!-- Gradle path stuff -->
<property environment="env" />
<condition property="gradle.executable" value="${env.GRADLE_HOME}/bin/gradle.bat" else="${env.GRADLE_HOME}/bin/gradle">
<os family="windows" />
</condition>
<!-- Macro def, gives us an ant 'gradle' task-->
<macrodef name="gradle">
<attribute name="task" />
<sequential>
<exec executable="${gradle.executable}" dir="." failonerror="true">
<arg value="#{task}" />
</exec>
</sequential>
</macrodef>
Example of using the macro def
<!-- Example, call grade with new macro -->
<target name="example">
<gradle task="build" />
</target>
Instead of switching build technology, why not use a combination of ivy and groovy to extend the capabilities of your existing ant builds?
An example is the following posting:
Parse HTML using with an Ant Script
BTW I'm a big fan of Gradle, however, like you I have to live with and support a large ANT legacy :-)
Actually I want to do the same thing and where implemented by calling a sh file and then the sh was calling the gradle but it was too much around the bush and finally the following code made it to work cool..
Hope this will help you..
<property environment="env" />
<property name="gradle.wrapper.executable" location="${env.GRADLE_HOME}/bin/gradle" />
<target name="dependencies-report" description="Creates a text file report of the depdency tree">
<exec executable="${gradle.wrapper.executable}" dir=".">
<arg value="dependencyReport" />
</exec>
</target>
Gradle doesn't offer an Ant task to run a Gradle build from Ant. What you could do is to invoke a Gradle command (like gradle build) from Ant.
In terms of Ant integration, Gradle offers two features: Importing Ant builds, and reusing Ant tasks.
Gradle is very different from Gant. Gradle is an entire new build system; Gant is a thin layer above Ant.

how to detect tomcat version installed and set CATALINA_HOME env variable using Ant Apache script?

I have a script which detects OS using Catalina.bat for windows and Catalina.sh for UNIX..it executes successfully for UNIX but for windows its not able to extract OS version from Catalina.bat..the reason i find out is because in Catalina.bat when it executes this line
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome
then OS version statement is not reached in catalina.bat file,so the solution to this is i guess; explicitly set CATALINA_HOME environment variable using my Ant script itself; how to do that plz suggest any solution.
i was using this code, here OS.version property should have cached the OS version from catalina.bat file similar code in UNIX is working fine but win i wonder whats wrong
<property name="version" location="${My_proj}\tomcat\bin\catalina.bat"/>
<exec executable="${version}" outputproperty="OS.version">
<arg value="version" />
<redirector>
<outputfilterchain>
<tokenfilter>
<containsstring contains="OS Name:"/>
<replacestring from="OS Name: " to=""/>
</tokenfilter>
</outputfilterchain>
</redirector>
</exec>
PROBLEM O SOLVED: you were right ..
<exec executable="cmd" outputproperty="tomcat.version">
<arg value="/c"/>
<arg value="${MY_PROJ}\tomcat\bin\version.bat"/>
<env key="CATALINA_HOME" value="${MY_PROJ}\tomcat\"/>
<redirector>
<outputfilterchain>
<tokenfilter>
<containsstring contains="Server version"/>
<replaceregex pattern="Server version: Apache Tomcat/(.*)$" replace="\1"/>
</tokenfilter>
</outputfilterchain>
</redirector>
</exec>
<echo message="tomcat.version: ${tomcat.version}"/>
OUTPUT:
versioncat:
[echo] tomcat.version: 6.0.33
LAST BUL NOT THE LEAST CAN ANY1 ANSWER OR SUGGEST A WORKAROUND FOR MY LAST COMMENT QUERY THE SILLY QUESTION
If I understand correctly, you are executing this OS detection from Ant. In that case, can you not instead use Ant's built-in support for OS identification - in the os condition?
However, if you really need to execute catalina.bat while setting CATALINA_HOME, you could do so using a nested env element in you exec task.
Here is a sample build file which uses both approaches:
<project default="test">
<target name="test">
<!-- Execute a command, in this case a simple bat file
which echoes the value of the var set in the env block
-->
<exec executable="cmd">
<arg value="/c"/>
<arg value="test.bat"/>
<env key="CATALINA_HOME" value="whatever"/>
</exec>
<!-- echo the values of built-in OS related properties -->
<echo message="os.arch: ${os.arch}"/>
<echo message="os.name: ${os.name}"/>
<echo message="os.version: ${os.version}"/>
<!-- test one of the os conditions -->
<condition property="is.windows">
<os family="windows"/>
</condition>
<echo message="is.windows ? ${is.windows}"/>
</target>
</project>
Here is the content of test.bat:
echo CATALINA_HOME=%CATALINA_HOME%
Here is the output:
test:
[exec]
[exec] C:\tmp\ant>echo CATALINA_HOME=whatever
[exec] CATALINA_HOME=whatever
[echo] os.arch: x86
[echo] os.name: Windows XP
[echo] os.version: 6.1 build 7601 Service Pack 1
[echo] is.windows ? true
Regarding your subsequent question (in comments) about tomcat version...
I now guess you are executing this version detection via Ant in your runtime environment.
Ant and Java don't know about your Tomcat environment, so now you're back to executing %CATALINA_HOME%\bin\catalina.bat -version and parsing what you need from the output.
Here's a working example:
<project default="version">
<property environment="env"/>
<condition property="script.ext" value="bat">
<os family="windows"/>
</condition>
<condition property="script.ext" value="sh">
<os family="unix"/>
</condition>
<target name="version">
<exec executable="${env.CATALINA_HOME}/bin/version.${script.ext}" outputproperty="tomcat.version">
<redirector>
<outputfilterchain>
<tokenfilter>
<containsstring contains="Server version"/>
<replaceregex pattern="Server version: Apache Tomcat/(.*)$" replace="\1"/>
</tokenfilter>
</outputfilterchain>
</redirector>
</exec>
<echo message="tomcat.version: ${tomcat.version}"/>
</target>
</project>
And here is the output:
version:
[echo] tomcat.version: 5.5.33
Note that this example assumes that you have the CATALINA_HOME (and JAVA_HOME) environment variable set in your terminal.
Alternatively, you could pass these variables using a nested <env> element as previously discussed. But it seems more likely that these should come from the runtime environment rather than embedded in your build file.
Do it like this :
<condition property="catalina.path" value="C:\Foo\catalina.bat">
<os family="windows"/>
</condition>
<condition property="catalina.path" value="/home/foo/catalina.sh">
<os family="unix"/>
</condition>
<exec> ... execute your script here </exec>
Depending on your situation, you may find this approach a little more platform agnostic and less error prone as you do not need to fork off a shell. This works at least as far back as Tomcat 6.x
<property environment="env"/>
<loadproperties>
<zipentry zipfile="${env.CATALINA_HOME}/bin/bootstrap.jar" name="META-INF/MANIFEST.MF"/>
<filterchain>
<prefixlines prefix="tomcat."/>
</filterchain>
</loadproperties>
<!-- Prints MAJOR.MINOR version, e.g.: 8.0 -->
<echo message="Tomcat Version: ${tomcat.Specification-Version}"/>
<!-- Prints full version, e.g.: 8.0.26 -->
<echo message="Tomcat Release: ${tomcat.Implementation-Version}"/>

Resources