Maven multi module project with separate tests module - Code Coverage? - java

I have a maven multi module project.
root:
moduleA/ # no unit tests
moduleB/ # no unit tests
moduleC/ # no unit tests
tests/ # All unit tests, since depends on modules A, B and C
All tests are in single module called tests/ and all code is in separate modules.
Is there a way I can get code coverage?

There is a way to accomplish this. The magic is to create a combined jacoco.exec file and to do it in two steps. My pom:
<properties>
...
<jacoco.overall.exec>${maven.multiModuleProjectDirectory}/target/jacoco_analysis/jacoco.exec</jacoco.overall.exec>
</properties>
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<configuration>
<destFile>${jacoco.overall.exec}</destFile>
<dataFile>${jacoco.overall.exec}</dataFile>
</configuration>
</plugin>
...
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>runTestWithJacoco</id>
<activation>
<property>
<name>runTestWithJacoco</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>createJacocoReport</id>
<activation>
<property>
<name>createJacocoReport</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-report</id>
<phase>validate</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Add this to your parent pom and execute mvn clean install -DrunTestWithJacoco and than mvn validate -DcreateJacocoReport. Now you have the complete coverage of a class and it doesn't matter which test covered it. The magic is to use maven.multiModuleProjectDirectory to create a combined jacoco.exec file. This property is available since maven 3.3.1 and points to the folder where you started your maven build.

I don't think either of jacoco or cobertura is capable of reporting code coverage across modules. You may want to try instrumenting the compiled classes before running the test coverage report rather than relying on on-the-fly instrumentation.
See this jacoco maven goal to perform the offline instrumentation.

Since Jacoco version: 0.7.7, you can use report-aggregate.
Root pom.xml :
<project>
[...]
<build>
<plugins>
<!-- refer:https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugins>
</build>
[...]
<pluginManagement>
<plugins>
<!-- unit test plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M5</version>
</dependency>
</dependencies>
<configuration>
<argLine>${argLine} -Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
[...]
</project>
Sub-modules pom.xml:
<project>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>[path]</include>
</includes>
</configuration>
</plugin>
</plugins>
[...]
</project>
If you use Jenkin, you can just use jacoco plugin and <goal>report</goal> without other new things.

Related

maven-surefire-plugin isn't working with profiles

I have profile configuration in my POM with surefire-maven-plugin & junit connection to run only specific tests by profile. For example:
mvn clean test -Pgroup1 --also-make -DfailIfNoTests=false
It works as expected with following versions:
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<junit.version>4.12</junit.version>
But stops working normally when I try to upgrade them:
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<junit.version>4.13</junit.version>
In this case mvn test always run all tests as I wouldn't set profile in command line.
My config of profiles is:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/unit/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>group1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/unit/**</exclude>
</excludes>
<groups>com.Group1</groups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>group2</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/unit/**</exclude>
</excludes>
<groups>com.Group2</groups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
......................
</profiles>
Every test class has connected interface linked to profile:
#Category(Group1.class)
#RunWith(JUnitParamsRunner.class)
public class Group1Test {
Playing with 'default' profile and 'activeByDefault' property also gave me no result. Any ideas how to fix it?
I got this to work by using "executions" in both the default plugin and in the profile plugin (which is not, by the way, an override of the default one)
<project>
...
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M8</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals><goal>test</goal></goals> <!-- REQUIRED -->
<configuration>
<enableAssertions>true</enableAssertions>
<systemPropertyVariables>
<log4j.debug>true</log4j.debug>
<client.test.url>http://localhost:8080/axis/services/MyService</client.test.url>
</systemPropertyVariables>
<excludes>
<exclude>**/TestAccountOp</exclude> <!-- Tomcat required - use the profile -->
<exclude>**/TestWsdl</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>myprofileid-testwithtomcat</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M8</version>
<executions>
<execution> <id>default-test</id> <phase>none</phase> </execution> <!-- Disable default Maven execution -->
<execution>
<id>myexecutionid-testwithtomcat</id>
<phase>test</phase>
<goals><goal>test</goal></goals> <!-- REQUIRED -->
<configuration>
<enableAssertions>true</enableAssertions>
<systemPropertyVariables>
<log4j.debug>true</log4j.debug>
<client.test.url>http://localhost:8080/axis/services/MyService</client.test.url>
</systemPropertyVariables>
<excludes>
<exclude>**/TestWsdl</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
...
</profiles>
</project>
In the profile, I removed the "default-test", otherwise the "excludes" were set from the default one, but that might have been before I moved the "configurations" into the "executions". You have to remember that "default-test" is active unless you disable it, but not using an "execution" in the main body did not work for me.
"default-test" is Maven's "id" for the one in the main body, so that is why I used that name in the "execution" in the main body.
I think you can get away with not bothering with the "phase" elements, because that's the default for the "test" goal, but I'm pretty sure that you need the goal.
Good luck!

How to exclude and include properly classes, packages and jar classes, lib from the jacoco report (Instrumentation offline)

