How to get coverage on code which is in a different module than tests? - maven

I have a project with three modules api, impl and report. Report module is just for coverage report aggregation. Impl module contains tests for both api and impl modules. I managed to get coverage working correctly, when I have tests in their respective modules, but this is not possible in my project, since the tests are not module specific. Is it possible to get combined code coverage even in this case with proper configuration or is it just impossible?
|- pom.xml (root)
|
|-- api-module
|--- src
|---- main
|
|-- impl-module
|--- src
|---- main
|---- test
|--- pom.xml
|
|-- report-module
|---pom.xml
pom.xml (root)
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>initialize-coverage-before-unit-test-execution</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<exclClassLoaders>*QuarkusClassLoader</exclClassLoaders>
<destFile>${project.build.directory}/jacoco-quarkus.exec</destFile>
<append>true</append>
</configuration>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/model/*DTO.*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
pom.xml (api-module)
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
pom.xml (impl-module)
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
pom.xml (report-module)
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>module-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>module-impl</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
<execution>
<id>merge-results-data</id>
<phase>verify</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>jacoco-quarkus.exec</include>
</includes>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Related

Exclude log4j.properties in the generated jar

I am using maven-compiler-plugin to build a JAR from one of my module, and I am not able to exclude log4j.properties from the generated JAR.
I tried:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
<excludes>
<exclude>**/log4j.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
But this does not work. I can exclude the file globally:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>log4j.properties</exclude>
</excludes>
</resource>
</resources>
But I don't want to do this as this impact the Eclipse build, and makes it impossible to run code from Eclipse that uses log4j.
I tried to see if the included log4j.properties was mine and not from another module, and it is mine. I've read numerous posts on the issue, including some here, but I still cannot solve it...
If this matters, this module is a submodule of a parent package, with the following <build>:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
</configuration>
<executions>
<execution>
<id>create-javadoc</id>
<phase>javadoc</phase>
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>

How to combine multiple build plugins into a single import?

For different projects, I am often using the same build plugins such as:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Is it possible to combine these into a single maven plugin so that all I would need is something like:
<build>
<plugins>
<plugin>
<groupId>my.awesome.group</groupId>
<artifactId>my-awesome-plugin</artifactId>
<version>1.0.0.RELEASE</version>
</plugin>
</plugins>
</build>
Thanks for your help!

combination of shade, proguard and appassembler maven plugins

I'm trying to build and obfuscate a multi module project using maven. I use the shade plugin to create a fat jar containing all of my own class files(every module) so that I could obfuscate the fat jar using proguard-maven-plugin and then create executable build output using appassembler plugin. everything works except that the other module dependencies also appear in the appassembler repo dir, which is wrong because the obfuscated classes already exist in the shaded jar.
I've tried defining the other module dependencies as provided and then adding the dependencies for the shade plugin, but the shade plugin seems to ignore them.
this is the relevant part of pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.build.finalName}-shaded.${project.packaging}</outputFile>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-a</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-b</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.13</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<injar>${project.build.finalName}-shaded.${project.packaging}</injar>
<outjar>${project.build.finalName}.${project.packaging}</outjar>
<proguardInclude>proguard.pro</proguardInclude>
<maxMemory>1024m</maxMemory>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
</libs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<programs>
<program>
<mainClass>my.package.Application</mainClass>
</program>
</programs>
<useWildcardClassPath>true</useWildcardClassPath>
<repositoryLayout>flat</repositoryLayout>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any ideas are welcome.
I found a solution which is not as convenient as I'd like but its better than removing the other module jars manually. I used assembly plugin to exclude the jars from the build distribution zip.
pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.build.finalName}-shaded.${project.packaging}</outputFile>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.13</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<injar>${project.build.finalName}-shaded.${project.packaging}</injar>
<outjar>${project.build.finalName}.${project.packaging}</outjar>
<proguardInclude>proguard.pro</proguardInclude>
<maxMemory>1024m</maxMemory>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
</libs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<programs>
<program>
<mainClass>my.package.Application</mainClass>
</program>
</programs>
<useWildcardClassPath>true</useWildcardClassPath>
<repositoryLayout>flat</repositoryLayout>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>descriptor.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
descriptor.xml:
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/appassembler</directory>
<excludes>
<exclude>**/module-a-${project.version}.${project.packaging}</exclude>
<exclude>**/module-b-${project.version}.${project.packaging}</exclude>
</excludes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
I think your issue comes from the fact that the shaded jar and the appassembler are ran during the same phase, package.
I think you should try to modify the phase of the appassembler plugin to:
<phase>post-package</phase>

"no main manifest attribute" while running Jar after building with maven

I have a problem getting a maven build to work properly. It builds just fine, except the Main-Class within the manifest file found in the JAR is not present.
Currently, I have one parrent POM, and one POM for each of two modules. The reactor summary shows a success on all three buils.
However, when I try to run the jar in one of the modules, I am told "no main manifest attribute" exists. Checking the content of the manifest-file within the jar shows the Main-Class is not set:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: user
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_66
This is the build content of the parrent POM:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<executable>${java.compiler}</executable>
<verbose>${java.compiler.verbose}</verbose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<quiet>${javadoc.quiet}</quiet>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<module>ConsolidatedCustomerMasterEndpoint</module>
<module>ConsolidatedCustomerMasterService</module>
</modules>
And this is the build content of the module POM:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>no.xyz.ccm.config.CcmApp</mainClass>
<classifier>jetty</classifier>
<requiresUnpack>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>CcmService</artifactId>
</dependency>
</requiresUnpack>
<addResources>false</addResources>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven-build-helper-plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/generated/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<configuration>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Problems running Maven Failsafe Plugin

I'm having trouble running failsafe plugin using mvn verify
Basically it doesn't run the integration tests! If I run mvn failsafe:integration-test works ok
Also, do I need jetty plugin or similar for running integration tests.
The failsafe tasks are not bound to verify task
Code has been checked in to..
https://github.com/tonymurphy/builder
<?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>junit-stuff</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.19.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<executions>
<execution>
<id>default-test</id>
<phase>none</phase>
</execution>
<execution>
<id>unit-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
<excludes>
<exclude>**/*$*</exclude>
<exclude>**/*ContractTest.java</exclude>
<exclude>**/*MvcTest.java</exclude>
<exclude>**/*_Test.java</exclude>
</excludes>
<excludedGroups>com.example.OneAtATime</excludedGroups>
</configuration>
</execution>
<execution>
<id>mvc-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*MvcTest.java</include>
</includes>
<excludedGroups>com.example.OneAtATime</excludedGroups>
<excludes>
<exclude>**/*$*</exclude>
<exclude>**/*ContractTest.java</exclude>
<exclude>**/*_Test.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>integration-test</id>
<configuration>
<groups>com.example.OneAtATime</groups>
</configuration>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.2.v20140723</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopPort>8005</stopPort>
<stopKey>STOP</stopKey>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<!-- stop any previous instance to free up the port -->
<goal>stop</goal>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.19.1</version>
<reportSets>
<reportSet>
<id>integration-tests</id>
<reports>
<report>failsafe-report-only</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
You need to create a separate execution for the verify goal:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
...
<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>

Resources