how to Include the buildnumber in maven pdf:pdf - maven

I am trying to include the build number in the pdf's that get generated with the maven pdf plugin. I have all the documentation of the project I am working on written as a maven site. This way all the documentation is stored with the source code.
Pom.xml
So in the pom.xml I have defined the buildnumber plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
<items>
<item>timestamp</item>
<item>${user.name}</item>
</items>
</configuration>
</plugin>
pdf.xml
And in the pdf.xml
<cover>
<coverTitle>${project.name}</coverTitle>
<coverSubTitle>v. ${project.version}</coverSubTitle>
<coverType>Technical documentation</coverType>
<coverVersion>build: ${project.buildNumber}</coverVersion>
<projectName>${project.name}</projectName>
<projectLogo>images/telfortlogo.jpg</projectLogo>
</cover>
I even put resource filtering to ${basedir}/site but it has no effect. I keep getting the ${buildNumber} instead of the result of the buildnumber plugin.

To get something similar working my buildnumber plugin configuration looked like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>pre-site</phase>
<goals>
<goal>create</goal>
</goals>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
<items>
<item>timestamp</item>
<item>${user.name}</item>
</items>
</configuration>
</execution>
</executions>
</plugin>
And in the pdf.xml:
<cover>
<coverTitle>${project.name}</coverTitle>
<coverSubTitle>v. ${project.version} build ${buildNumber}</coverSubTitle>
<coverType>User Guide</coverType>
<projectName>${project.name}</projectName>
</cover>
I believe that your issue comes from the fact that you are executing the buildnumber create goal during the generate-resources phase. And if you are using mvn pdf:pdf or mvn site, the generate-resources will not get executed.
I my setup I have configured the pdf plugin to run on the site phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pdf-plugin</artifactId>
<executions>
<execution>
<id>pdf</id>
<phase>site</phase>
<goals>
<goal>pdf</goal>
</goals>
I can then get the pdf to be generated at the end of the site phase.

Related

How to resolve the path to an artifact in a maven pom without being a project dependency?

I would like to know if it is possible to resolve the path for a maven artifact without having this artifact as a dependency in the project?
What I am trying to do is to execute an external java code generator via exec-maven-plugin or maven-antrun-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar ${de.powerstat.fb:generator:1.0-SNAPSHOT}</argument>
<argument>de.powerstat.fb.generator.CodeGenerator</argument>
<argument>${fb.hostname}</argument>
<argument>${fb.port}</argument>
<argument>${fb.username}</argument>
<argument>${fb.password}</argument>
<argument>${project.build.directory}</argument>
</arguments>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
So the point here is how to solve the jars path for ${de.powerstat.fb:generator:1.0-SNAPSHOT} without having it as a dependency of the whole project? Also using the exec:java goal is not a solution, because this one seems to have different problems, but thats another question.
Using the antrun plugin I come to the same problem:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<target>
<java fork="true" failonerror="true" module="de.powerstat.fb.generator" modulepath="${de.powerstat.fb:generator:1.0-SNAPSHOT}">
<arg value="${fb.hostname} ${fb.port} ${fb.username} ${fb.password} ${project.build.directory}"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
So question is again how to solve the modulepath="${de.powerstat.fb:generator:1.0-SNAPSHOT}"?
Solutions that use the maven dependency plugin with the goal dependency:properties will not work in this scenario.
I suggest to use dependency:copy to copy the JAR to some place in /target. Then you can add that path to the antrun or exec plugin.

Maven Config Plugins running twice

I have the following pom config. I added the cobertura plugin, and now pmd, cpd, findbugs, and test are running twice.
I understand that is because of my "phases" config, but I don't understand how can I achieve the following:
What I want is before I commit to the repo, build my app and check for pmd errors, findbugs errors, check my tests, and check my cobertura.
How can I achieve this? I am used to run "mvn clean package" before commit. It's that ok?
Here's my config:
...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<linkXref>false</linkXref>
<rulesets>
<!-- Custom Ruleset -->
<ruleset>codequality/pmd.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>pmd</goal>
<goal>cpd</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<check>
<haltOnFailure>true</haltOnFailure>
<branchRate>70</branchRate>
<lineRate>70</lineRate>
<totalBranchRate>70</totalBranchRate>
<totalLineRate>70</totalLineRate>
<packageLineRate>70</packageLineRate>
<packageBranchRate>70</packageBranchRate>
</check>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any maven plugin has a default execution phase. In this case, the plugins that you apply are executed in the verify phase (pmd-plugin). But you are defining it to run also in the compile phase. Removes the phase tag and lets it run in the verification phase.
<execution>
<!--<phase>compile</phase>-->
<goals>
<goal>...</goal>
...
</goals>
</execution>
Finally, the good practice to validate your project before a commit is to run:
mvn clean verify