I am using JaCoCo code coverage, but the report is including classes from jar, lib. (Offline Instrumentation, Maven)
I solved the problem with the offline configuration since "aspectj-maven-plugin" was changing the class files, and also now I successfully exclude the packages outside of target/classes -> src. thanks to this answer in stackoverflow.
But now I am getting the classes from jar, lib inside the report and I have not idea how to exclude then. I Show my configuration and examples below
I also tried this solution Exclude classes of jar files from jacoco coverage report But it doesn't work for me.
<exclude>**/lib/*</exclude>
My jacoco offline configuration:
<properties>
<jacoco.version>0.8.4</jacoco.version>
<argLine></argLine>
</properties>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
</dependency>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
<configuration>
<!-- this configuration affects all goals -->
<excludes>
<exclude>*</exclude>
<exclude>com/company/rrPackage/**/*.class</exclude>
<exclude>org/**/*.class</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
surefire-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<testNGArtifactName>...</testNGArtifactName>
<suiteXmlFiles>
<suiteXmlFile>...</suiteXmlFile>
</suiteXmlFiles>
<skip>${skip.test}</skip>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<properties>
...
</properties>
</configuration>
</plugin>
And the reason what I think that I am getting classes from jar inside de jacoco:report. In my pom.xml I have the following dependencies:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
</dependency>
Also I have a couple of import in my classes like this
import org.hsqldb.lib.StringUtil;
for example:
This has no dependency on the pom.xml but is used in one of the project classes, and jacoco shows it in the report
import javax.mail.internet.AddressException;
I have other cases with the same behavior that result in the same problem: Jacoco show those classes from jar in the report, as shown in the images
Try includes instead of excludes. Notice that you need .class at the end.
try something like that:
<configuration>
<includes>
<include>com/company/package/**/*.class</include>
</includes>
</configuration>
Base on your example:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
<configuration>
<!-- this configuration affects all goals -->
<includes>
<include>com/company/packageToInclude/**/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I don't know how you generate lib directory, because you don't provide complete example.
However in case of the following example
src/main/java/Example.java
class Example {
}
src/test/java/ExampleTest.java
public class ExampleTest {
#org.junit.Test
public void test() {
new Example();
}
}
and pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<jacoco.version>0.8.4</jacoco.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>test-compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>junit</includeArtifactIds>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
execution of mvn clean verify produces
$ ls -R target/classes
target/classes:
Example.class lib
target/classes/lib:
junit-4.12.jar
and following report
And after addition of following <configuration>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>instrument</goal>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
<configuration>
<excludes>
<exclude>lib/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
execution of the same command mvn clean verify produces following report
If the above doesn't help, then please provide absolutely complete example allowing everybody else to reproduce exactly the same what you do.

Package Grails App to war using maven

