Ant - printing a collection - map - ant

I have an ant script that is executing a jmx get task:
<jmx:get
name="java.lang:type=GarbageCollector,name=Copy"
attribute="LastGcInfo"
resultproperty="CopyLastGcInfo"
/>
Now, the LastGcInfo attribute has a map Map called memoryUsageBeforeGC. In this map there is a pair with a key called "value". Althoug I am unable to elicit it and print it out. I have tried to accomplish it like that:
<echo>${CopyLastGcInfo.memoryUsageBeforeGc.value.used}</echo>
<echo>${CopyLastGcInfo.memoryUsageBeforeGc.0.used}</echo>
<echo>${CopyLastGcInfo.memoryUsageBeforeGc.used}</echo>
That didn't work. Do you have any suggestions how to tackle this problem?
Thanks.

The following example worked for me:
<project name="Catalina Ant JMX" xmlns:jmx="antlib:org.apache.catalina.ant.jmx" default="jmx">
<target name="jmx" description="Show JMX stats">
<jmx:open host="localhost" port="9012" username="controlRole" password="tomcat"/>
<jmx:get name="java.lang:type=GarbageCollector,name=Copy"
attribute="LastGcInfo"
resultproperty="CopyLastGcInfo"
echo="false"
/>
<echo>
memoryUsageBeforeGc
===================
Perm Gen.used=${CopyLastGcInfo.memoryUsageBeforeGc.Perm Gen.used}
Tenured Gen.used=${CopyLastGcInfo.memoryUsageBeforeGc.Tenured Gen.used}
Eden Space.used=${CopyLastGcInfo.memoryUsageBeforeGc.Eden Space.used}
Code Cache.used=${CopyLastGcInfo.memoryUsageBeforeGc.Code Cache.used}
Survivor Space.used=${CopyLastGcInfo.memoryUsageBeforeGc.Survivor Space.used}
</echo>
</target>
</project>
Example output
jmx:
[echo]
[echo] memoryUsageBeforeGc
[echo] ===================
[echo] Perm Gen.used=9660888
[echo] Tenured Gen.used=8393792
[echo] Eden Space.used=4456448
[echo] Code Cache.used=2038016
[echo] Survivor Space.used=172784
[echo]
Note
The "echo" attribute of the "jmx:query" task can be used to see the ANT properties and values created by the task. Useful for troubleshooting.

Related

How to search for a file in a directory and read its names and print out largest numbered file using Ant?

I need to develop an ANT script that reads a directory content. The files in this directory are numbered along with its name. I have to find the largest numbered file in this directory and return its name and the number.
For example:
>> ls
patch-aaa-050.jar
patch-aaa-051.jar
patch-aaa-052.jar
patch-aaa-053.jar
patch-aaa-054.jar
patch-aaa-055.jar
I need to echo out: Latest patch is patch-aaa-055: Patched level: 55
Although this is a very easy task in Bash, PowerShell or Python. I am a bit clueless in ANT. I have checked out dirset Type and fileset Type. These types are able to list the directories but parsing the file names are overwhelming for me. The design is to run only with ANT, so I am limited to use that tool.
Thanks
Use resource collection path combined with basename and script task, f.e.:
<project>
<path id="foo">
<last>
<sort>
<fileset dir="C:/WKS/temp" includes="*.jar"/>
</sort>
</last>
</path>
<basename file="${toString:foo}" property="foobar"/>
<script language="javascript">
var a = project.getProperty('foobar').split('.')[0];
var b = project.getProperty('foobar').split('-')[2].split('.')[0];
print ('Latest patch is ' + a + ': Patched level: ' + b);
</script>
</project>
There are also other possibilities for sort, see ant manual

ivy:listmodule does not find an existing module

