Monday 24 December 2018

Running JMeter Script from Eclipse in Maven project

We can run JMeter Script from Eclipse in 4 simple steps:

  1. Create a simple Maven project in eclipse
  2. Import JMeter Script(.jmx) file in eclipse
  3. Add jmeter-maven-plugin to pom.xml
  4. Script Execution

Eclipse + Maven Project


  • Go to File > New > Project 


  • Select Wizard > select Maven Project


  • Select project name and location wizard > Keep selected use default work-space location > Click Next


  • Select an Archetype > simply Click Next

  • Specify Archetype parameters wizard > Please enter GroupId, Artifact Id  > Click Finish

  • Your Maven Project is successfully created, project hierarchy will look like this


Import JMeter Script in Eclipse


  • To import and execute JMeter script in eclipse, first we need to rename the src/test/java source folder to src/test/jmeter. After renaming project hierarchy will look like this



  • Place the JMeter script to src/test/jmeter source folder as shown 



Add JMeter Maven plugin to POM

  • Add below JMeter Maven plugin to pom.xml
<build>
    <plugins>
        <plugin>
            <groupId>com.lazerycode.jmeter</groupId>
            <artifactId>jmeter-maven-plugin</artifactId>
            <version>2.1.0</version>
            <executions>
                <execution>
                    <id>jmeter-tests</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jmeter</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

  •  Complete pom.xml will look like this

<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>example</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>example</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>2.1.0</version>
                <executions>
                    <execution>
                        <id>jmeter-tests</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Script Execution


  • To run JMeter script please use below commands

mvn clean install

  • You can also specify target defined in pom.xml 
mvn clean verify


1 comment:

  1. How to run it directly from JAR and not through the mvn command. As per your pom.xml, a target folder is being created but it doesn't run the Jmeter Performance Tests. Any idea?

    ReplyDelete