maven+jmeter+jenkins集成

马上要国庆了,最近比较忙,但是感觉忙的效率很一般,之前写过ant的集成,这两天研究了下maven,其中核心的插件便是jmeter-maven-plugin,要想了解更多的朋友,可以自行去官网wiki学习,地址:https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/wiki。下面是我实践的下记录,供参考。

jdk、jmeter、maven这些软件的安装以及环境变量的设置我这里不做介绍,只要在命令窗口分别输入jmeter -v    、java -version、mvn -v。能分别出来相应的版本说明环境没问题。

eclipse部分

1. 首先是打开eclipse,新建一个maven项目。

2. 依次在工程的src/test目录下新建jmeter文件夹和resources文件夹,然后将自己写的jmeter脚本放在jmeter文件夹下,生成报告的模版文件/meter.results.shanhe.me.xsl放在resources文件夹下

3. 如果properties文件有过更改,则把相关的propertie文件也复制到jmeter文件夹里,见上图,否自系统会使用默认的jmeter文件

maven部分

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:myworkapache-maven-3.0.4-m2
epository</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文件配置。

<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
esults</jmeter.result.jtl.dir>
    <jmeter.result.html.dir>${project.build.directory}jmeterhtml</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:GITyzxAPIAutoTestTXPTAPIAutoTestplugnsjmeter-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:workspaceNmaven2jmetersrc	estjmetercase1</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	est
esourcesjmeter.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	est
esourcesjmeter.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文件做几点说明。

1. jmeter-maven-plugin插件的版本号一定要和本地jmx文件生成的jmeter版本号对应,不然到时候会碰到com.thoughtworks.xstream.converters.ConversionException或者空指针异常。

2. testFilesIncluded提供了灵活的脚本执行选择方式,可以指定具体文件或目录,也可以使用正则表达式

3. 如果有多个jmx文件,会生成多个jtl日志文件,同时生成的html报告也会有多个,这点不如ant,ant会把多个jmx的文件汇总到一个html文件里面。
4. 如果有多个xsl报告模版文件,则可以配置多个transformationSet,maven会根据配置的transformationSet同时生成多个html报告,这点可以根据自己的需求选择

5.增加jmeter官方第三方的扩展jar包,需要先将jar下载到本地,然后把该jar包注册到本地maven仓库,然后再调用即可,具体步骤是

  • 下载插件到本地
  • 解压后,将lib/ext中的jar包注册到本地maven仓库:命令为
mvn install:install-file -Dfile=D:GITyzxAPIAutoTestTXPTAPIAutoTestplugnsjmeter-plugins-json.jar -DgroupId=com.jmeter.chajian -DartifactId=jmeter-plugins-json -Dversion=2.6 -Dpackaging=jar
  • 在pom文件中,执行的时候将该文件复制到target的jmeter->lib->ext文件夹下,具体配置:
<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>
  • 自己写的jar包配置方法同上

执行部分

1. 打开命令窗口,进入到项目所在的路径,输入 mvn verify,或者在eclipse右键项目,run as->maven build…,输入verify命令。即可看到执行过程,最终build success后

2. 执行结果的所有文件,都会存放在target目录下,如:

jenkins集成部分

1. jenkins新建一个maven风格的项目,做如下配置,具体配置不再细说,和我之前的ant集成的文章类似,具体可以参考

ant+jmeter+jenkins+git持续集成以及邮件报告展示

 

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

原文地址:https://www.cnblogs.com/qiaoyeye/p/7608179.html