JProfiler 支持通过 JProfiler Maven 插件从 Maven 进行分析 (profiling)。此外,JProfiler 还提供了许多用于处理快照的命令行可执行文件,它们都有对应的 Maven 目标 (goals)。
使用 Maven 插件
JProfiler Maven 插件可通过坐标 com.jprofiler:jprofiler-maven 在 Maven Central 仓库中获取。要在 Maven
构建中使用它,请将其添加到 pom.xml 文件的插件列表中:
<build>
<plugins>
<plugin>
<groupId>com.jprofiler</groupId>
<artifactId>jprofiler-maven</artifactId>
<version>X.Y.Z</version>
...
</plugin>
</plugins>
</build>
默认情况下,该插件会自动预配 (auto-provision) 与插件版本相同的 JProfiler 发行版。自动预配的发行版会保存到特定于平台的缓存目录中:Windows 上为
%LOCALAPPDATA%\jprofilerDist,macOS 上为 ~/Library/Caches/jprofilerDist,Linux 上为
$XDG_CACHE_HOME/jprofilerDist 或 ~/.cache/jprofilerDist。要覆盖缓存目录,请设置每个目标 (goal) 的
autoProvisioningCacheDir 参数。
如果您想改用本地 JProfiler 安装,可以通过 jprofiler.home 项目属性或每个目标 (goal) 的 installDir 参数告知插件
JProfiler 的安装位置。在 macOS 上,安装目录为应用程序包的路径,例如 /Applications/JProfiler.app。该插件还分发在 JProfiler 安装目录下的
bin/maven.jar 文件中。
所有目标 (goals) 均支持 skip 参数,该参数可以通过 jprofiler.skip 属性进行设置以跳过执行。
从 Maven 进行分析
profile 目标会启动一个带有分析代理 (profiling agent) 的新 Java 进程。主类通过必需的 mainClass 参数指定。通过
classpathScope 参数,您可以将项目的运行时或测试类路径 (classpath) 添加到被分析 (profiled) 进程中,还可以通过
additionalClasspath 参数添加其他类路径元素。主类和 JVM 的参数可以通过 arguments 和
jvmArguments 参数指定。
在没有任何进一步配置的情况下,该目标会启动一个交互式分析会话 (session),其中分析代理将在默认端口 8849 上等待来自 JProfiler GUI 的连接。对于离线分析 (offline
profiling),您可以引用导出的配置文件,也可以直接使用与 JavaProfile Gradle 任务相同的参数来配置通用会话设置和自动记录。对于接受多个值的参数,使用
, 作为分隔符。
<plugin>
<groupId>com.jprofiler</groupId>
<artifactId>jprofiler-maven</artifactId>
<version>X.Y.Z</version>
<executions>
<execution>
<id>profile</id>
<phase>package</phase>
<goals>
<goal>profile</goal>
</goals>
<configuration>
<mainClass>com.mycorp.MyMainClass</mainClass>
<classpathScope>runtime</classpathScope>
<offline>true</offline>
<callTreeMode>sampling</callTreeMode>
<profile>com.mycorp.,org.example.</profile>
<snapshotFile>snapshots/run.jps</snapshotFile>
<recording>cpu,allocation,jdbc</recording>
<duration>10m</duration>
</configuration>
</execution>
</executions>
</plugin>
要在没有执行定义的情况下直接从命令行调用该目标,请运行
mvn com.jprofiler:jprofiler-maven:X.Y.Z:profile -Djprofiler.mainClass=com.mycorp.MyMainClass
分析测试和其他 Maven 插件执行
要将分析代理添加到由其他 Maven 插件(例如 maven-surefire-plugin 或 exec-maven-plugin)启动的 Java
进程中,set-agentpath-property 目标会将项目属性设置为分析代理的 VM 参数。默认情况下会设置属性
jprofiler.agentpath,可以通过 propertyName 参数选择不同的属性名称。分析参数与
profile 目标的参数相同。
<plugin>
<groupId>com.jprofiler</groupId>
<artifactId>jprofiler-maven</artifactId>
<version>X.Y.Z</version>
<executions>
<execution>
<id>set-agentpath-property</id>
<phase>initialize</phase>
<goals>
<goal>set-agentpath-property</goal>
</goals>
<configuration>
<offline>true</offline>
<configFile>jprofiler_config.xml</configFile>
<sessionId>479</sessionId>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${jprofiler.agentpath}</argLine>
</configuration>
</plugin>
从快照中导出数据
使用 export 目标,您可以从快照中导出视图 (view),就像使用 Export Gradle 任务一样。快照文件通过
snapshotFile 参数指定,视图通过嵌套的 view 元素指定,它们的选项通过嵌套的 option 元素指定。
<configuration>
<snapshotFile>snapshots/test.jps</snapshotFile>
<views>
<view>
<name>CallTree</name>
<file>calltree.html</file>
</view>
<view>
<name>HotSpots</name>
<file>hotspots.html</file>
<options>
<option>
<name>expandbacktraces</name>
<value>true</value>
</option>
<option>
<name>aggregation</name>
<value>class</value>
</option>
</options>
</view>
</views>
</configuration>
比较快照
compare 目标对应于 Compare Gradle 任务,并在两个或多个快照之间执行比较。快照文件通过
snapshotFiles 参数指定,比较配置嵌套在 comparisons 元素下,就像 export 目标的视图一样。
<configuration>
<sortByTime>true</sortByTime>
<snapshotFiles>
<snapshotFile>snapshots/test1.jps</snapshotFile>
<snapshotFile>snapshots/test2.jps</snapshotFile>
</snapshotFiles>
<comparisons>
<comparison>
<name>TelemetryHeap</name>
<file>heap.html</file>
</comparison>
<comparison>
<name>TelemetryThreads</name>
<file>threads.html</file>
<options>
<option>
<name>measurements</name>
<value>inactive,active</value>
</option>
<option>
<name>valuetype</name>
<value>bookmark</value>
</option>
<option>
<name>bookmarkname</name>
<value>test</value>
</option>
</options>
</comparison>
</comparisons>
</configuration>
分析堆快照
与 Analyze Gradle 任务类似,analyze 目标会对通过离线分析保存的快照预先准备堆快照分析,以便在 GUI 中更快地访问。应处理的快照通过
snapshotFiles 参数指定。
<configuration>
<snapshotFiles>
<snapshotFile>snapshots/test_heap.jps</snapshotFile>
</snapshotFiles>
</configuration>