How can I start Tomcat 8 using ANT - ant

I upgraded tomcat from 6 to version 8. Now my ant scripts stopped working.
I used to start tomcat 6 using following target:
<target name="start-server">
<java jar="${server.home}/bin/bootstrap.jar" fork="true" spawn="true">
<jvmarg line="-Dcatalina.home=${server.home} -Dcatalina.base=${server.home}"/>
</java>
</target>
This doesn't work any more.

You can try to use startup.bat or catalina.bat instead of starting it via Java.
<exec dir="${server.home}/bin" executable="cmd">
<arg value="/c"/>
<arg line="catalina.bat"/>
<arg line="start"/>
<env key="CATALINA_HOME" value="${server.home}"/>
<env key="JAVA_OPTS" value="-Xms256m -Xmx1024m"/>
</exec>

Related

Ant exec, How to arg

So I have the below build file:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\Abc\Desktop\cmd.bat" >
<arg value="exit"/>
<arg value="mkdir C:\Users\Abc\Desktop\ant\test"/>
</exec>
</target>
I thought it would create a test folder there but its not. It pops out another command prompt window, I thought the exit would close that but it doesn't. I'm not sure what I am doing wrong.
The content of bath script test1.bat:
#echo off
echo %1
And ant's build.xml:
<target name="test">
<echo message="test"></echo>
<exec executable="C:\Users\AAA\Desktop\ant\test1.cmd">
<arg value="hello"/>
</exec>
</target>
As per the official documentation:
Note that .bat files cannot in general by executed directly. One
normally needs to execute the command shell executable cmd using the
/c switch
Follow this format instead:
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
Source: https://ant.apache.org/manual/Tasks/exec.html

ant exec task when command is fail

I have some code:
<exec executable="src/main/webapp/bin/webdriver.bat" failonerror="true" resultproperty="return.code">
<arg line="${ccc}/eeee--report-format JSON --report-file testResultserer/resultere-data/wwwww.json"/>
</exec> (1)
So, now I would like to exec command , when (1) are going fail.
How can I do this.
You can set property with the value of the return code and then execute the other command conditionally on the property's value:
<project>
<exec executable="${cmd}" resultproperty="ret1"/>
<condition property="cmd1failed" value="true">
<not>
<equals arg1="0" arg2="${ret1}"/>
</not>
</condition>
<exec executable="echo" xmlns:if="ant:if" if:true="${cmd1failed}">
<arg value="${cmd} failed"/>
</exec>
<exec executable="echo" xmlns:unless="ant:unless" unless:true="${cmd1failed}">
<arg value="${cmd} didn't fail"/>
</exec>
</project>
For example
$ ant -f exec.xml -Dcmd=/bin/true
Buildfile: /tmp/exec.xml
[exec] /bin/true didn't fail
BUILD SUCCESSFUL
Total time: 0 seconds
$ ant -f exec.xml -Dcmd=/bin/false
Buildfile: /tmp/exec.xml
[exec] Result: 1
[exec] /bin/false failed
BUILD SUCCESSFUL
Total time: 0 seconds
This uses the if/unless attributes introduced with Ant 1.9.1.
If you are using an older version of Ant, you'll have to use separate targets, something like
<project default="both">
<target name="cmd1">
<exec executable="${cmd}" resultproperty="ret1"/>
<condition property="cmd1failed" value="true">
<not>
<equals arg1="0" arg2="${ret1}"/>
</not>
</condition>
</target>
<target name="cmd1-fail" depends="cmd1" if="cmd1failed">
<exec executable="echo">
<arg value="${cmd} failed"/>
</exec>
</target>
<target name="cmd1-pass" depends="cmd1" unless="cmd1failed">
<exec executable="echo">
<arg value="${cmd} didn't fail"/>
</exec>
</target>
<target name="both" depends="cmd1-fail,cmd1-pass"/>
</project>

No output running yui-compressor from Ant

I'm going to minify my js files by yuicomressor via ant, I wrote this:
<property name="concat-js-file-name" value="main.concat.js"/>
<property name="concat-js-file-path" value="${temp-folder}/js/${concat-js-file-name}"/>
<property name="yui-jar-path" value="lib/yuicompressor-2.4.7.jar"/>
<target name="minification" depends="concatation">
<echo>---Minification is started</echo>
<java jar="${yui-jar-path}" fork="true">
<arg value="${concat-js-file-path}"/>
<arg value="-o minified.js"/>
</java>
<echo>---Minification is finished successfully...</echo>
</target>
The problem is the output file is not generated!
Any idea?
You should set <java ... failonerror="true"/> and increase the noiselevel to see what's going on, means start your ant build with ant -f yourbuild.xml -debug
Actually, after some tries I found a solution:
I used <arg line="-o outputfile inputfile"/> instead, and it worked.
I suggest using <arg value="..."> instead of <arg line="...">. <arg value="..."> ensures that each command line argument has quotes around it, if necessary.
In the case of yui-compressor, the "-o" and "<file>" arguments should each go in their own <arg value="..."> elements:
<java jar="${yui-jar-path}" fork="true">
<arg value="-o"/>
<arg value="minified.js"/>
<arg value="${concat-js-file-path}"/>
</java>

