JMeter:通过Maven在Jenkins部署
Environment prepares
- Write the JMeter scripts.
- Install Java, Jenkins, Maven environment in system.
Run JMeter scripts by Maven
- Create the Maven project and organize directory.
- Copy the JMeter scripts into the
src/test/jmeter
directory. It's the default directory set byjmeter-maven-plugin
which used to run script. - Copy the Property file into the
src/test/jmeter
directory. You can edit it to override JMeter's default properties. - Copy the resources to
src/test/resources
directory.
That's the screenshot:
- Edit the Pom.xml
It's is the whole content in Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testops.top</groupId>
<artifactId>Jmeter</artifactId>
<version>1.0</version>
<properties>
<!-- Specify a JMX script to run, need param: -D jmxName="${targetTagName}"-->
<jmxName>Specify a name of JMX script</jmxName>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Specify resources source dir-->
<jmeter.resources.source.dir>${project.basedir}/src/test/resources</jmeter.resources.source.dir>
<!-- Specify the resources output directory after build -->
<jmeter.resources.output.dir>${project.build.directory}/jmeter/html</jmeter.resources.output.dir>
<!-- Specify testFiles source dir-->
<jmeter.testFiles.source.dir>${project.basedir}/src/test/jmeter</jmeter.testFiles.source.dir>
<!-- JTL directory of jmeter test result -->
<jmeter.result.jtl.dir>${project.build.directory}/jmeter/results</jmeter.result.jtl.dir>
<!-- The stylesheet used for converting JTL to HTML-->
<jmeter.result.html.dir>${project.build.directory}/jmeter/html</jmeter.result.html.dir>
<jmeter.result.html.stylesheetFile>${jmeter.result.html.dir}/jmeter-results-detail-report_21.xsl
</jmeter.result.html.stylesheetFile>
</properties>
<build>
<plugins>
<plugin>
<!-- Get current time without timezone problem used to set Html Report datetime -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>build.time</name>
<pattern>yyyy-MM-dd HH:mm:ss</pattern>
<locale>CN</locale>
<timeZone>Asia/Shanghai</timeZone>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Copy resources to the output directory -->
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${jmeter.resources.output.dir}</outputDirectory>
<resources>
<resource>
<directory>${jmeter.resources.source.dir}</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>3.4.0</version>
<executions>
<!-- Generate JMeter configuration -->
<execution>
<id>configuration</id>
<goals>
<goal>configure</goal>
</goals>
</execution>
<!-- Run JMeter tests -->
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
<!-- Fail build on errors in test -->
<execution>
<id>jmeter-check-results</id>
<goals>
<goal>results</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Specify testFiles directory -->
<testFilesDirectory>${jmeter.testFiles.source.dir}</testFilesDirectory>
<!-- Specify testFiles-->
<testFilesIncluded>
<jMeterTestFile>${jmxName}.jmx</jMeterTestFile>
</testFilesIncluded>
<!-- The JTL results directory -->
<resultsDirectory>${jmeter.result.jtl.dir}</resultsDirectory>
<!-- The Jar Extensions In Jmeter -->
<jmeterExtensions>
<artifact>kg.apc:jmeter-plugins-functions:2.1</artifact>
<artifact>mysql:mysql-connector-java:8.0.23</artifact>
</jmeterExtensions>
<!-- The plugin uses some broken dependencies
An alternative is to set this to true and use excludedArtifacts, see below
-->
<downloadExtensionDependencies>false</downloadExtensionDependencies>
<!-- To set <generateReports> is used to make <resultsFileFormat> useful when it is "xml" -->
<generateReports>false</generateReports>
<resultsFileFormat>xml</resultsFileFormat>
<!-- If you don't want the maven execution to stop, set <ignoreResultFailures> true -->
<ignoreResultFailures>true</ignoreResultFailures>
<!-- If you do not want a timestamp added you can disable this behaviour by setting the <testResultsTimestamp> configuration setting to false. -->
<testResultsTimestamp>false</testResultsTimestamp>
</configuration>
</plugin>
<plugin>
<!-- Current plugin convert jlt in xml style to html -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>${jmeter.result.jtl.dir}</dir>
<stylesheet>${jmeter.result.html.stylesheetFile}</stylesheet>
<outputDir>${jmeter.result.html.dir}</outputDir>
<fileMappers>
<fileMapper
implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>html</targetExtension>
</fileMapper>
</fileMappers>
<parameters>
<parameter>
<name>dateReport</name>
<value>${build.time}</value>
</parameter>
</parameters>
</transformationSet>
</transformationSets>
</configuration>
<!-- using XSLT 2.0 -->
<dependencies>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>saxon</artifactId>
<version>8.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
If you can't learn why it is or how to edit, you sould learn the follow knowledges:
- Maven Plugin: xml-maven-plugin, jmeter-maven-plugin, maven-resources-plugin
- Run scripts by
mvn
mvn clean verify
- Read the Html Project