I have a grails app converted to maven project.
When I do mvn compile, I expect class files to be generated in target folder, but I get the below message.
--- maven-compiler-plugin:3.1:compile (default-compile) # sampleService --- [INFO] Nothing to compile - all classes are up to
date
I have the following snippet in my pom.xml. Please help
<build>
<sourceDirectory>grails-app</sourceDirectory>
<pluginManagement/>
<plugins>
<!-- Disables the Maven surefire plugin for Grails applications, as we have our own test runner -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4</version>
<configuration>
<filesets>
<fileset>
<directory>plugins</directory>
<includes>
<include>**/*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.grails</groupId>
<artifactId>grails-maven-plugin</artifactId>
<version>2.4.4</version>
<configuration>
<grailsVersion>${grails.version}</grailsVersion>
</configuration>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>clean</goal>
<goal>maven-war</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>grails</id>
<name>grails</name>
<url>https://repo.grails.org/grails/core</url>
</repository>
<repository>
<id>grails-plugins</id>
<name>grails-plugins</name>
<url>https://repo.grails.org/grails/plugins</url>
</repository>
</repositories>
<profiles>
<profile>
<id>tools</id>
<activation>
<property>
<name>java.vendor</name>
<value>Sun Microsystems Inc.</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
Either you have Maven folder structure or if its grails-app give packing in pom.xml as below
<packaging>grails-app<packaging>
I've encountered this before while working with groovy and maven. The problem was detailed here
Basically, you have to add a src/main/java folder to your project in order for the plugin to suddenly find all your groovy files. Without knowing your folder structure I can't tell if this is the issue or not.
Hope that helps!

Ensure test jars are named correctly

I'm setting up our system to do dual building for different versions of java artifacts based on the jdk used. There are 4 jars to build: artifact, artifact-tests, artifact-sources, and artifact-test-sources. Here is the output of the build
[INFO] Installing /Users/carlos/workspace/svn/Libraries/artifact-name/trunk/pom.xml to /Users/carlos/.m2/repository/package-path/artifact-name/1.0.8-SNAPSHOT/artifact-name-1.0.8-SNAPSHOT.pom
[INFO] Installing /Users/carlos/workspace/svn/Libraries/path/artifact-name-1.0.8-SNAPSHOT-java6.jar to /Users/carlos/.m2/repository/package-path/artifact-name/1.0.8-SNAPSHOT/artifact-name-1.0.8-SNAPSHOT-java6.jar
[INFO] Installing /Users/carlos/workspace/svn/Libraries/path/artifact-name-1.0.8-SNAPSHOT-sources.jar to /Users/carlos/.m2/repository/package-path/artifact-name/1.0.8-SNAPSHOT/artifact-name-1.0.8-SNAPSHOT-sources.jar
[INFO] Installing /Users/carlos/workspace/svn/Libraries/path/artifact-name-1.0.8-SNAPSHOT-test-sources.jar to /Users/carlos/.m2/repository/package-path/artifact-name/1.0.8-SNAPSHOT/artifact-name-1.0.8-SNAPSHOT-test-sources.jar
[INFO] Installing /Users/carlos/workspace/svn/Libraries/path/artifact-name-1.0.8-SNAPSHOT-tests.jar to /Users/carlos/.m2/repository/package-path/artifact-name/1.0.8-SNAPSHOT/artifact-name-1.0.8-SNAPSHOT-tests.jar
You can see the main artifact is built with java6 and has the appropriate classifier. I'm assuming the test classifier is overwriting the java6 classifier, but I'm unsure. Is there a way to get it to be named explicitly for both tests and the jdk? Something like -1.0.8-SNAPSHOT-tests-java6.jar. I'de like to refrain from doing manual changes to the final.name if possible and just use stock functionality like I did for the main artifact.
Here are the relevant parts of the pom.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>analyze</id>
<goals>
<goal>analyze-only</goal>
</goals>
<configuration>
<failOnWarning>true</failOnWarning>
<ignoreNonCompile>true</ignoreNonCompile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
<!--<configuration>-->
<!--<skip>true</skip>-->
<!--</configuration>-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classifier>${jdk.version.display}</classifier>
</configuration>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>java6</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.6</jdk>
</activation>
<properties>
<jdk.version>1.6</jdk.version>
<jdk.version.display>java6</jdk.version.display>
</properties>
</profile>
<profile>
<id>java7</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<jdk.version>1.7</jdk.version>
<jdk.version.display>java7</jdk.version.display>
</properties>
</profile>
<profile>
<id>java8</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<jdk.version>1.8</jdk.version>
<jdk.version.display>java8</jdk.version.display>
</properties>
</profile>
</profiles>
The -sources, -tests and -test-sources JARs are themselves using classifiers. So e.g. in the case of sources, you would need to override the maven-source-plugin's <classifier> configuration option (see also Maven deploy + source classifiers). I doubt that doing this is well tested across all the tool sets that consume -sources artifacts. For example, will Eclipse still download the sources for your java6 classifier artifact if you call the classifier java6-sources? And what about the tests, test-sources and (if you need it later) javadoc classifiers—will you complicate your POM further to generate all of those differently as well? Perhaps you could make it all work, but rather than trod down that path, it would be easier to simply use two different artifactIds, one for java6 and one for java7, and leave classifiers out of the equation.

Maven multi-module project site with javadocs

I would like use Maven for creating site for my application. This is a multi-module app, the parent module is simple site module, and first child is a core of app, the second is a GUI (Swing).
I now use follow for parent pom.xml
<modules>
<module>core</module>
<module>kayako-desktop</module>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.2</version>
<configuration>
<locales>en</locales>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<artifactId>maven-changes-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</reporting>
My core's pom:
<build>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<phase>package</phase>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
<links>
<link>http://download.oracle.com/javase/6/docs/api/</link>
</links>
</configuration>
</plugin>
</plugins>
</reporting>
(I stripped out unrelated parts from both)
The problem: I tried mvn site:stage, but javadoc is not collected from core module. What do I wrong?
Configure the javadoc plugin in the <reportPlugins> section of the configuration for the maven-site-plugin, in the parent pom.
Here's what worked for me.
In the parent pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<reportSets>
<reportSet>
<id>aggregate</id>
<reports>
<report>aggregate</report>
</reports>
</reportSet>
</reportSets>
<configuration>
<!-- Here you can add special configurations for your javadoc, if needed -->
</configuration>
</plugin>
<!-- Here you can also configure more report plugins -->
<!-- for your site, such as maven-project-info-reports-plugin -->
</reportPlugins>
</configuration>
</plugin>
<!-- ... -->
</plugins>
</build>
<!-- ... -->
<distributionManagement>
<site>
<id>website</id>
<url>http://site.url/can/be/tentative/or/hypothetical</url>
</site>
</distributionManagement>
In each of the child poms, you can also configure specific reports for the site plugin, for example, surefire test reports or project info. However, you shouldn't need to place any javadoc plugin configurations there (unless you also want non-aggregated javadocs for your child modules).
You should then be able to do mvn site site:stage from the parent directory. To view your aggregated javadocs, point your browser to target/staging/index.html in that directory, and click "Project Reports" and then "JavaDocs" in the index on the left-hand side of the page.
Additional tip:
Sometimes I want to look quickly at the aggregated javadocs without having to do an entire site site:stage, which does more stuff and takes longer. So I also include a configuration for the maven-javadoc-plugin directly in the <plugin> section of the parent pom. That way, I can run mvn javadoc:aggregate and quickly get the aggregated javadocs in target/site/apidocs/index.html.

Categories

Resources