Running groovy-maven-plugin within POM packaged project - java

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.

Related

Maven doesn't pick up resources on jenkins(linux)

I'm having really weird issue - I have created multi-module java project that creates "fat-jar". Unfortunetly the "frontend" module jar doesn't have resources in it when launched on jenkins(linux) - it does work on Windows for some reason:
This is the "frontend" pom:
<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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.frontend</groupId>
<artifactId>frontend-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<resources>
<resource>
<directory>dist</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>node build app</id>
<phase>prepare-package</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run-script build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I have tried with:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
But that did not help neither.
The default npm build goal is
ng build --output-path dist/META-INF/resources
Jar file in target does not include files from "dist" -> it also does not work when I create it in default location src/main/resources
The maven log output includes this:
[WARNING] JAR will be empty - no content was marked for inclusion!
The dist directory is created and filled during build. Maven locates files on Windows but does not on Jenkins.
EDIT:
The maven goals are the same.
You could check few things on Jenkins. First if mvn goal is exactly same. Second, whether all resources are checked out properly or not. Also, see if file write permission are there for the linux user used by Jenkins process. Finally, append a -X behind your goals on Jenkins to see maven logs in detail and debug your issue.

Creating jars for Maven repository, how?

Googled this question but no straight answer was found sadly.
So, I have a Maven project. I want to upload my release to Maven Central Repository. This means I need to create 3-4 jar files:
Compiled source jar
Javadoc jar
Source code jar
Tests jar (optional)
How can I create all these jars? Add some configuration to POM?
BTW, I am using latest Netbeans IDE, but this shouldn't matter :)
Any help is appreciated!
EDIT
So this is my POM:
<?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.company</groupId>
<artifactId>sdk</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.5.3</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Maven can create these jars for your. Next to 1,2,3,4 you will also need to sign your release artifacts. To get the artifact into maven central I would advise you to read:
The official Maven guide to get your artifact into maven central
The guide from the Open Source Sonatype repository. It contains all the information you will need to know.
With regards to your pom.xml you can add a separate release profile, which builds all these artifacts for you.
Just add the following profile to your pom.xml
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Now when you want to create the release you can just call:
mvn clean install -Prelease
and it will create all the required artifacts.
Take a look at this guide and this. You need to raise a ticket for a mirror repository of your choice, then you need to use additionally plugins for signing jar, generating documents, adding source and doing remote deployment for you.

Settting up line separator in Maven

We are working on a multi module Maven project where in one sub project we have some resources file (free marker template files). When we create EAR out of the project depending on Operating System Maven is updating line separator in template files.
If we run mvn install on windows it keeps line separator as and when we run it on linux it changes line separator to .
Template are created on Windows machine and have default separator as , we don't want Maven to changes to even if we build project on windows / linux. As code is always deployed on Windows machine and fails when encounters linux line separator.
Is there any way to tell Maven not to mess up with line separators?
Project POM looks like
<project>
<parent>
<groupId>com.xyz</groupId>
<artifactId>Foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>FooBatch</artifactId>
<packaging>jar</packaging>
<properties>
<spring-version>4.1.4.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.21</version>
</dependency>
// and more dependencies
</dependencies>
</project>
and here is the Parent POM
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>Foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>FooBatch</module>
<module>FooEJB</module>
<module>FooEAR</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<!-- skips surefire tests without skipping failsafe tests. Property
value seems to magically default to false -->
<skipTests>${skipUnitTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<argLine>${jacoco.agent.arg}</argLine>
</configuration>
</execution>
</executions>
<configuration>
<!-- skips failsafe tests without skipping surefire tests. Property
value seems to magically default to false -->
<skipITs>${skipITTests}</skipITs>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2.201409121644</version>
<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>
</plugins>
</pluginManagement>
</build>
</project>
Try to add the following to your parent pom.
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Cobertura on Jenkins Duplicate Test Runs

I have a build that includes cobertura as part of my Jenkins run. However, I realize now that it is running all my tests twice.
I have the project setup as a Maven project in Jenkins. and I had the goal to run set to: clean coberture:cobertura install
I had thought that the install portion of the command would simply run the remaining portions of the install goal, such as package. However, it reruns the compile and all the tests. This is despite the fact that there are already tests there.
I've tried configuring different combinations of pre build and post build steps, but I keep running into issues. In some combinations, the build artifacts, such as jars, never get published on Jenkins. In other cases the test results are missing.
I've thought that perhaps I need to remake the build as just a shell build. I think I could then run the command: mvn clean cobertura:cobertura && mvn install -Dmaven.test.skip=true
I think this would do what I want. It would at least stop running all the tests twice.
Is this the best approach or is there another way?
This is how I am including Cobertura in my POM:
<?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>
<parent>
<groupId>com.foo.bar</groupId>
<artifactId>my-parent</artifactId>
<version>1.2.1</version>
</parent>
<groupId>com.foo.bar.baz</groupId>
<artifactId>osom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<description>TODO</description>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<dependencies>
</dependencies>
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<useUnlimitedThreads>true</useUnlimitedThreads>
<parallel>suites</parallel>
<reuseForks>false</reuseForks>
<includes>
<include>**/*Test.java</include>
<include>**/Test*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<!-- use mvn cobertura:cobertura to generate cobertura reports -->
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<aggregate>true</aggregate>
<format>xml</format>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
The best approach I could come up with is to use a "Freestyle" project in Jenkins.
I made the project run two separate maven commands. mvn clean cobertura:cobertura and mvn install -Dmaven.test.skip=true
This prevents the tests from running twice.
If you dont want to run your tests twice, use qualinsight-mojo-cobertura maven plugin instead of cobertura-maven-plugin.

Jacoco Maven multi module project coverage

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.

Categories

Resources