We have our shared Ivy repository on an nginx web server reachable within our intranet.
I configured a url resolver to read from the shared repository and an ssh resolver to write to it, mostly following Jason Grimes' excellent blog post on managing dependencies in non-Java projects.
Now I just successfully published a module to the repository via the ssh resolver.
In my SFTP client I can see the directory structure and files sitting in the directory served by the web server:
com.organization/modulename/ivy-modulename-2.0.1.xml.md5
com.organization/modulename/ivy-modulename-2.0.1.xml.sha1
com.organization/modulename/ivy-modulename-2.0.1.xml
com.organization/modulename/modulename-2.0.1.zip.md5
com.organization/modulename/modulename-2.0.1.zip.sha1
com.organization/modulename/modulename-2.0.1.zip
However, when I execute ivy:listmodule it doesn't seem to find it. Frankly, it doesn't output anything apart from the ivysettings initialization output.
This is the Ant target I am executing:
<!-- ================================
target: check-already-in-repo
Check if the current version of a module already exists in the (shared) repository.
================================ -->
<target name="check-already-in-repo">
<ivy:listmodules resolver="shared" organisation="${ivy.organisation}" module="${ivy.module}" revision="${version}" property="already-in-repo" value="true"/>
<ac:if>
<isset property="already-in-repo"/>
<then>
<echo>${ivy.module} ${version} already exists in the repository.</echo>
<echo>Skipping publishing of ${ivy.module}.</echo>
</then>
</ac:if>
</target>
And this is the only output:
$ ant check-already-in-repo -Dversion=2.0.1 -Divy.organisation=com.organization -Divy.module=modulename
Buildfile: [...]/build.xml
check-already-in-repo:
[ivy:listmodules] :: Apache Ivy 2.4.0-rc1 - 20140315220245 :: http://ant.apache.org/ivy/ ::
[ivy:listmodules] :: loading settings :: file = [...]/build/ivysettings.xml
BUILD SUCCESSFUL
Total time: 0 seconds
I checked the value of ivy.shared.default.root and the corresponding ivy and artifact patterns and they're all matching (I kept them simple).
I tried it with the glob matcher and called ivy:listmodules with organization=*, module=* and revision=*, so it should in every case return something. Which it doesn't.
What do I miss?
Here's the rest of the relevant config:
<ivysettings>
<!-- This file is referenced from multiple projects - DO NOT EDIT! -->
<!-- shared -->
<property name="ivy.shared.default.root" value="http://10.79.1.30/ivy"/>
<property name="ivy.shared.default.ivy.pattern" value="[organisation]/[module]/ivy-[module]-[revision].[ext]"/>
<property name="ivy.shared.default.artifact.pattern" value="[organisation]/[module]/[artifact]-[revision].[ext]"/>
<!-- local -->
<property name="ivy.local.default.root" value="${ivy.default.ivy.user.dir}/local"/>
<property name="ivy.local.default.ivy.pattern" value="${ivy.shared.default.ivy.pattern}"/>
<property name="ivy.local.default.artifact.pattern" value="${ivy.shared.default.artifact.pattern}"/>
<settings defaultResolver="default"/>
<resolvers>
<filesystem name="local">
<ivy pattern="${ivy.local.default.root}/${ivy.local.default.ivy.pattern}" />
<artifact pattern="${ivy.local.default.root}/${ivy.local.default.artifact.pattern}" />
</filesystem>
<!-- read access -->
<url name="shared">
<ivy pattern="${ivy.shared.default.root}/${ivy.shared.default.ivy.pattern}" />
<artifact pattern="${ivy.shared.default.root}/${ivy.shared.default.artifact.pattern}" />
</url>
<!-- write access -->
<ssh name="ssh" host="10.79.1.30" port="22" user="ivy" userPassword="${ivy.ssh.password}" publishPermissions="0664">
<ivy pattern="${ivy.shared.default.ivy.pattern}" />
<artifact pattern="${ivy.shared.default.artifact.pattern}" />
</ssh>
<chain name="default" returnFirst="true">
<resolver ref="local"/>
<resolver ref="shared"/>
</chain>
</resolvers>
</ivysettings>
After executing the Ant target with the -d (debug) option something caught my eye:
$ ant check-already-in-repo -Dversion=2.0.1 -Divy.organisation=com.organization -Divy.module=modulename -d
[...]
[ivy:listmodules] using shared to list all in http://10.79.1.30/ivy/
[ivy:listmodules] HTTP response status: 403 url=http://10.79.1.30/ivy/
[ivy:listmodules] CLIENT ERROR: Forbidden url=http://10.79.1.30/ivy/
[ivy:listmodules] HTTP response status: 403 url=http://10.79.1.30/ivy/
[ivy:listmodules] CLIENT ERROR: Forbidden url=http://10.79.1.30/ivy/
[ivy:listmodules] problem while listing resources in http://10.79.1.30/ivy/ with shared (java.io.IOException: The HTTP response code for http://10.79.1.30/ivy/ did not indicate a success. See log for more detail.)
[ivy:listmodules] java.io.IOException: The HTTP response code for http://10.79.1.30/ivy/ did not indicate a success. See log for more detail.
[...]
It seems for listmodules to work, the web server needs to have directory listings enabled. And indeed, after adding
location /ivy {
autoindex on;
}
to the nginx config and restarting the web server it finally worked as expected!
$ ant check-already-in-repo -Dversion=2.0.1 -Divy.organisation=com.organization -Divy.module=modulename
Buildfile: [...]/build.xml
check-already-in-repo:
[ivy:listmodules] :: Apache Ivy 2.4.0-rc1 - 20140315220245 :: http://ant.apache.org/ivy/ ::
[ivy:listmodules] :: loading settings :: file = [...]/build/ivysettings.xml
[echo] modulename 2.0.1 already exists in the repository.
[echo] Skipping publishing of modulename.
BUILD SUCCESSFUL
Total time: 0 seconds
Hooray! :-)

