How to generate a SINGLE jar contains /src/main/java and /src/test/java - maven

I have created a maven project and trying to generate a SINGLE jar file should contain both /src/main/java & /src/test/java.
But it is generating two SEPARATE jar files. Please let me know, how can I achieve this in maven?
generated jar files:
test-0.0.1-SNAPSHOT.jar
test-0.0.1-SNAPSHOT-tests.jar
my pom.xml:
<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>com.testmaven</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

You cannot access test classes from application code. So no maven plugin will help you to add src/main/java and src/test/java in a single executable jar.
If at all you want to access the main classes from the test project, check the answer similar to your question on stackoverflow here:
How can I include test classes into Maven jar and execute them?

I have tried with below approach and it worked.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/test/java/</source>
</sources>
</configuration>
</execution>
</executions>

Related

Apache Dashboard HTML reports with Jmeter Maven plugin

I want mvn clean verify to run following command for me
jmeter -n -t <file.jmx> -l <reports.csv> -e -o /htmlReports
I checked below similar question How to create HTML reports using Jmeter Maven Plugin 2.1.0. but it didn't work for me.
I want all data to be inside target folder, plus .jtl so that at next run all previous gets cleaned up.
pom.xml
<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>com.demo.performancetesting</groupId>
<artifactId>Demo-performance-testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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.8.0</version>
<configuration>
<generateReports>true</generateReports>
</configuration>
<executions>
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>de.codecentric</groupId>
<artifactId>jmeter-graph-maven-plugin</artifactId>
<version>0.1.0</version>
<configuration>
<inputFile>${project.build.directory}/jmeter/results/*.jtl</inputFile>
<graphs>
<graph>
<pluginType>ResponseTimesOverTime</pluginType>
<width>800</width>
<height>600</height>
<outputFile>${project.build.directory}/jmeter/results/BlazeDemoRequest.png</outputFile>
</graph>
</graphs>
</configuration>
</plugin>
</plugins>
</build>
</project>
You can do this using Maven Exec plugin, the relevant configuration would be
<configuration>
<basedir>/path/to/bin/folder/of/your/jmeter/installation</basedir>
<executable>jmeter</executable>
<commandlineArgs>-n -t -f file.jmx -l ${basedir}/target/reports.csv -e -o ${basedir}/target/htmlReports</commandlineArgs>
</configuration>
Just in case something "will not work" again here is full pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>jmeter</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<basedir>/path/to/bin/folder/of/your/jmeter/installation</basedir>
<executable>jmeter</executable>
<commandlineArgs>-n -t -f file.jmx -l ${basedir}/target/reports.csv -e -o
${basedir}/target/htmlReports
</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Don't forget to change /path/to/bin/folder/of/your/jmeter/installation to real path to your JMeter otherwise it "will not work".
Report generation via JMeter Maven Plugin should also work fine, if you experience any issues please show your pom.xml file and maven command output.
Example configuration you can use for reference:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>jmeter</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.8.0</version>
<executions>
<!-- Run JMeter tests -->
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
<configuration>
<generateReports>true</generateReports>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
N.B.
you will need to copy your file.jmx script into src/test/jmeter folder, otherwise it "will not work"
if your test is using any JMeter Plugins - it "will not work" without defining them as dependencies
if your test uses external data files - it "will not work" unless you copy them to src/test/jmeter folder
More information: Five Ways To Launch a JMeter Test without Using the JMeter GUI
Report generation with JMeter Maven Plugin works for your requirement, you don't need any additional plugin.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.demo.performancetesting</groupId>
<artifactId>Demo-performance-testing</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.8.0</version>
<executions>
<!-- Run JMeter tests -->
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Requirements:
copy your jmx file into src/test/jmeter folder
if you use 3rd party plugins like JMeter Plugins, ensure you follow this documentation
if you use CSV data files - copy them to src/test/jmeter folder
Step 1 - Create test plan and save JMX script
Step 2 - Executing JMeter tests
<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>
<ignoreResultFailures>true</ignoreResultFailures>
<testResultsTimestamp>false</testResultsTimestamp>
</configuration>
</plugin>
Step 3 - Save test result into csv/xls file
Step 4 - The following plugin is used to transform the result
<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/main/jmeter-results-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>

Error in NonGUIDriver java.lang.IllegalArgumentException

I am trying to run a jmeter script using 'mvn verify' and getting below error. I am new to Jmeter and tried out solutions from previous post but in vain. How to resolve this?
[INFO] Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML <>, missing class com.thoughtworks.xstream.converters.ConversionException:
<?xml version="1.0" encoding="UTF-8"?>
<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>dap-Jmeter</groupId>
<artifactId>Jmeter</artifactId>
<version>1</version>
<packaging>jar</packaging>
<name>jmeter-maven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>1.App</start-class>
<java.version>1.8</java.version>
<msgpack.version>0.7.0-p3</msgpack.version>
<lombok.version>1.14.8</lombok.version>
<rest.assured.version>2.3.3</rest.assured.version>
</properties>
<dependencies>
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-standard</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-extras-libs</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.7.0</version>
<executions>
<!-- Run JMeter tests -->
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
<!-- Fail build on errors in test -->
</executions>
<configuration>
<jmeterExtensions>
<artifact>kg.apc:jmeter-plugins:pom:1.3.1</artifact>
</jmeterExtensions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Check target/jmeter/logs folder, it should have full log file for your test(s), my expectation is that your test relies on a plugin or a third-party .jar file which is missing in the JMeter Classpath, if you need all this stuff like RestAssured and Lombok in your test you need to add them a little bit differently to wit
<configuration>
<testPlanLibraries>
<artifact>org.msgpack:msgpack-core:0.7.0-p3</artifact>
<articact>org.projectlombok:lombok:1.14.8</articact>
<artifact>com.jayway.restassured:rest-assured:2.3.3</artifact>
</testPlanLibraries>
<jmeterExtensions>
<artifact>kg.apc:jmeter-plugins:pom:1.3.1</artifact>
<articact>kg.apc:jmeter-plugins-standard:1.4.0</articact>
</jmeterExtensions>
<downloadExtensionDependencies>false</downloadExtensionDependencies>
</configuration>
Full pom.xml just in case:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>com.example.jmeter</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.7.0</version>
<executions>
<!-- Run JMeter tests -->
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
<!-- Fail build on errors in test -->
<execution>
<id>jmeter-check-results</id>
<goals>
<goal>results</goal>
</goals>
</execution>
</executions>
<configuration>
<testPlanLibraries>
<artifact>org.msgpack:msgpack-core:0.7.0-p3</artifact>
<articact>org.projectlombok:lombok:1.14.8</articact>
<artifact>com.jayway.restassured:rest-assured:2.3.3</artifact>
</testPlanLibraries>
<jmeterExtensions>
<artifact>kg.apc:jmeter-plugins:pom:1.3.1</artifact>
<articact>kg.apc:jmeter-plugins-standard:1.4.0</articact>
</jmeterExtensions>
<downloadExtensionDependencies>false</downloadExtensionDependencies>
</configuration>
</plugin>
</plugins>
</build>
</project>
References:
Adding jar's to the /lib directory
Adding jar's to the /lib/ext directory
JMeter Maven Plugin Wiki
Five Ways To Launch a JMeter Test without Using the JMeter GUI
The below POM solved the issue.
<dependencies>
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-extras-libs</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.7.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
<configuration>
<jmeterExtensions>
<artifact>kg.apc:jmeter-plugins-casutg:2.4</artifact>
<artifactId>kg.apc:jmeter-plugins-extras-libs:1.3.1</artifactId>
</jmeterExtensions>
<!-- The plugin uses some broken dependencies
An alternative is to set this to true and use excludedArtifacts, see below
-->
<downloadExtensionDependencies>false</downloadExtensionDependencies>
</configuration>
</plugin>
</plugins>
</build>

Creating executable JAR with Maven Shade plugin

I am having trouble making an executable jar from a simple Java program.
I followed these Maven instructions and modified my pom.xml:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>package.MyClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
When executing the install goal, I can read the line:
[INFO] Replacing /home/project-1.0.0.jar with /home/project-1.0.0-shaded.jar
But all I can find at that location is the project-1.0.0.jar, actually containing the dependencies -and having the right size- but when trying to execute it with my jre, it says:
The file '/home/project-1.0.0.jar' is not marked as executable.
Thus nothing gets executing!
How can I get my jar to execute properly?
Thanks for helping!
Given is the pom.xml to create an executable jar file
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.executablejar</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>1.8</java-version>
<docker.image.prefix>springDemo</docker.image.prefix>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>demo</finalName>
</build>
I have a simple solution with the assembly plugin. This solution will make a jar (target/${project.groupdId}-${project.artifactId}-${project.version}.jar):
containing all the required dependencies
that is executable (via java -jar *.jar)
Declare in you pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>package.MyClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
I hope I helped you!
Cheers !

Maven install plugin with depedency packaging

I have Project A and Project B. ProjectB just has only proprietary jars or third party libs that ProjectA needs which cannot be found in maven repository. I intended to package all the related ( grouping dependencies) jars of Project B into one/more jar files. Question is, how do I install these jars and shade/assemble them in to one jar at the same time using POM. I am getting proprietary jars installed separately but I cannot get them packaged them in to one jar. M2 eclipse is also not helping much , so I decide to use mvn commands inside eclipse. What is the best practice to do this?. I think shading them since I want to use these dependencies in other projects as well.Can I install and shade/assemble jars( logically group the related dependencies in to one jar) at the time using POM?.
I followed this( from stack over flow forum) , it did not work- as this is creating one project jar which doesn't have anything + proprietary jars separately but not shading all jars in to one. I want them installed and combined in to one jar at the same time.
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.projectdeps</groupId>
<artifactId>sharedlibs</artifactId>
<version>1.0-SNAPSHOT</version>
<name>shared-libs</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>install activationjar</id>
<goals><goal>install-file</goal></goals>
<phase>validate</phase>
<configuration>
<file>${basedir}/src/main/libs/activation.jar</file>
<groupId>com.mycompany.activation</groupId>
<artifactId>activation</artifactId>
<version>12.8</version>
<packaging>jar</packaging>
</configuration>
</execution>
<execution>
<id>install opsjar</id>
<goals><goal>install-file</goal></goals>
<phase>validate</phase>
<configuration>
<file>${basedir}/src/main/libs/Operations.jar</file>
<groupId>com.mycompany.gcs.ops</groupId>
<artifactId>Operations</artifactId>
<version>12.8</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>Operations</artifact>
<includes>
<include>/**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
</project>

run findbugs through maven eclipse

how to run findbugs through eclipse with maven project. I have configured in maven as:
<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>com.home.app</groupId>
<artifactId>home-app</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>home-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<effort>Max</effort>
<threshold>Low</threshold>
<xmlOutput>true</xmlOutput>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
i want to know what are the errors/warnings reported by findbugs.
You will find the report during the usual compile package phase created in the target directory. If you like to see a more readable output you have to use the findbugs goal in the reporting block instead of the build block.
You could also just use the findbugs eclipse plugin which will give you a nice eclipse window that points you directly to all of your bugs.

Resources