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 wa开发者_JS百科y? 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)
精彩评论