java - 有条件地执行 JMeter Maven 插件

标签 java maven jenkins jmeter jmeter-maven-plugin

我正在尝试弄清楚如何有条件地执行我的 JMeter 性能测试计划。我想让我的 Jenkins CI 作业执行它,但是当开发人员运行 mvn clean install 时,我不希望运行以下插件。关于如何修改我的 pom.xml 以有条件运行以下插件有什么想法吗?

Maven POM.xml JMeter 插件:

<plugin>
    <groupId>com.lazerycode.jmeter</groupId>
        <artifactId>jmeter-maven-plugin</artifactId>
        <version>1.8.1</version>
        <executions>
         <execution>
          <id>jmeter-tests</id>
          <phase>verify</phase>
          <goals>
           <goal>jmeter</goal>
          </goals>
         </execution>
        </executions>
        <configuration>
         <testFilesDirectory>${project.basedir}/src/test/jmeter</testFilesDirectory>
         <ignoreResultFailures>true</ignoreResultFailures>
         <testResultsTimestamp>false</testResultsTimestamp>
        </configuration>
       </plugin>
       <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
     <execution>
      <phase>verify</phase>
      <goals>
       <goal>transform</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <transformationSets>
      <transformationSet>
       <dir>${project.build.directory}/jmeter/results</dir>
       <stylesheet>${project.basedir}/src/test/resources/jmeter-results-detail-report_21.xsl</stylesheet>
       <outputDir>${project.build.directory}/jmeter/results</outputDir>
       <fileMappers>
        <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.RegExpFileMapper">
         <pattern>(.*?)\s(.*?)</pattern>
         <replacement>$1$2</replacement>
         <replaceAll>true</replaceAll>
        </fileMapper>
        <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
         <targetExtension>.html</targetExtension>
        </fileMapper>
       </fileMappers>
      </transformationSet>
     </transformationSets>
    </configuration>
   </plugin>
   <plugin>
      <groupId>ch.fortysix</groupId>
      <artifactId>maven-postman-plugin</artifactId>
      <version>0.1.2</version>
      <executions>
       <execution>
        <id>send a mail</id>
        <phase>install</phase>
        <goals>
         <goal>send-mail</goal>
        </goals>
        <inherited>false</inherited>
        <configuration>
         <from><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0b0e0703042a1e0f191e44090507" rel="noreferrer noopener nofollow">[email protected]</a></from>
         <subject>Load Test Results</subject>
         <failonerror>true</failonerror>
         <mailhost>relay.apple.com</mailhost>
         <htmlMessageFile>${project.build.directory}/jmeter/results/LoadTestPlan.html</htmlMessageFile>
         <receivers>
            <receiver><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9df8f0fcf4f1ddf0f8b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a></receiver>
         </receivers>
         <fileSets>
            <fileSet>
                <directory>${project.build.directory}/jmeter/results</directory>
                <includes>
                    <include>LoadTestPlan.html</include>
                </includes>
            </fileSet>
          </fileSets>
         </configuration>
       </execution>
      </executions>
     </plugin>

最佳答案

实现这一目标的最佳方法是使用 profiles 。您定义一个包含您的插件配置的配置文件。默认情况下,此配置文件将被关闭(因此,当开发人员执行 mvn clean install 时,它不会被激活),并且您只能在 Jenkins 工作期间激活它。

例如,在您的 pom 中,您将有以下内容:

<project>
    ...
    <profiles>
        <profile>
            <id>ci-environment</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>build.environment</name>
                    <value>jenkins</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.lazerycode.jmeter</groupId>
                        <artifactId>jmeter-maven-plugin</artifactId>
                        <!-- rest of your jmeter configuration goes here -->
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>xml-maven-plugin</artifactId>
                        <!-- rest of your xml-maven configuration goes here -->
                    </plugin>
                    <plugin>
                        <groupId>ch.fortysix</groupId>
                        <artifactId>maven-postman-plugin</artifactId>
                        <!-- rest of your postman configuration goes here -->
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

因此默认情况下此配置文件未激活,并且插件不会执行。在 Jenkins 上,您将配置要执行的构建,如下所示:

mvn clean install -Dbuild.environment=jenkins

由于配置文件有一个id,您还可以将 Jenkins 配置为按名称专门使用该配置文件,如下所示:

mvn clean install -Pci-environment

有关激活配置文件的可能方法的详细信息,请参阅以下 sonatype 资源: http://books.sonatype.com/mvnref-book/reference/profiles-sect-activation.html

关于java - 有条件地执行 JMeter Maven 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20488401/

相关文章:

java - Maven 不使用 maven jar 插件生成 MANIFEST 文件

java - jenkins 执行器未构建 MasterToSlaveCallable

java - 如何使用 Java 将文件从 Zip 文件读取到内存?

java - 如何在 Java 中使用正则表达式捕获多行模式?

maven - 如何使用 Maven 更改 WildFly 监听端口?

gwt - 如何在仅针对 Maven 的测试中设置 GWT headless (headless)模式?

java - 如何获取 Jenkins 服务器上 findbugs 生成的报告?

linux - 启动 Hudson 的 Init.d 脚本不会在 Ubuntu 上启动时运行

java - 在 Java 中的枚举值之间切换

java - 为什么,如果两个对象根据 equals 不相等,则不需要返回不同的 hashCode 值?