2013-12-10 150 views
2

我想弄清楚如何有條件地執行我的JMeter性能測試計劃。我想讓我的Jenkins CI工作執行它,但是當開發人員運行mvn clean install時,我不希望下面的插件運行。關於如何修改我的pom.xml以有條件地運行下面的插件的任何想法?有條件地執行JMeter Maven插件

的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>[email protected]</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 protected]</receiver> 
     </receivers> 
     <fileSets> 
      <fileSet> 
       <directory>${project.build.directory}/jmeter/results</directory> 
       <includes> 
        <include>LoadTestPlan.html</include> 
       </includes> 
      </fileSet> 
      </fileSets> 
     </configuration> 
     </execution> 
     </executions> 
    </plugin> 

回答

5

實現這一目標的最好方法是使用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> 

所以默認情況下此配置文件不活躍,和插件不會執行。在詹金斯將配置構建要執行如下:

mvn clean install -Dbuild.environment=jenkins

由於輪廓有一個id您還可以配置詹金斯按名稱具體使用的配置文件如下:

mvn clean install -Pci-environment

有關可能的激活配置文件的詳細信息,請參閱以下sonatype資源: http://books.sonatype.com/mvnref-book/reference/profiles-sect-activation.html

+1

非常詳細的答案,非常感謝! – c12

+0

很高興聽到它的幫助。 – DB5