JProfiler supports profiling from Maven with the JProfiler Maven plugin. In addition, JProfiler offers a number of command line executables for working with snapshots that have corresponding Maven goals.
Using the Maven plugin
The JProfiler Maven plugin is available from the Maven Central repository with the coordinates
com.jprofiler:jprofiler-maven. To use it in a Maven build, add it to the plugins
in your pom.xml file:
<build>
<plugins>
<plugin>
<groupId>com.jprofiler</groupId>
<artifactId>jprofiler-maven</artifactId>
<version>X.Y.Z</version>
...
</plugin>
</plugins>
</build>
By default, the plugin auto-provisions a JProfiler distribution with the same version as the plugin.
Auto-provisioned distributions are saved to the platform-specific cache directory:
%LOCALAPPDATA%\jprofilerDist on Windows, ~/Library/Caches/jprofilerDist on macOS
and $XDG_CACHE_HOME/jprofilerDist or ~/.cache/jprofilerDist on Linux. To override
the cache directory, set the autoProvisioningCacheDir parameter of each goal.
If you want to use a local JProfiler installation instead, you can tell the plugin where JProfiler is
installed with the jprofiler.home project property or with the installDir
parameter of each goal. On macOS, the installation directory is the path of the application bundle,
for example /Applications/JProfiler.app. The plugin is also distributed in the file
bin/maven.jar of the JProfiler installation.
All goals support the skip parameter that can be set with the
jprofiler.skip property to skip execution.
Profiling from Maven
The profile goal starts a new Java process with the profiling agent. The main class
is specified with the required mainClass parameter. With the
classpathScope parameter you can add the runtime or the test class path of the project
to the profiled process, additional class path elements can be added with the
additionalClasspath parameter. Arguments for the main class and the JVM can be specified
with the arguments and jvmArguments parameters.
Without any further configuration, the goal starts an interactive profiling session where the profiling
agent waits on default port 8849 for a connection from the JProfiler GUI. For offline profiling, you can
either reference an exported config file or configure common session settings and automated recording directly
with the same parameters as for the JavaProfile
Gradle task. For parameters that accept several
values, use , as the separator.
<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>
To invoke the goal directly from the command line without an execution definition, run
mvn com.jprofiler:jprofiler-maven:X.Y.Z:profile -Djprofiler.mainClass=com.mycorp.MyMainClass
Profiling tests and other Maven plugin executions
To add the profiling agent to a Java process that is started by another Maven plugin, such as the
maven-surefire-plugin or the exec-maven-plugin, the set-agentpath-property goal sets a
project property to the VM parameter of the profiling agent. By default, the property
jprofiler.agentpath is set, a different property name can be chosen with the
propertyName parameter. The profiling parameters are the same as for the
profile goal.
<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>
Exporting data from snapshots
With the export goal you can export views from snapshots, just like with the
Export Gradle task. The snapshot file is
specified with the snapshotFile parameter, views are specified with nested
view elements and their options with nested option elements.
<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>
Comparing snapshots
The compare goal corresponds to the Compare Gradle task
and performs comparisons between two or more snapshots. The snapshot files are specified with the
snapshotFiles parameter, comparisons are nested below the comparisons
element just like views for the export goal.
<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>
Analyzing heap snapshots
Like the Analyze Gradle task, the analyze goal
prepares the heap snapshot analysis in snapshots that have been saved with offline profiling for faster access
in the GUI. The snapshots that should be processed are specified with the snapshotFiles
parameter.
<configuration>
<snapshotFiles>
<snapshotFile>snapshots/test_heap.jps</snapshotFile>
</snapshotFiles>
</configuration>