立刻要國慶了,最近比較忙,可是感受忙的效率很通常,以前寫過ant的集成,這兩天研究了下maven,其中核心的插件即是jmeter-maven-plugin,要想了解更多的朋友,能夠自行去官網wiki學習,地址:https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/wiki。下面是我實踐的下記錄,供參考。html
jdk、jmeter、maven這些軟件的安裝以及環境變量的設置我這裏不作介紹,只要在命令窗口分別輸入jmeter -v 、java -version、mvn -v。能分別出來相應的版本說明環境沒問題。java
1. 首先是打開eclipse,新建一個maven項目。mysql
2. 依次在工程的src/test目錄下新建jmeter文件夾和resources文件夾,而後將本身寫的jmeter腳本放在jmeter文件夾下,生成報告的模版文件/meter.results.shanhe.me.xsl放在resources文件夾下git
3. 若是properties文件有過更改,則把相關的propertie文件也複製到jmeter文件夾裏,見上圖,否自系統會使用默認的jmeter文件github
1.最好maven能連上公司的私服,這樣下載jar會快點,下面是我maven的conf文件配置。正則表達式
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- <localRepository>D:/COMPANY/repo</localRepository> --> <!-- 此處配置本地maven的倉庫地址 --> <localRepository>F:\mywork\apache-maven-3.0.4-m2\repository</localRepository> <offline>false</offline> <pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> </pluginGroups> <profiles> <!-- profile | Specifies a set of introductions to the build process, to be activated using one or more of the | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/> | or the command line, profiles have to have an ID that is unique. | | An encouraged best practice for profile identification is to use a consistent naming convention | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc. | This will make it more intuitive to understand what the set of introduced profiles is attempting | to accomplish, particularly when you only have a list of profile id's for debug. | | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo. <profile> <id>jdk-1.4</id> <activation> <jdk>1.4</jdk> </activation> <repositories> <repository> <id>jdk14</id> <name>Repository for JDK 1.4 builds</name> <url>http://www.myhost.com/maven/jdk14</url> <layout>default</layout> <snapshotPolicy>always</snapshotPolicy> </repository> </repositories> </profile> --> <profile> <id>myProfile</id> <repositories> <repository> <id>myRepository</id> <name>Repository for me</name> <url>http://192.168.100.10/nexus/content/groups/public/</url> </repository> </repositories> </profile> <profile> <id>sonar</id> <properties> <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> <sonar.jdbc.username>sonar</sonar.jdbc.username> <sonar.jdbc.password>sonar</sonar.jdbc.password> <sonar.host.url>http://localhost:9000</sonar.host.url> <!-- Sonar服務器訪問地址 --> </properties> </profile> <profile> <id>jdk-1.7</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.7</jdk> </activation> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion> </properties> </profile> </profiles> <activeProfiles> <activeProfile>myProfile</activeProfile> <activeProfile>sonar</activeProfile> </activeProfiles> <updatePolicy>always</updatePolicy> </settings>
2. 下面是關鍵的部分,即maven的pom文件配置。sql
<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>mavenjmeter</groupId> <artifactId>maven2jmeter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>maven2jmeter</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--此文件路徑是jenkins的路徑,若是不用jenkins,則不能這樣寫,須要寫項目的build路徑,如: --> <!-- <jmeter.result.jtl.dir>${project.build.directory}\jmeter\results</jmeter.result.jtl.dir> <jmeter.result.html.dir>${project.build.directory}\jmeter\html</jmeter.result.html.dir>--> <jmeter.result.jtl.dir>${env.WORKSPACE}/Report/${env.BUILD_ID}/jtl</jmeter.result.jtl.dir> <jmeter.result.html.dir>${env.WORKSPACE}/Report/${env.BUILD_ID}/html</jmeter.result.html.dir> </properties> <dependencies> </dependencies> <build> <plugins> <plugin> <!-- 核心插件,用來執行jmx腳本,注意版本號,2.1.0可使用用jmeter3.1生成的腳本。最新的2.2.0使用jmeter3.2生成的腳本 --> <groupId>com.lazerycode.jmeter</groupId> <artifactId>jmeter-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <!-- 增長jar包,須要先將jar註冊到本地maven倉庫,打開cmd使用以下命令--> <!--mvn install:install-file -Dfile=D:\GIT\ZyzxAPIAutoTest\TXPTAPIAutoTest\plugns\jmeter-plugins-json.jar -DgroupId=com.jmeter.chajian -DartifactId=jmeter-plugins-json -Dversion=2.6 -Dpackaging=jar --> <jmeterExtensions> <!-- jmeter擴展插件 json path assert --> <artifact>com.jmeter.chajian:jmeter-plugins-json:2.6</artifact> <!-- 本地本身寫的jar--> <artifact>com.smrz:smrz-utils:1.0</artifact> </jmeterExtensions> <!-- 設置jmeter生成結果文件格式--> <resultsFileFormat>xml</resultsFileFormat> <!-- 設置忽略失敗是否中止運行--> <ignoreResultFailures>true</ignoreResultFailures> <!--設置結果是否有時間戳--> <testResultsTimestamp>false</testResultsTimestamp> <testFilesIncluded> <!-- //指定運行的jmeter腳本 --> <jMeterTestFile>testmaven1.jmx</jMeterTestFile> <jMeterTestFile>testmaven2.jmx</jMeterTestFile> <!-- //使用正則表達式 <jMeterTestFile>test*.jmx</jMeterTestFile> --> </testFilesIncluded> <!-- 指定jmx運行目錄 <testFilesDirectory>D:\workspaceN\maven2jmeter\src\test\jmeter\case1</testFilesDirectory> --> <!-- 指定jtl生成目錄 --> <resultsDirectory>${jmeter.result.jtl.dir}</resultsDirectory> </configuration> <executions> <execution> <id>jmeter-tests</id> <phase>verify</phase> <!--腳本所在的文件夾 --> <goals> <goal>jmeter</goal> </goals> </execution> </executions> </plugin> <plugin> <!--根據xsl模版把jtl文件轉換成html,官網地址: http://www.mojohaus.org/xml-maven-plugin/examples/transform-saxon.html--> <groupId>org.codehaus.mojo</groupId> <artifactId>xml-maven-plugin</artifactId> <version>1.0-beta-3</version> <executions> <execution> <phase>verify</phase> <goals> <goal>transform</goal> </goals> </execution> </executions> <configuration> <transformationSets> <!-- 能夠根據不一樣的模版,同事生成多個報告 <transformationSet> <dir>${jmeter.result.jtl.dir}</dir> <stylesheet>src\test\resources\jmeter.results.shanhe.me.xsl</stylesheet> <outputDir>${jmeter.result.html.dir}</outputDir> <fileMappers> <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper"> <targetExtension>html</targetExtension> </fileMapper> </fileMappers> </transformationSet> --> <transformationSet> <dir>${jmeter.result.jtl.dir}</dir> <stylesheet>src\test\resources\jmeter.results.shanhe.me.xsl</stylesheet> <outputDir>${jmeter.result.html.dir}</outputDir> <!-- 把jtl格式轉傳承html --> <fileMappers> <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper"> <targetExtension>html</targetExtension> </fileMapper> </fileMappers> </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>
這裏須要對pom文件作幾點說明。apache
1. jmeter-maven-plugin插件的版本號必定要和本地jmx文件生成的jmeter版本號對應,否則到時候會碰到com.thoughtworks.xstream.converters.ConversionException或者空指針異常。json
2. testFilesIncluded提供了靈活的腳本執行選擇方式,能夠指定具體文件或目錄,也可使用正則表達式服務器
3. 若是有多個jmx文件,會生成多個jtl日誌文件,同時生成的html報告也會有多個,這點不如ant,ant會把多個jmx的文件彙總到一個html文件裏面。
4. 若是有多個xsl報告模版文件,則能夠配置多個transformationSet,maven會根據配置的transformationSet同時生成多個html報告,這點能夠根據本身的需求選擇
5.增長jmeter官方第三方的擴展jar包,須要先將jar下載到本地,而後把該jar包註冊到本地maven倉庫,而後再調用便可,具體步驟是
mvn install:install-file -Dfile=D:\GIT\ZyzxAPIAutoTest\TXPTAPIAutoTest\plugns\jmeter-plugins-json.jar -DgroupId=com.jmeter.chajian -DartifactId=jmeter-plugins-json -Dversion=2.6 -Dpackaging=jar
<jmeterExtensions> <!-- jmeter擴展插件 json path assert --> <artifact>com.jmeter.chajian:jmeter-plugins-json:2.6</artifact> <!-- 本地本身寫的jar--> <artifact>com.smrz:smrz-utils:1.0</artifact> </jmeterExtensions>
1. 打開命令窗口,進入到項目所在的路徑,輸入 mvn verify,或者在eclipse右鍵項目,run as->maven build…,輸入verify命令。便可看到執行過程,最終build success後
2. 執行結果的全部文件,都會存放在target目錄下,如:
1. jenkins新建一個maven風格的項目,作以下配置,具體配置再也不細說,和我以前的ant集成的文章相似,具體能夠參考
1. 須要適當修改Root POM的pom文件位置,./表明jenkins的workspace的當前項目路徑,由於個人maven2jmeter項目在git上還有上級目錄,因此須要再加上maven2jmeter,若是從git拉取的直接是maven2jmeter項目,則不須要再加maven2jmeter,直接./pom.xml
2. html report路徑是:Report/$BUILD_ID/html,$BUILD_ID表明動態根據每次的構建id,生成相應目錄的文件夾,和pom文件的 <jmeter.result.html.dir>${env.WORKSPACE}/Report/${env.BUILD_ID}/html</jmeter.result.html.dir>對應
1. 文中所用的的例子demo已上傳至github:https://github.com/qiaoyeye/ApiautoTest.git
2. xsl轉換報告插件官網:http://www.mojohaus.org/xml-maven-plugin/examples/transform-basic.html
3. jmeter-maven-plugin官網:https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/wiki
4. 參考博客:http://blog.csdn.net/xujiamin123456/article/details/77451660