I have multi-module project with structure like:
my-project
- moduleA
- moduleB
- moduleC
pom.xml for moduleA configured like:
<profiles>
<profile>
<id>withArtifacts</id>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.ekiryuhin</groupId>
<artifactId>moduleB</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.ekiryuhin</groupId>
<artifactId>moduleC</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>
moduleB,moduleC
</includeArtifactIds>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then:
Add some code in classes inside moduleB and moduleC.
cd to my-project/moduleA.
Run mvn clean install -PwithArtifacts -DskipTests -am
And finally I have jar files in ${project.build.directory}/lib but they do not contain my edits from (1).
Why maven may do not rebuild dependencies before copy?
UPD:
pom.xml from moduleB:
You need build all the modules for that. Go the main project my-project and call mvn clean install. You also need to make sure that moduleA depends on moduleB and moduleC so that the build order will be correct.
Related
I want to run groovy scripts as part of parent POM which has POM packaging.
I set maven-groovy-plugin configured as follows:
<groupId>org.sct</groupId>
<artifactId>app-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>app</name>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
...
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${project.basedir}/src/test/groovy/check-xsl-id.groovy</source>
</configuration>
</plugin>
...
However the script is not executed during the build.
If I run the goal individually mvn groovy:execute it works fine.
How can I bind plugin execution to a POM packaging phase?
Using <plugin> instead of <pluginManagement> for plugin configuration works perfectly well.
I want to share my log4j property file with other modules within a project. I found out that maven-remote-resource plugin is one of the solution, but I have some problems using it.
The log4j property file is intended to be used only on test, so the tests on all module will reference the same log4j file.
The following is the structure of my project and submodules.
parent-module
-module_A
-src
-test
-properties
-log4j.properties
pom.xml
-module_B
-src
-main
-java
-src
-test
-java
pom.xml
-module_C
-src
-main
-java
-src
-test
-java
pom.xml
-module_D
-src
-main
-java
-src
-test
-java
pom.xml
pom.xml
The pom on "module_A" (the one to share resource, the log4j.properties) is as follows:
<build>
<resources>
<resource>
<directory>${basedir}/src/test/resources/</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
and on the pom of parent module, I added the module_a to the dependency list and call the remote resource plugin
<dependency>
<groupId>com.myproject</groupId>
<artifactId>module_A</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>com.myproject:module_A:${project.version}</resourceBundle>
</resourceBundles>
<attachToMain>false</attachToMain>
<attachToTest>true</attachToTest>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I tried to do maven clean install on the whole project, each individual module doesn't see the log4j.properties.
But if I move the remote-resource to every individual sub module pom, they can see the property.
Is there a way to share the log4 property by declaring once on the parent pom?
I would suggest to make the Module_A a default jar which contains the resources in src/main/resources...nothing needed to be declared or configured. Only put the properties into the src/main/resources directory..everything will be packaged into a ja file..
<project>
<parent>
...
</parent>
<artifactId>module_A</artifactId>
</project>
And add the above module as a test dependency like:
<dependency>
<groupId>com.myproject</groupId>
<artifactId>module_A</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
in each module which needs the configuration. Than you have the properties on the test class path and it will work...
Seems like there are couple of questions, which are quite old and things changed from Java 8 support of Jacoco.
My Project contains following structure
pom.xml
|
|
-----sub module A pom.xml
|
|
-----sub module B pom.xml
|
|
-----sub module C pom.xml
I have configured the main pom like this
Main 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.test</groupId>
<artifactId>jacoco-multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>projectA</module>
<module>projectB</module>
</modules>
<properties>
<jacoco.version>0.5.7.201204190339</jacoco.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<destFile>${project.basedir}/../target/jacoco.exec</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>default-integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
</plugins>
</build>
</project>
A 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>
<parent>
<artifactId>jacoco-multi</artifactId>
<groupId>com.test</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>projectA</artifactId>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
B 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>
<parent>
<artifactId>jacoco-multi</artifactId>
<groupId>com.test</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>projectB</artifactId>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>projectA</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</pluginManagement>
</build>
I am executing this command mvn clean package. I can see jacoco.exec is getting generated, however I am not able to see any HTML reports are being to verify the data.
Please help me with this.
Another point is, my configuration is correct for Multi-Module projects?
Update
Identified issue.
<destFile>${project.basedir}/../target/jacoco.exec</destFile>
changed to
<destFile>${project.basedir}/target/jacoco.exec</destFile>
Now it's generating reports for individual modules.
Any idea how to generate consolidated report
JaCoCo version 0.7.7 can generate an aggregate coverage report from multiple Maven modules through a new goal jacoco:report-aggregate.
After scanning many solutions I created a simple but complete Jacoco demo project showing:
Multi module project
Unit test (via mvn clean install)
Integration test (via mvn clean install -P integration-test)
Jacoco - test coverage ( both aggregate data file and aggregate reporting)
FindBugs - code quality
Enjoy the simple demo project. In this simple project the README.md file contains information you are looking for. An example is:
The simple demo project contains 3 branches:
Master branch - containing the above functionality
Multi-module-only-unit-tests - contains modules with only unit tests
Multi-module-unit-tests-try2 - contains modules with unit tests, differently.
Follow below-mentioned instructions
Create a new sub-project (usually called maven module). This will be used as report aggregator.
parent pom will be like:
<modules>
<module>A</module>
<module>B</module>
<module>C</module>
<module>ReportAggregator</module>
</modules>
In aggregator module pom- add other subprojects dependencies.
<dependency>
<groupId>xyz</groupId>
<artifactId>A</artifactId>
<version>${project.version}</version>
</dependency>
In aggregator module pom- configure jacoco plugin
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFileIncludes>
<dataFileInclude>**/jacoco.exec</dataFileInclude>
</dataFileIncludes>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
In aggregator module pom- configure surefire plugin as
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>**/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
(optional step) If anybody face warning/error like:
Classes in bundle '*' do no match with execution data. For report generation, the same class files must be used as at runtime.**
Then add below mentioned lines in aggregator module pom
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<id>instrument-ut</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>restore-ut</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFileIncludes>
<dataFileInclude>**/jacoco.exec</dataFileInclude>
</dataFileIncludes>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
run mvn clean install
One problem in multimodule projects is caused, if the aggregator pom is used as parent pom for the modules either, like it is the case in the above example:
- parentAggregator pom
---sub module A pom.xml -> parentAggregator pom
---sub module B pom.xml -> parentAggregator pom
---sub module C pom.xml -> parentAggregator pom
In this case, the build order is:
- parentAggregator
- sub module A
- sub module B
- sub module C
which means, that the parent aggregator can not collect complete information. In my case a transfer of data into sonarQube by mvn sonar:sonar resulted in unexpected and uncomplete results.
Changing the module structure to:
- aggregator pom
-- parent pom
---sub module A pom.xml -> parent pom
---sub module B pom.xml -> parent pom
---sub module C pom.xml -> parent pom
will change the build order to:
- parent
- sub module A
- sub module B
- sub module C
- aggregator
In this case aggregator will be the last one and work with the results of the modules. In my case the results in SonarQube were like expected.
Run Coverage as... Junit for each module separately. Then create a launch group and add each of the run configurations. There is a popup "Launch Mode" with options Inherit, Profile, Coverage, Debug or Run. Choose Coverage for all of them. You probably also want to select "Wait until terminated".
You can now run all the coverage tests in one click. After they complete you need to go into the Coverage View and select merge sessions (double red/green bar) and merge them all into one report.
I have a maven project which generates 2 artifacts.
1 main artifact - a war (packaging element in pom.xml) and
1 secondary artifact - a jar (using maven-jar-plugin) comprised of subset of classes that needs to be shared with other projects.
When I run mvn install, I need to only install the jar artifact and skip the war, and also generate the pom so that the installed jar can be imported by other projects.
I can generate the war and jar artifacts, but both the war and jar are installed in my local maven repo and when I try to import from another project it only shows the war.
I tried a few approaches but I have not been able to solve this.
<project>
<groupId>com.company.component</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>some-artifact</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<configuration>
<includes>
<include>com/company/component/common/*.class</include>
</includes>
<finalName>component-</finalName>
<classifier>common</classifier>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
I have following project structure:
Project "parent-project" does not have any source file and has subprojects as "junit-wrapper","child1-test" and "child2-test".
Subproject "junit-wrapper" has only java source inside src/main/java and this is basically created to wrap all the dependencies and binaries under the hierarchy "parent-project".
Subproject "child1-test" and "child2-test" has no source files and only contains subprojects as "child1-env" and "child2-env".
Subproject "child1-env" and "child2-env" has only junits inside src/test/java.
I want to build a super jar(within junit-wrapper) by building parent pom.xml
I hope this is possible by using maven-assembly-plugin but don't know how to configure this in pom.xml. what should be my pom.xml or assembly.xml(on using plugin) entries in order to ensure this is achieved?
please suggest.
thanks.
To create a jar which contains test-classes the best solution is to use the maven-jar-plugin like this:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
In other modules you can use the test-jar via the following dependency:
<project>
...
<dependencies>
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
You will get your "uber-jar" when you include this configuration into the pom file of junit-wrapper:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
An assembly descriptor (assembly.xml) is not necessary because the jar-with-dependencies descriptor is already available within the maven-assembly-plugin. Note that you should not execute the assembly plugin before the package phase. Otherwise the code of your maven module will not get packaged into your assembly.