Run buildfile with parameters under Linux

I created an Ant task that runs buildfile (dfs-build.xml) with parameters. It works well under OS Windows. But, I would like ANT task runs under Linux. Any ideas?
<exec executable="cmd">
<arg value="/c"/>
<arg value="${ant.basedir}\bin\ant -Dproperty.files.dir=${property.files.dir} -Dbasedir=${antscripts.basedir}/../DocumentumCoreProject/dfs6.7 -Dmodule.name=rbacs -f ${antscripts.basedir}\sub_ANTs\Create_EAR_WAR\dfs-build.xml generate"/>
</exec>
The exec task is simpler under Linux:
<exec executable="${ant.basedir}\bin\ant" osfamily="unix">
<arg value="-Dproperty.files.dir=${property.files.dir} -Dbasedir=${antscripts.basedir}/../DocumentumCoreProject/dfs6.7 -Dmodule.name=rbacs -f ${antscripts.basedir}\sub_ANTs\Create_EAR_WAR\dfs-build.xml generate"/>
</exec>
Another cross-platform option is to run the build within the same ANT process
<subant antfile="{antscripts.basedir}\sub_ANTs\Create_EAR_WAR\dfs-build.xml" target="generate">
<property name="property.files.dir" value="${property.files.dir}"/>
<property name="basedir" value="${antscripts.basedir}/../DocumentumCoreProject/dfs6.7"/>
<property name="module.name" value="rbacs"/>
</subant>

How to get a local IP address in ant 1.5.1

I was wondering, if there is a way to figure out the local IP address with ant. I cannot use the hostinfo task, since I am bound to ant 1.5.1. Now I would write little scripts for each platform and use ant conditional mechanics to execute the appropriate script for each platform. However, maybe any of you know a more elegant way? Thanks in advance.
Benjamin
This works on my mac running os x 10.8.2 :
<target name="getCurrentIP">
<exec executable="/usr/sbin/ipconfig" outputproperty="currentIP">
<arg value="getifaddr"/>
<arg value="en0"/>
</exec>
<echo>currentIP : ${currentIP}</echo>
</target>
<target name="checkos">
<condition property="isWindows" value="true">
<os family="windows" />
</condition>
<condition property="isLinux" value="true">
<os family="unix" />
</condition>
</target>
<target name="if_windows" depends="checkos" if="isWindows">
<exec executable="cmd" outputproperty="myHostName">
<arg value="/c" />
<arg value="hostname"/>
</exec>
<exec executable="cmd" outputproperty="infraServerIPTemp" >
<arg value="/c"/>
<arg value="FOR /f "tokens=1 delims=:" %d IN ('ping ${myHostName} -4 -n 1 ^| find /i "reply"') DO FOR /F "tokens=3 delims= " %g IN ("%d") DO echo infraServerIP=%g > myIP.properties"/>
</exec>
<property file="myIP.properties"/>
</target>
<target name="if_unix" depends="checkos" if="isLinux">
<exec executable="hostname" outputproperty="infraServer">
<arg line="-i"/>
</exec>
<property name="infraServerIP" value="${infraServer}"/>
</target>
<target name="checkOSType" depends="if_windows, if_unix"/>
<target name="do-something" depends="checkOSType">
</target>
with the help of Ant addon Flaka you might use :
<project xmlns:fl="antlib:it.haefelinger.flaka">
<!-- on windows -->
<exec executable="cmd" outputproperty="winip">
<arg value="/c" />
<arg value="ipconfig" />
</exec>
<!-- simple echo -->
<fl:echo>localip => #{replace('${winip}', '$2' , '\s(IP.+):\s?(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)')}</fl:echo>
<!-- set property -->
<fl:let>localip := replace('${winip}', '$2' , '\s(IP.+):\s?(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)')</fl:let>
<!-- on linux -->
<exec executable="hostname" outputproperty="linuxip">
<arg value="-i" />
</exec>
<!-- simple echo -->
<fl:echo>localip => #{replace('${linuxip}', '$1' , '(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)\s(.+)')}</fl:echo>
<!-- set property -->
<fl:let>localip := replace('${linuxip}', '$1' , '(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)\s(.+)')</fl:let>
</project>
Our solution to the problem is, that we built a little Java program, which prints the local ip to the standard output. We store this output in an ant property. (We use Java instead of a scripting language of some sort, because we would otherwise have to deploy the language runtime on many systems and Java is already deployed throughout our system landscape)

Resources