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.
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 have a Maven artefact that includes data computed at compile-time
This is achieved by using the exec-maven-plugin to call the generating class.
This is correctly being executed when the artefact is built, but when reporting plugins are added, the execution is happening multiple times, slowing it down significantly.
Take the following 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.me</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<main.class>test.Runner</main.class>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<id>${main.class}</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
When clean install site is run, the test.Runner is called three time - and each time a reporting plugin is added, it's run again.
Try changing the reports config to this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<reportSets>
<reportSet>
<reports>
<report>report-only</report>
</reports>
</reportSet>
</reportSets>
</plugin>
This tells Maven which reports should be run. Without that reportSet config,
every reporting goal available in the plugin is rendered once
per the Maven Site plugin docs. This is important because as #khmarbaise notes, the report mojo
Invokes the execution of the lifecycle phase test prior to executing itself.
I use Spring Tool Suite 4. We have a spring-boot API and we would like to create an executable (jar or war, no clue, which one i need). We have dependencies stated in pom.xml. By default this is what we have under build if you make a clean spring-boot application:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
So my question what do i have to add so i can actually execute maven install for my executable? As of now if i run maven install (which for me the only way i know to create executable), it gives me an AssertionError. I tried to find guides online, but none of them worked.
For now i would be happy if i could even manage to execute maven install, but my plan includes the usage of config file outside of the executable also i would like to log (log4j2) next to the jar file, so i need help with those too. How to specify these in pom.xml so i can use config file from outside and to be able to log outside?
You'll want an executable jar. Add this to your pom file:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
specify.your.main.Class
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
So I got AssertionError, because for some reason some of my tests dont work for maven (even though if i execute them they pass).
It actually matters if jar or war. In my case it has to be a war because im making an API
Also here are the minimum you have to change in pom.xml to have a chance to create executable: (this goes before dependencies inside the pom.xml)
<packaging>war</packaging>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</properties>
The following part goes in between <build></build>.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.class</mainClass>
</manifest>
</configuration>
</plugin>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>org.apache:apache-jar-resource-bundle:1.0</resourceBundle>
</resourceBundles>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
To sum up I still have no idea what most of these do, but this works.
I have a local jar that I need to add it as a dependency to my maven project to be included in the published jar of the project.
It is placed in the project at "my_project/lib/external1.jar" , at first I added it to dependencies as follow:
<dependency>
<groupId>installExternalJars11</groupId>
<artifactId>externalJar11</artifactId>
<version>3.1.14</version>
<systemPath>${project.basedir}//lib/external1.jar</systemPath>
<scope>system</scope>
</dependency>
It compiled successfully, but the jar content is not included in the resulting project jar, hence, when I use the project jar I get NoClassDefFoundError for these classes, although I am using the following plugins which shall package all the dependencies in the output jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
I found several posts recommending to use the maven install plugin to install it, then add it as a dependency, so I followed this approach as follow:
<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</groupId>
<artifactId>waheed</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>waheed</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.maven-install-plugin>2.5.2</version.maven-install-plugin>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>${version.maven-install-plugin}</version>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>installExternalJars</groupId>
<artifactId>externalJar1</artifactId>
<version>3.1.14</version>
<file>${project.basedir}/lib/external1.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<executions>
<execution>
<id>install-ibm-foundation</id>
<phase>validates</phase>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>installExternalJars11</groupId>
<artifactId>externalJar11</artifactId>
<version>3.1.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
In eclipse I get the dependency highlighted with following error:
Missing artifact installExternalJars11:externalJar11:jar:3.1.14
and when I run maven build (from eclipse: right click pom.xml > run as > maven build) I get the following error (apparently maven is looking for the jar in my nexus repository although it is referred as local file in the install section):
Failure to find installExternalJars11:externalJar11:jar:3.1.14
in http://ur_to_nexus/nexus/content/repositories/central/ was cached in
the local repository, resolution will not be reattempted until the
update interval of ubknexus has elapsed or updates are forced
Tried the fixes proposed here https://stackoverflow.com/a/6112344/458999 but did not work
P.S : When I add the Jars to the build path in eclipse (Java build Path > Libraries > Add Jars) and run the project from eclipse (or export from eclipse and run the exported jar) it works
The system scope subtly makes Maven behave different which bites you now.
Install the artifact in your local Maven repository server (Nexus or Artifactory) or identify an equivalent artifact on Maven Central. If commercial software, ask your vendor for access to their repository.
If you cannot do that you can install locally as part of your build. See how it can be done at Multiple install:install-file in a single pom.xml. This should be considered a short term solution though.
I recently converted one of my Java project to a Maven project. My pom.xml looks something like this:
<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>
<packaging>jar</packaging>
<groupId>com.myproject.groupid</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyProject</name>
<description>The first version of my maven project</description>
<dependencies>
<dependency>
<groupId>com.dependent.jar</groupId>
<artifactId>dependentjar</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<type>jar</type>
<systemPath>${project.basedir}/jars/dependent.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myproject.main.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</build>
</project>
When i execute the mvn compile and mvn install, the project works fine and it also generates the jar. But when i try to run the jar [using java -jar MyProject.jar], i get an error which says: Exception in thread "main" java.lang.NoClassDefFoundError and this is because maven is not able to add the dependent jar specified in the section. [it is not available during run time]
Could anybody let me know the best possible way for me to copy the systemPath jars to the jar that is being generated by maven?
I looked at maven-shade-plugin and maven-assembly-plugin and could not find much luck with both of them. Any help would be appreciated!
Try to add this to your maven-jar-plugin configuration
<addClasspath>true</addClasspath>
<classpathPrefix>*path to dependencies*</classpathPrefix>
Also to manage your dependencies you can use Maven dependency plugin
In the build section of your pom, you can use:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com:library:**</include>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then, on the command line, you can run mvn package, it will show in the output which jars it is excluding. Then, you can include selected jars accordingly. If you then get NoClassDefFoundError, you may need to point Java to your class, by for example using com.company.Main as an argument to the java command.