Need to run Jtest by using an ant script...any example?

Need to run Jtest by using an ant script...any example ?
I have done this so far
<target name="test">
<jtest config="/Carts Static Analysis.properties" localsettings="/ApplicationCore.jtest/scripts/build.properties"
publish="false" showdetails="false" nobuild="true"
report="c:/reports" workspace="C:/">
<resource name="EC"/>
</jtest>
</target>
</project>
But I get an error that "Failed to create task or type jtest. Any suggestions ?
Did you check Parasoft web page :
http://build.parasoft.com/docs/overview.html ?

How do I capture the output of an antcall inside of ant?

I have an Ant script which I run in Eclipse and it outputs in the console like so:
buildStuff:
[echo] Building <project>
doStuff:
syncStuff:
[sync] Copying 1 file to <directory>
doOtherStuff:
callWebservice:
[http] HTTP Request
[http] ********************
[http] URL: <url>
[http] Method: GET
[http] HTTP Response
[http] ********************
[http] Status: 200
[echo] [callWebservice] Success
I would like to put some/all of this output into a property inside the ant script.
With the exec task I can specify an "outputproperty" attribute, but this does not work for antcall task.
So, how do I either access or redirect the console output from within ant?
Ok, found something that works...
The record task can listen to the output and send it to a file.
Apparently it doesn't allow relative paths - the file is created in the same directory as the build script (irrespective of basedir value).
The loadfile task can then be used to put this into a property, followed by delete to clean up afterwards.
It would be nicer to have the recorder output direct to a property, but this doesn't appear to be an option, for whatever reason.
In summary, this worked:
<record name="${CurProject}.status" />
<echo>Building ${CurProject}</echo>
etc...
<record name="${CurProject}.status" action="stop" />
<loadfile srcFile="build/${CurProject}.status" property="Status" />
<delete file="build/${CurProject}.status" />

Removing comment in ini file writen by ant when updating the ini file

I have an ant file which updates the data in ant file due this ini file gets updated and at the top it has a comment as follows
#Thu, 07 Jul 2011 06:54:54 -0500
I don't want this comment as i am accessing this file by php using parse_ini. Due to this comment i get an failure
Comments starting with '#' are deprecated in build.ini on line 1
so is there any way so that i will not get the comment in ini file.
Thanks.
EDIT:
<propertyfile file="build.ini">
<entry key="build-number" type="int" operation="+" value="1" />
</propertyfile>
this updates my ini file build-number by +1
Martin's comment points you to a way to remove comments using replaceregexp. (I was about to show you a similar idea but using move, filterchain and striplinecomments. But the replaceregexp is more compact.)
The other suggestion I have is that since you are editing ini files, maybe you should use a task dedicated to that, rather than using the PropertyFile task. There is an IniFile taks in ant-contrib might do the job.
If the replaceregexp doesn't work for you because your file has other # comments in it and you only want to remove that top line, then try this:
<target name="test">
<propertyfile file="test.properties">
<entry key="key" value="value"/>
</propertyfile>
<move file="test.properties" tofile="test.properties.tmp">
<filterchain>
<headfilter lines="-1" skip="1"/>
</filterchain>
</move>
<move file="test.properties.tmp" tofile="test.properties"/>
</target>
Output:
$ cat test.properties
one=1
# existing comment
$ ant
Buildfile: C:\tmp\ant\build.xml
test:
[propertyfile] Updating property file: C:\tmp\ant\test.properties
[move] Moving 1 file to C:\tmp\ant
[move] Moving 1 file to C:\tmp\ant
BUILD SUCCESSFUL
Total time: 0 seconds
$ cat test.properties
one=1
# existing comment
key=value

Resources