简体   繁体   中英

Conditionally Execute JMeter Maven Plugin

I'm trying to figure out how to execute my JMeter performance test plan conditionally. I want to have my Jenkins CI job execute it, but when developers run mvn clean install I don't want the below plugins to run. Any ideas on how I can modify my pom.xml to conditionally run the below plugins?

Maven POM.xml JMeter Plugins:

<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>admin@test.com</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>email@me.com</receiver>
         </receivers>
         <fileSets>
            <fileSet>
                <directory>${project.build.directory}/jmeter/results</directory>
                <includes>
                    <include>LoadTestPlan.html</include>
                </includes>
            </fileSet>
          </fileSets>
         </configuration>
       </execution>
      </executions>
     </plugin>

The best way to achieve this is with profiles . You define a profile which contains your plugin configuration. This profile would by default be turned off (so when a developer executes mvn clean install it is not activated), and you would only activate it during your Jenkins job.

So for example in your pom you would have something along these lines:

<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>

So by default this profile is not active, and the plugins wont execute. On Jenkins you would configure the build to be executed as follows:

mvn clean install -Dbuild.environment=jenkins

As the profile has an id you can also configure Jenkins to specifically use the profile by name as follows:

mvn clean install -Pci-environment

For details on possible ways to activate a profile see the following sonatype resource: http://books.sonatype.com/mvnref-book/reference/profiles-sect-activation.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM