Since I had some problems using cobertura with Java 7 - I'm trying Jacoco.
My project has a parent pom.xml and sub projects.
In one project I use spring to run some integration tests - so I have this plugin in this project's pom.xml:
<plugin>
<version>2.12.4</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- -XX:-UseSplitVerifier is for java 7 -->
<argLine>
-XX:-UseSplitVerifier
-javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar
</argLine>
</configuration>
</plugin>
Since I use Java 7, I've set this plugin in the parent pom.xml:
<plugin>
<version>2.5.1</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<debug>true</debug>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<encoding>utf8</encoding>
</configuration>
</plugin>
Now, when I'm using mvn clean install all the projects have this file in the /target folder: jacoco.exec
; But this project that uses the spring-instrument does not have this file.
I think the problem is that Jacoco wishes to use the asm of spring-instrument but it fails (but I'm not sure I'm right).
The Jacoco version is 0.6.3.201306030806.
Why does Jacoco fails to instrument in this case?
How can I overcome this?
I thought maybe I can configure the maven-compiler-plugin to compile the code to 1.6 and then I wouldn't need the maven-surefire-plugin plugin. Does it make sense?
The argLine value defined by Jacoco Maven Plugin is being rewritten by the Surefire Plugin.
Set a property name in your "jacoco-maven-plugin" configuration like this:
<propertyName>coverageAgent</propertyName>
and then edit the argLine in your surefire plugin configuration so it includes Jacoco's agent:
<argLine>
-XX:-UseSplitVerifier
${coverageAgent}
-javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar
</argLine>
Notice that the Jacoco's agent is placed before the Spring's Instrument. That's the way it should be done because Jacoco has problems dealing with modified bytecode (e.g. the one produced by AspectJ LTW).
Actually, even when being the first agent, Jacoco's reports can still be wrong, but the problem is usually limited to a small set of circumstances (e.g. http://sourceforge.net/p/eclemma/discussion/614869/thread/3d875388 ).
my guess is that JaCoCo also uses the java command line, as it is probably implemented as a javaagent.
could it be that the spring-instrument javaagent overrides the JaCoCo one ?
Related
I do not know the difference between spring-boot-maven-plugin and maven-compiler-plugin.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Is that mean Spring Boot Maven Plugin's feature include maven-compiler-plugin?
I just use Spring Boot Maven Plugin is ok, do not need add 2 plugins??
"Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”."
"Maven Compiler Plugin is used to compile the sources of your project."
maven-compiler-plugin has two goals. Both are already bound to their proper phases within the Maven Lifecycle and are therefore, automatically executed during their respective phases.
compiler:compile is bound to the compile phase and is used to compile the main source files.
compiler:testCompile is bound to the test-compile phase and is used to compile the test source files.
To understand more about maven build lifecycle - http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference
maven-compiler-plugin usages -
To compile source code using -source and -target javac Options
To compile source code using a different JDK
To compile source code using Memory Allocation Enhancement
To Pass Compiler Arguments
Most commonly used to define source and target versions.
Sometimes you may want to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target. maven-compiler-plugin can also be configured to provide these options during compilation.
For example, if you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), you can either add the two following properties, which are the default property names for the plugin parameters:
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
or configure the plugin directly:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Technically we can use both spring-boot-maven-plugin and maven-compiler-plugin in combination if the requirement is to create an executable jar as well as make sure source and target code have a specific version (which is accomplished by including maven-compiler-plugin).
In my case, i didn't use in combination but when my java project is a spring boot app that needs to run as a micro-service etc then we need an executable jar as build output so used spring boot maven plugin (only) but my other java project that consists of spring beans or components and is going to be used as a spring enabled library in other external apps but not required to run on its own but had to make sure source and target versions are specified then normal "mvn package" generated jar should work. For that maven compiler plugin (only) should do the job.
Maven is a powerful build tool for Java software projects.
I was reading about Maven plugins. (maven-compiler-plugin)_
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
For any project to run using maven, it definitely need to compile the code.
But i can see many sample projects online which doesn't have this plugin and still work.
Can anybody explain what is the difference in this plugin and by default compiling?
If you do nothing, Maven will call the maven compiler plugin which is called in the maven default lifecycle.
If you want to change the configuration, you need to add the plugin to <plugins> and then change the configuration. Often people do this to change the Java version (which can also be done through properties) or to fix the version of the plugin (instead of always using the latest version).
My project uses two other libraries that are added to it as Maven dependencies. When I generate a coverage report for the project using the command prompt, it generates the jacoco.exec file and I can use it to see the coverage report in the IDE.
But it doesn't give me coverage over the dependencies used.
I have seen in the Eclipse plugin for JaCoCo where it gives the opportunity to configure coverage and select specific jars being used in the project to be included in the coverage report.
How can I to the same in the case of command line based execution?
My project use two other libraries which are added to it as a maven dependency.
If those libraries are other modules of the same Maven build, then please have a look at report-aggregate goal of JaCoCo Maven Plugin.
I have seen in eclipse plugin for JaCoCo where it gives opportunity to configure coverage and select specific jars being used in the project to be included in coverage report. But how to do same in the case of command line based execution?
In JaCoCo 0.7.9 there are JaCoCo Ant Tasks, and in 0.8.0 there will be JaCoCo Command Line Interface - both provide more freedom than JaCoCo Maven Plugin in specification of what should be included into report, in particular can be used for arbitrary (e.g. third-party) JAR files. Ant Tasks can be executed from within Maven build using maven-antrun-plugin.
And there is JaCoCo APIs with examples (in particular ReportGenerator.java) that can be used to build you own report generators, other integrations. Mentioned by you Eclipse plugin that is based on JaCoCo - is Eclipse EclEmma. And it uses exactly these APIs for integration into Eclipse IDE.
Use Codehaus plugin for better result which generates class wise coverage report. you can generate html as well as xml report.
<project>
...
<reporting>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.5.201112152213</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<excludes>
<exclude>**/*Config.*</exclude>
<exclude>**/*Dev.*</exclude>
</excludes>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
You can also provide in tag to exclude the classes for code coverage and similarly for tag for including the classes
I want to use maven to build projects in which there are unresolved compilation problems.
The main purpose is package and deploy or run aplications using some kind of stubs for classes that contains compilation errors, like I understand that Eclipse does (thanks to JDT Core).
I configurate maven java compiler plugin following Apache Maven documentation at Using Non-Javac compiler to use Eclipse compiler. Thinking that maybe should set some arguments to modify the compiler/builder behaivor I was reading Help Eclipse - Compiling Java code but I don't realize which compiler/builder option or combination of these does the trick.
So far, the next configuration of the maven java compiler plugins compile using the eclipse compiler and package the application including generated .class (jvm bytecode) only for java classes without compilation errors. To get this behaivor it just require use the eclipse compiler (see compilerId and the dependency) and set failOnError=false.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>eclipse</compilerId>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<failOnError>false</failOnError>
<compilerArguments>
<org.eclipse.jdt.core.compiler.problem.fatalOptionalError>disabled</org.eclipse.jdt.core.compiler.problem.fatalOptionalError>
<org.eclipse.jdt.core.compiler.problem.forbiddenReference>ignore</org.eclipse.jdt.core.compiler.problem.forbiddenReference>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</plugin>
With this configuration I could run java application as long as the execution doesn't use classes not included for compilation errors (because the stubs aren't generated) but on a Java EE container, the classloading will faild so the application can never be deployed.
I appreciate any help on this.
Just for share the solution, at that moment I just replace the plexus-compiler-eclipse with tycho-compiler-jdt to get the desire behaivor.
The proceedOnError parameter indicates that it must keep compiling in spite of errors, dumping class files with problem methods or problem types how to deal with the compilation errors.
Next is the final configuration sample.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>jdt</compilerId>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<failOnError>false</failOnError>
<compilerArguments>
<proceedOnError/>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-jdt</artifactId>
<version>0.22.0</version>
</dependency>
</dependencies>
</plugin>
There is more plugin configuration examples in the Tycho FAQ.
And the possible compiler arguments are described in section Using the batch compiler of the Java development user guide (Eclipse Help site).
I am currently working on a project which will run on an embedded device. The device runs a Java ME JRE (comparable to Java 1.4).
Because of this maven is configured to compile for source & target level 1.4.
Is it possible to run the maven test phase on a different source/target level? Because this way I could use Mockito for unit-testing.
The source and target versions can be set separately for the compile and testCompile goals of the maven compiler plugin. You can change the settings either by defining properties in your pom:
<properties>
<maven.compiler.source>1.4</maven.compiler.source>
<maven.compiler.target>1.4</maven.compiler.target>
<maven.compiler.testSource>1.5</maven.compiler.testSource>
<maven.compiler.testTarget>1.5</maven.compiler.testTarget>
</properties>
Or by explicit configuration of the compiler plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
<testSource>1.5</testSource>
<testTarget>1.5</testTarget>
</configuration>
</plugin>