In my last post, I included Ant code calling a separate Groovy script. Here is an example of an inline Groovy script inside the <groovy> tags.
The purpose of this target was to query a running Oracle app server using asctl and then regex out a specific container website port. asctl is called and the resulting text is put in the property 'ports.list'. One line in string value of this property is contains "http-web-site", possibly some whitespace, a pipe, possibly more whitespace, and then a multi-digit port string. The embedded groovy calls the regex matcher operator on the value of this property via the 'properties' map, performs a capture on the port string, and then invokes a closure that sets the 'properties' map value of 'http.web.site' to the captured value.
<target name="set-ports">
<exec executable="${ORACLE_HOME}/bin/asctl" dir="${T_WORK}" outputproperty="ports.list" >
<arg line="listPorts -topoNode ${INSTANCE_NAME}/${OC4J_COMP_NAME}" />
</exec>
<echo>${ports.list}</echo>
<groovy>
(properties['ports.list'] =~ /http-web-site\s*\|\s*(\d+)/).each {
all, port -> properties['http.port'] = port
}
</groovy>
<echo>http-web-site port is ${http.port}</echo>
</target>
We only expect one match, so 'each' really isn't the right method here, but it looks goods, works, and I don't know what else to do. Please comment if you have another way to do this, I'm always looking to up my groovy-fu.
Post a Comment