JProfiler 提供的 Ant 任务与 Gradle 任务非常相似。本章重点介绍了与 Gradle 任务的区别,并展示了每个 Ant 任务的示例。
所有 Ant 任务都包含在归档文件 bin/ant.jar 中。
为了使任务对 Ant 可用,您必须首先插入一个 taskdef
元素,告知 Ant 在何处找到该任务定义。以下所有示例都包含该 taskdef。每个构建文件中它只需出现一次,并且可以出现在
project 元素之下的任何层级。
无法将 ant.jar 归档文件复制到 Ant 发行版的 lib 文件夹中,您必须在任务定义中引用 JProfiler 的完整安装路径。
与 Gradle 和 Maven 集成不同,Ant 任务不会自动配置 JProfiler 发行版。
通过 Ant 进行分析
com.jprofiler.ant.ProfileTask 派生自内置的 Java 任务,并支持其所有属性和嵌套元素。附加属性与
JavaProfile Gradle 任务 相同。
Ant 属性不区分大小写,通常使用小写形式书写。对于接受多个值的属性,请使用 : 或 , 作为分隔符。
<taskdef name="profile"
classname="com.jprofiler.ant.ProfileTask"
classpath="<path to JProfiler installation>/bin/ant.jar"/>
<target name="profile">
<profile classname="MyMainClass" offline="true"
calltreemode="sampling" profile="com.mycorp.:org.example."
snapshotfile="snapshots/run.jps" recording="cpu:allocation:jdbc"
duration="10m">
<classpath>
<fileset dir="lib" includes="*.jar" />
</classpath>
</profile>
</target>
从快照导出数据
使用 com.jprofiler.ant.ExportTask,您可以从快照中导出视图 (view),就像使用
Export Gradle 任务 一样。视图的指定方式与 Gradle
任务不同:它们直接嵌套在 task 元素下方,选项则通过嵌套的 option 元素指定。
<taskdef name="export"
classname="com.jprofiler.ant.ExportTask"
classpath="<path to JProfiler installation>/bin/ant.jar"/>
<target name="export">
<export snapshotfile="snapshots/test.jps">
<view name="CallTree" file="calltree.html"/>
<view name="HotSpots" file="hotspots.html">
<option name="expandbacktraces" value="true"/>
<option name="aggregation" value="class"/>
</view>
</export>
</target>
比较快照
com.jprofiler.ant.CompareTask 对应于 Compare Gradle 任务,并在两个或多个快照之间执行比较。与
com.jprofiler.ant.ExportTask 类似,比较直接嵌套在元素下方,选项则嵌套在每个 comparison 元素中。快照文件通过嵌套的文件集
(file set) 指定。
<taskdef name="compare"
classname="com.jprofiler.ant.CompareTask"
classpath="<path to JProfiler installation>/bin/ant.jar"/>
<target name="compare">
<compare sortbytime="true">
<fileset dir="snapshots">
<include name="*.jps" />
</fileset>
<comparison name="TelemetryHeap" file="heap.html"/>
<comparison name="TelemetryThreads" file="threads.html">
<option name="measurements" value="inactive,active"/>
<option name="valuetype" value="bookmark"/>
<option name="bookmarkname" value="test"/>
</comparison>
</compare>
</target>
分析堆快照
与 Analyze Gradle 任务类似,Ant 对应的 com.jprofiler.ant.AnalyzeTask
会为通过离线分析保存的快照预先准备堆快照分析,以便在 GUI 中更快地访问。需要处理的快照通过嵌套的文件集指定。
<taskdef name="analyze"
classname="com.jprofiler.ant.AnalyzeTask"
classpath="<path to JProfiler installation>/bin/ant.jar"/>
<target name="analyze">
<analyze>
<fileset dir="snapshots" includes="*.jps" />
</analyze>
</target>