Multi-language coverage reporter for Codacy https://www.codacy.com
codacy-coverage-reporter will only work with:
* Java JRE 8 and higher
Codacy assumes that coverage is previously configured for your project. The official coverage tools we support for Java are JaCoCo and Cobertura.
You can run the coverage reporter:
-
Download the latest jar from https://github.com/codacy/codacy-coverage-reporter/releases/latest
-
Run the command bellow
$ java -cp codacy-coverage-reporter-assembly-<version>.jar com.codacy.CodacyCoverageReporter -l Java -r jacoco.xml
To update Codacy, you will need your project API token. You can find the token in Project -> Settings -> Integrations -> Project API.
Then set it in your terminal, replacing %Project_Token% with your own token:
export CODACY_PROJECT_TOKEN=%Project_Token%
Codacy automatically detects the CommitUUID from several sources:
Environment Variables
- CI_COMMIT
- TRAVIS_PULL_REQUEST_SHA
- TRAVIS_COMMIT
- DRONE_COMMIT
- CIRCLE_SHA1
- CI_COMMIT_ID
- WERCKER_GIT_COMMIT
Git directory
- If it finds a git directory it will get current commit.
Force CommitUUID
- You may want to enforce a specific commitUUID with:
codacy-coverage-reporter -l Java --commitUUID "mycommituuid" -r coverage.xml
Upload coverage
Next, simply run the Codacy reporter. It will find the current commit and send all details to your project dashboard:
codacy-coverage-reporter -l Java -r coverage.xml
Note: You should keep your API token well protected, as it grants owner permissions to your projects.
Multiple coverage reports
If you have multiple modules generating multiple report files you need to merge them before invoking this reporter, since Codacy only receives one report per commit per language.
Most coverage tools support merge/aggregation, example: http://www.eclemma.org/jacoco/trunk/doc/merge-mojo.html
To send coverage in the enterprise version you should:
export CODACY_API_BASE_URL=<Codacy_instance_URL>:16006
Due to a limitation in Java 6, the plugin is unable to establish a connection to codacy.com. You can run this script after the execution to upload the generated report to Codacy.
If you are having any issues with your installation, you can also build the coverage reporter from source.
NOTE: To make sure you are using the version that you are building, you can remove your previously installed version:
[sudo] jpm remove codacy-coverage-reporter
-
Clone our repository https://github.com/codacy/codacy-coverage-reporter
-
Run the command
sbt assembly
. This will produce a .jar that you can run in thecodacy-coverage-reporter/target/codacy-coverage-reporter-assembly-<version>.jar
-
In the project you want to send the coverage, use the jar. Example:
<path>/java-project$ java -cp ../codacy-coverage-reporter/target/codacy-coverage-reporter-assembly-<version>.jar com.codacy.CodacyCoverageReporter -l Java -r jacoco.xml
A big shout-out to tompahoward, you can create a gradle task as suggested in https://github.com/mountain-pass/hyperstate/commit/857ca93e1c8484c14a5e2da9f0434d3daf3328ce
task uploadCoverageToCodacy(type: JavaExec, dependsOn : jacocoTestReport) {
main = "com.codacy.CodacyCoverageReporter"
classpath = configurations.codacy
args = [
"-l",
"Java",
"-r",
"${buildDir}/test-results/jacoco/${archivesBaseName}.xml"
]
}
task (codacyDepsize) << {
def size = 0;
configurations.codacy.collect { it.length() / (1024 * 1024) }.each { size += it }
println "Total dependencies size: ${Math.round(size * 100) / 100} Mb"
configurations
.codacy
.sort { -it.length() }
.each { println "${it.name} : ${Math.round(it.length() / (1024) * 100) / 100} kb" }
}
task (codacyLocs) << {
configurations.codacy.each {
String jarName = it
println jarName
}
}
Gradle task by Mr_ramych. Made up from solution above.
configurations { codacy }
repositories {
maven { url "https://jitpack.io" }
maven { url "http://dl.bintray.com/typesafe/maven-releases" }
}
dependencies {
codacy 'com.github.codacy:codacy-coverage-reporter:-SNAPSHOT'
}
task sendCoverageToCodacy(type: JavaExec, dependsOn: jacocoTestReport) {
main = "com.codacy.CodacyCoverageReporter"
classpath = configurations.codacy
args = [
"-l",
"Java",
"-r",
"${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
]
}
Thanks to the amazing job of halkeye you can now send your coverage to Codacy using a maven plugin!
Just follow the instructions on his repository.
If you want to use codacy with Travis CI and report coverage generated from your tests run in Travis, update your .travis.yml to include the following blocks:
before_install:
- sudo apt-get install jq
- wget -O ~/codacy-coverage-reporter-assembly-latest.jar $(curl https://api.github.com/repos/codacy/codacy-coverage-reporter/releases/latest | jq -r .assets[0].browser_download_url)
after_success:
- java -cp ~/codacy-coverage-reporter-assembly-latest.jar com.codacy.CodacyCoverageReporter -l Java -r build/reports/jacoco/test/jacocoTestReport.xml
Make sure you have set CODACY_PROJECT_TOKEN
as an environment variable in your travis job!
Error when running the command, then you'll probably have codacy-coverage-reporter 1.0.3 installed. Make sure you install version 1.0.4, that fixes that error.
Example (issue: #11) :
codacy-coverage-reporter -l Java -r PATH_TO_COVERAGE/coverage.xml
2015-11-20 04:06:58,887 [info] com.codacy Parsing coverage data...
2015-11-20 04:06:59,506 [info] com.codacy Uploading coverage data...
2015-11-20 04:07:00,639 [error] com.codacy Failed to upload report: Not Found
Even after doing all of the above troubleshooting steps in case you still encounter the same error
2015-11-20 04:07:00,639 [error] com.codacy Failed to upload report: Not Found
Please try running the command with a --prefix option with path to your code as shown below , it helps to locate the files for which code coverage is desired
codacy-coverage-reporter -l Java -r PATH_TO_COVERAGE/coverage.xml --prefix PATH_TO_THE_DIRECTORY
Example
codacy-coverage-reporter -l Java -r api/target/site/jacoco/jacoco.xml --prefix api/src/main/java/
Follow the steps in http://jpm4j.org/#!/md/macos
Codacy is an Automated Code Review Tool that monitors your technical debt, helps you improve your code quality, teaches best practices to your developers, and helps you save time in Code Reviews.
- Identify new Static Analysis issues
- Commit and Pull Request Analysis with GitHub, BitBucket/Stash, GitLab (and also direct git repositories)
- Auto-comments on Commits and Pull Requests
- Integrations with Slack, HipChat, Jira, YouTrack
- Track issues Code Style, Security, Error Proneness, Performance, Unused Code and other categories
Codacy also helps keep track of Code Coverage, Code Duplication, and Code Complexity.
Codacy supports PHP, Python, Ruby, Java, JavaScript, and Scala, among others.
Codacy is free for Open Source projects.