Not able to get it test coverage in sonarqube

[enter image description here][1][enter image description here][2]I know many people got such similar problem. i show many answars, tried the sample code given on sonar site. that sample is working fine. Also i show following link
How to configure multi-module Maven + Sonar + JaCoCo to give merged coverage report?
But nothing seems working in my case. Its not showing IT Test Coverage in sonarqube though mvn clean intsall is making jacoco folder in site where index.html is having coverage report.
i tried almost every thing on net but not able to resolve issue.
its a multi module project
I am using
java8.
sonarqube 4.5.6 with java plugin 2.5.1.
maven 3.0.5
please help me resolve this.
Below is parent module pom file
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<append>true</append>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.basedir}/target/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/../target/jacoco.exec</dataFile>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
<propertyName>failsafe.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.basedir}/../target/jacoco-it.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<argLine>${failsafe.argLine}</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- automatic label creation -->
</plugins>
</build>
<!-- Plugin to generate unit test coverage in SonarQube 4.5.x report. -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<use>false</use>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven-surefire-report-plugin.version}</version>
<configuration>
<alwaysGenerateFailsafeReport>true</alwaysGenerateFailsafeReport>
<alwaysGenerateSurefireReport>true</alwaysGenerateSurefireReport>
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*.mar</exclude>
<exclude>${jacoco.excludePattern}</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</reporting>
Properties in same pom file
<sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath> <!-- This is the default, put here to be explicit -->
<sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
<sonar.language>java</sonar.language>
<sonar.java.binaries>${project.basedir}/../target/classes</sonar.java.binaries>
<jacoco-maven-plugin.version>0.7.4.201502262128</jacoco-maven-plugin.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-surefire-report-plugin.version>2.19.1</maven-surefire-report-plugin.version>
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>2.16</maven-failsafe-plugin.version>
mvn clean install is creating jacoco folder with index.html for test coverage but mvn sonar:sonar is not showing it in sonarqube
what mistake i am making.
in mvn sonar:sonar, it builds successfully and one of the line is JaCoCoSensor: JaCoCo report not found.
what could be the reason
i seriously feel this a bug with jacoco or sonarqube. May be it would not be compatible with java 8 or something. I tried almost every thing. Many things are deprecated with sonar java plugin 2.5.1. Please help me, i need the solution desperately
Just tried it, and the sample project works perfectly. Please make sure you do the following:
git clone https://github.com/SonarSource/sonar-examples.git
cd projects/languages/java/code-coverage/combined ut-it/combined-ut-it-multimodule-maven-jacoco
mvn clean package
mvn sonar:sonar
Also - not sure if this is related, but you're using a very old version of the SQ Java plugin (2.5.1). I can only advise you to update it to latest version.
Well, i am not sure why the above code is not working but i tried same thing bit differently and miracle happened. Let me give the solution what it worked for me.
I removed the below code from parent pom and put it in all child pom. Also since i was using it in child module i used target/jacoco.exec instead of ${project.basedir}/../target/jacoco.exec and same for jacoco-it.exec.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
<append>true</append>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/jacoco-it.exec</destFile>
<propertyName>failsafe.argLine</propertyName>
<append>true</append>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco-it.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
Additional information. following plugin which i mentioned in my question is not required if Test coverage is required only in sonar report. If there is requirement for code report separately then its required
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*.mar</exclude>
<exclude>${jacoco.excludePattern}</exclude>
</excludes>
</configuration>
</plugin>

maven wsdl2java configuration not working properly

