I have a Scala project built with SBT. I use Jenkins to do my build, and include the following as a build target:
scoverage:test
This gets me very nice coverage reports for my Scala sources using the Cobertura plugin for Jenkins.
My project has Java files as well, however, and I get no coverage for them. If this were Maven, I'd know what to do. But being SBT, I'm a bit unclear on how I can get coverage for the Java sources and then ensure that Jenkins sees the output and displays it as well.
I do know that the Java tests run using "sbt test" so that's not an issue.
Is this even possible?
Related
is there any reason for a jacoco coverage report to produce different results when running it in a CI environment?
I have the following situation after migrating to Java 17. when I run ./gradlew clean build the report is generated and the jacocoTestCoverageVerification doesnt fail.
But when the same command is executed in a github runner I have a very different result, the coverage report is also created with different coverage values and some classes just fail the coverage verification.
Unfortunately, I cannot provide code snippets but I already tried this:
Run the github runner on my machine, with the same jdk and it produces a different result if I just run the build in the command line :(
add jvmargs noverify for tests tasks
run the github action in a container instead of a self-hosted, same result
other devs have also built the project on different machines without error
run the same build command of the ci
So it seems that something in a github action env produces this behavior.
versions:
jacoco plugin: 0.8.7
gradle 7.3
java 17
The problem here was lombok config files. It turns out that the new version of lombok plugin compatible with java 17 and Gradle 7.3 does not generate the lombok.config files anymore.
In the previous version, these files were generated by io.freefair.lombok plugin, and we had them in .gitignore. Because of that, a local build worked just fine, since the files were still there.
I realized this by looking at the generated reports and I noticed that lombok classes were being analyzed only by the CI.
In other cases, a similar problem could be related to this:
https://www.eclemma.org/jacoco/trunk/doc/classids.html
I'm using Eclipse and working on a J2EE project. We are trying to write Groovy/Spock tests for unit testing. The project is a Maven project and added Groovy nature to it. The problem is If I edit any of the Groovy test classes, It's doesn't get compiled. So running a test as Junit is not picking up the latest changes. I have to run a Maven build in order to pickup the recent changes test class.
My setup,
Groovy compiler 2.4
Installed Groovy-Eclipse plugin
Added Groovy nature to the project
Checked another question, it didn't help much
Another Question
Un checked, the following fixed the issue.
Preferences > Groovy > compiler > Enable script Folder support
I have two separate projects, Product and CompoTestProduct. Product is written in Scala and uses sbt while CompoTestProduct is written in Java and uses maven. Product will be deployed in a server and CompoTestProduct's tests classes will be ran locally. I tried to use scoverage as it says that it supports Multi project reports but I still have no idea how to do this and I'm still struggling to make the samples work. I am getting this error on the sample projects:
[error] Not a valid command: coverage
[error] Not a valid project ID: coverage
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: coverage (similar: homepage, package, compilerCache)
[error] coverage
[error] ^
I am new to this code coverage testing thing. Do you have other tutorials or maybe know other framework/toolkit that can generate test code coverage report for do this setup?
The multi project support refers to being able to aggregate the results of multiple reports.
Scoverage instruments your code when you activate coverage on the sbt command line. So sbt clean coverage compile would be enough in your Product project to get the classfiles instrumented.
Then you could run your unit tests as normal with Maven. At this point Maven needs to be configured to also use Scoverage, as it will need to write out the coverage data once it is completed.
Then you would need to run the report step after.
So, summary, it is possible, with a LOT of hassle, but why are you going through these hoops to have an awkward project setup? Just move your tests into the main project, do a combined java/scala compile and it should be much easier as you could run the entire build via sbt or maven, and not both mixed.
official documentation http://docs.sonarqube.org/display/SONAR/Analyzing+with+Maven says that the proper way of invoking sonar is:
mvn clean install -DskipTests=true
mvn sonar:sonar
but doesn't say why. how does sonar work? does it need compiled classes? so why not just mvn clean compile? or does it need a jar file? so why not just mvn clean package? what exactly does sonar plugin?
Explanation from a SonarSource team member:
In a multi-module build an aggregator plugin can't resolve dependencies from target folder. So you have two options:
mvn clean install && mvn sonar:sonar as two separate processes
mvn clean package sonar:sonar as a single reactor
I was surprised too, so I made a tweet an received the following answer from the official Maven account:
If the plugin is not designed to use the target/classes folder as a substitute, then yes you would need to have installed to get the jar when running *in a different session*. Complain to the plugin author if they force you to use install without foo reason [ed - #connolly_s]
The SonarQube analyzer indeed needs compiled classes (e.g for Findbugs rules, coverage). And since by default it executes tests itself, the compile phase can skip tests.
You can run SonarQube as part of a single Maven command if you meet some requirements:
As Mithfindel mentions, some SonarQube plugins need to analyze .class files. And if you run unit tests outside of SonarQube, then of course the testing plugins must read output from the test phase.
Got integration tests? Then you need to run after the integration-test phase.
If you want to run SonarQube as a true quality gate then you absolutely must run it before the deploy phase.
One solution is to just attach SonarQube to run after the package phase. Then you can get a full build with a simple clean install or clean deploy. Most people do not do this because SonarQube is time-consuming, but the incremental mode added in 4.0 and greatly improved in the upcoming 4.2 solves this.
As far as the official documentation goes, it's a lot easier to say "build and then run sonar:sonar" then it is to say, "open your POM, add a build element for the sonar-maven-plugin, attach it to verify, etc".
One caveat. SonarQube requires Java 6, so if you're building against JDK 1.5 (still common in large organizations), the analysis will have to happen in a separate Maven invocation with a newer JDK selected. We solved this issue with custom Maven build wrapper.
I'm looking for a plugin that would run in a console continuously to scan a Maven project's test sources directory, and when it detects a change kicks off a test cycle. Something analogous to mvn scala:cc or the Scala Build Tool, but for Java. Can anyone point me towards one?
I have personally used sbt even for a java only project just for continuous test feature.
I added a sbt build file to a maven based project and use sbt when developing, but use maven when building the final package, starting embedded jetty etc and this has worked out quite well.
I've just discovered that the scala-maven-plugin supports both continuous compilation & testing, as well as cross-compilation (Java + Scala). So it's possible to use it over a pure Java build and get all the continuous build goodness.
Recently, I have had a need for a solution to this. Having been learning scala and finding about the goodness of sbt ~test, I want to apply it to Java projects that do not have continuous test.
Using the scala-maven-plugin that you mentioned, I have created a github seed that will run Java Junit tests everytime I save a Java source or a Java test.
Check it out:
https://github.com/ailveen/maven-scala-java-continuous-test
The project is very simple right now (contains only Java files because that is my current need) but in the future I hope to add scala test (or specs2 or scalacheck) so it works with mixed java and scala files.
Hope you find it useful.
It is not exactly for Maven, but JUnit Max does continuous testing and if you're on Eclipse it might be the tool you would like to check out