I am looking to execute wsdl2java via maven and have tried several different methods with no full success. The first way I was doing it:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.apache.axis.wsdl.WSDL2Java</mainClass>
<arguments>
<argument>-client</argument>
<argument>-o</argument>
<argument>gensrc</argument>
<argument>wsdl/JobAPIWebWrapped.wsdl</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
This version will create the exact structure that I am looking for due to the call to org.apache.axis.wsdl.WSDL2Java, but will not continue with any other maven plugins beyond that. It ends the log with executing main or something to that effect.
The other method I have tried:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>gensrc</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>wsdl/JobAPIWebWrapped.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
The problem with this execution is that it generates a lot more java files than the previous execution stated higher up. I have checked the compatibility of this larger fileset and found it will work fine, but would like to find a way to force it to execute with the same java class as the first example. This version will, however, complete and allow me to move on to the following plugins called by maven.
Third:
<plugin>
<groupId>org.apache.axis</groupId>
<artifactId>wsdl2java-maven-plugin</artifactId>
<version>1.4.1-SNAPSHOT</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<implementationClassName>org.apache.axis.wsdl.WSDL2Java</implementationClassName>
</configuration>
</execution>
</executions>
</plugin>
This version isn't even being recognized... wondering if I am calling the plugin incorrectly because it isn't even showing up anywhere with verbose logging.
I have been searching quite a bit and have yet to find a successful answer. I am very close to just writing a shell script to run the maven set-up by calling the first example then moving on. Any help is greatly appreciated. Thanks.
Instead of using the exec-maven-plugin invoking WSDL2Java, you should use axistools-maven-plugin. Your pom would look like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<wsdlFiles>
<wsdlFiles>wsdl/JobAPIWebWrapped.wsdl</wsdlFiles>
</wsdlFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
By the way, Apache Axis is quite old and broken. You should think about moving to Apache CXF which is more recent and more robust.
I gave in and ended up using the antrun plugin for maven:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<configuration>
<tasks>
<java classname="org.apache.axis.wsdl.WSDL2Java" fork="true">
<arg value="-client"/>
<arg value="-o"/>
<arg value="gensrc"/>
<arg value="wsdl/JobAPIWebWrapped.wsdl"/>
<classpath refid="maven.compile.classpath"/>
</java>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

How do I include test classes and configuration in my war for integration testing using maven?

I currently have a maven web project that I am attempting to write integration tests for. For the structure of the project, I've defined test stubs under src/test/java, whilst the spring bean definitions for these stubs sit under src/test/resources.
What I would like to do, is that when I build my war artifact I'd like all of the test stub classes to be compiled and included in the war along with the spring bean definition files. I've tried to do it with the maven war plugin but the only things I've been able to copy are the resources. Simply put, I'd like to make use of the test class path and include all these classes in my war file.
It seems the useTestClassPath option with the maven jetty plugin would solve my problem but the current project I'm working on is currently using Tomcat 6.0. Is there another maven plugin or a way I can configure the maven war plugin to achieve my objective?
You can also do it straightforwardly. This will add both test classes and test resources to the WEB-INF/classes:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I also recommend you place it into separate profile like "integration" and also to override the package name in that profile to not be able to confuse normal war without tests packaged in and the testing war.
The full example with profile is here. You may run mvn clean package to have a war war-it-test.war without tests included, or you may run mvn clean package -Pintegration to have a war war-it-test-integration.war for the war with tests included.
I believe the following configuration for the maven war plugin would do what you want. You copy your test-classes to your WEB-INF/classes folder. You can even filter those resources.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>generate-test-war</id>
<phase>pre-integration-test</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
<configuration>
<warSourceDirectory>${basedir}/src/test/webapp</warSourceDirectory>
<warName>${project.artifactId}-test</warName>
<webappDirectory>${basedir}/target/${project.artifactId}-test</webappDirectory>
<primaryArtifact>false</primaryArtifact>
<webResources>
<resource>
<directory>${basedir}/target/test-classes</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
See http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
You can use the maven build helper plugin to add additional folders to the "normal" class path.
But I would recommend to create an new folder for your integration test (for example src/it/java), and add this folder, but not the "normal" test folder (src/test/java) -- the same for the resources folder.
Instead of using the maven antrun plugin, you could instead use the maven resources plugin and configure it like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<id>test-classes</id>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>false</overwrite>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/test-classes</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Use Tomcat 7 plugin with additional classpath directories configuration.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<additionalClasspathDirs>
<additionalClasspathDir>${build.testOutputDirectory}</additionalClasspathDir>
</additionalClasspathDirs>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
</executions>
</plugin>
You can do this Configuration in pom.xml file you don't get any errors in pom.xml and adding test classes to our jar or war file.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
We need to add the below plugin to the pom.xml in order to add the test cases to jar. Thanks to #IvonSopov
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-test-classes</phase>
<configuration>
<target>
<copy todir="${basedir}/target/classes">
<fileset dir="${basedir}/target/test-classes" includes="**/*" />
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
But after adding this line we got the build succeeded and also able to add the test case classes into the jar.. but the problem is in pom.xml it is showing as error like
Plugin execution not covered by lifecycle configuration:
org.apache.maven.plugins:maven-antrun-plugin:1.8:run
(execution:default, phase: process-test-classes)
In order to remove this error we need to include the below plugin as a separate tag within the build tag. (not inside the plugins which we added earlier.)
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Now we can create the jar which includes the test classes without any errors.

Resources