I am trying to store the index.html file from the JaCoCo report by converting into PDF format in the CI/CD pipeline. What is the right command to convert from HTML to PDF in GitLab?
Jacoco does not support pdf natively.
It does generate an html report though.
So you could use a cli tool to convert html to pdf.
For example https://wkhtmltopdf.org/ or pandoc.
You would need to install it in your job/use a docker image that contains it already.
Example:
Test:
image: maven:3.8.3-jdk-11-slim
stage: test
script:
- mvn $MAVEN_CLI_OPTS clean org.jacoco:jacoco-maven-plugin:prepare-agent test
- mvn $MAVEN_CLI_OPTS jacoco:report
artifacts:
when: always
paths:
- target/site/jacoco/jacoco.xml
jacoco2pdf:
image: riftbit/goracle:alpine-19.3
stage: deploy
script:
- cd target/site/jacoco
- wkhtmltopdf index.html jacoco.pdf
artifacts:
when: always
paths:
- target/site/jacoco/jacoco.pdf
Note that the used docker image is just an example. You should not use it in production unless you verify its contents first.
When I run the build in TeamCity with command:
;set test in assembly := {};clean;compile;flywayClean;flywayMigrate;assembly
I get merge errors:
TraceEvent(Error, java.lang.RuntimeException: deduplicate: different
file contents found in the following:
/home/teamcity/tcagent/system/sbt_ivy/cache/org.joda/joda-convert/jars/joda-convert-2.2.1.jar:module-info.class
/home/teamcity/tcagent/system/sbt_ivy/cache/com.fasterxml.jackson.core/jackson-databind/bundles/jackson-databind-2.11.0.jar:module-info.class
/home/teamcity/tcagent/system/sbt_ivy/cache/com.fasterxml.jackson.core/jackson-annotations/bundles/jackson-annotations-2.11.0.jar:module-info.class
/home/teamcity/tcagent/system/sbt_ivy/cache/com.fasterxml.jackson.core/jackson-core/bundles/jackson-core-2.11.0.jar:module-info.class
However, when I do this locally in IDEA, I don't get such errors.
Probably Idea runs sbt without merging. How can I fix this to get the same errors in Idea?
I created sbt task in IDEA with command:
;set test in assembly := {};clean;compile;assembly
and it worked.
I have a java-maven application and I am trying to display my junit results on gitlab as shown in the gitlab help here: https://docs.gitlab.com/ee/ci/junit_test_reports.html#viewing-junit-test-reports-on-gitlab
I added the maven-surefire-plugin in the pom build and the maven-surefire-report-plugin in the pom reporting section. I checked that it works because the surefire-report.html is correctly created in my local target repository, with the test results.
Then i configured my gitlabci.yaml by adding the last lines:
image: "maven"
before_script:
- cd gosecuri
stages:
- build
- test
- run
job_build:
stage: build
script:
- mvn compile
job_test:
stage: test
script:
- mvn package
artifacts:
paths:
- gosecuri/target/*.war
expire_in: 1 week
reports:
junit:
- target/surefire-reports/TEST-*.xml
The pipeline succeeds. In gitlab i have the message "There are no tests to show." in the job tests tab, and in the console I have this warning: target/surefire-reports/TEST-*.xml: no matching files
What am I missing? Thanks
PS: i'm running in gitlab.com Saas free plan
Right after docs you mentioned there is Enabling the feature paragraph.
This feature comes with the :junit_pipeline_view feature flag disabled
by default.
Looks like feature is disabled on public gitlab.com. If you launch your instance of GitLab you can enable it.
Update: Path to reports was incorrect: target/... should be gosecuri/target/...
Is it possible to download gradle dependencies using only build.gradle file?
What I am trying to accomplish is the following:
I have a set of unit tests and I want to execute them (as part of CI process) inside a docker container. Initially, I used the openjdk:8-jdk image from docker hub as base image for my tests. So, docker compose file contains the following:
version: '3.2'
services:
unit:
image: openjdk:8-jdk
volumes:
- ..:/usr/test
working_dir: /usr/test
command: sh -c "exec ./gradlew junitPlatformTest -Punit -p moduleA/"
Whole project is mounted on /usr/test directory inside the container. When the container starts, it executes the junitPlatformTest task against moduleA. The problem with openjdk:8-jdk image is that gradle and its dependencies are downloaded every time I run the unit service.
To solve this, I decided to create a new image which would have gradle and my project dependencies already downloaded. The Dockerfile is the following:
FROM openjdk:8-jdk
COPY . /usr/test
WORKDIR /usr/test
RUN apt-get -y install wget unzip
RUN wget https://services.gradle.org/distributions/gradle-4.1-bin.zip
RUN mkdir /opt/gradle
RUN unzip -d /opt/gradle gradle-4.1-bin.zip
RUN /opt/gradle/gradle-4.1/bin/gradle dependencies
The build.gradle file is located in same folder as Dockerfile so the command COPY . /usr/test copies it in the working directory.
However, executing the gradle dependencies command does not download the libraries. After built the image, ran a container and entered into it (with docker exec), it seems that ~/.gradle/caches/modules-2/files-2.1/ directory contains only pom files, not jars.
I'm not use if gradle dependencies is the correct command. Any suggestions?
EDIT - Gradle file
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
jcenter()
}
ext.versions = new Properties()
file("./versions.properties").withInputStream {
stream -> ext.versions.load(stream)
}
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:$versions.junitJupiterVersion")
testCompile("org.junit.jupiter:junit-jupiter-engine:$versions.junitJupiterVersion")
testCompile("org.junit.jupiter:junit-jupiter-params:$versions.junitJupiterVersion")
testCompile("org.mockito:mockito-core:'$versions.mockitoCore")
testCompile("org.junit.platform:junit-platform-launcher:1.0.0-RC3")
compile("com.google.inject:guice:$versions.guice")
....
}
Add below to your gradle file
task download (type: Exec) {
configurations.testCompile.files
commandLine 'echo', 'Downloaded all dependencies'
}
Also change
RUN /opt/gradle/gradle-4.1/bin/gradle dependencies
to
RUN /opt/gradle/gradle-4.1/bin/gradle
This will cache all dependencies to ~/.gradle when you run the gradle command. It will download the jars also and not just poms. And if you want to cache further you can even use a named volume for gradle folder
version: '3.2'
services:
unit:
image: openjdk:8-jdk
volumes:
- ..:/usr/test
- gradlecache:/root/.gradle/
working_dir: /usr/test
command: sh -c "exec ./gradlew junitPlatformTest -Punit -p moduleA/"
volumes:
gradlecache: {}
Your idea is right but the approach is wrong. First of all, there is no need to download and install Gradle manually, that's exactly what the Gradle Wrapper does for you. Second of all, there is no need to hack Gradle to force download dependencies - it doesn't make any sense. Just check out/clone your test project from inside the container and run your tests with ./gradlew clean test. There is no need for a local /usr/test directory, which is flies in the face of CI, because it uses a relative location, and hence only works when you've laid out files in an exact manner.
Edit:
If you don't want to download Gradle or the dependencies for every build, you can start the container by volume mapping the$HOME/.m2 directory to the host, so that dependencies downloaded once stay in the Maven local cache. To avoid downloading Gradle, you can build your own Docker image with Gradle in it.
I want to execute gradle build without executing the unit tests. I tried:
$ gradle -Dskip.tests build
That doesn't seem to do anything. Is there some other command I could use?
You should use the -x command line argument which excludes any task.
Try:
gradle build -x test
Update:
The link in Peter's comment changed. Here is the diagram from the Gradle user's guide
Try:
gradle assemble
To list all available tasks for your project, try:
gradle tasks
UPDATE:
This may not seem the most correct answer at first, but read carefully gradle tasks output or docs.
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
You can add the following lines to build.gradle, **/* excludes all the tests.
test {
exclude '**/*'
}
The accepted answer is the correct one.
OTOH, the way I previously solved this was to add the following to all projects:
test.onlyIf { ! Boolean.getBoolean('skip.tests') }
Run the build with -Dskip.tests=true and all test tasks will be skipped.
Every action in gradle is a task, and so is test. And to exclude a task from gradle run, you can use the option --exclude-task or it's shorthand -x followed by the task name which needs to be excluded. Example:
gradle build -x test
The -x option should be repeated for all the tasks that needs to be excluded.
If you have different tasks for different type of tests in your build.gradle file, then you need to skip all those tasks that executes test. Say you have a task test which executes unit-tests and a task testFunctional which executes functional-tests. In this case, you can exclude all tests like below:
gradle build -x test -x testFunctional
Using -x test skip test execution but this also exclude test code compilation.
gradle build -x test
In our case, we have a CI/CD process where one goal is compilation and next goal is testing (Build -> Test).
So, for our first Build goal we wanted to ensure that the whole project compiles well. For this we have used:
./gradlew build testClasses -x test
On the next goal we simply execute tests:
./gradlew test
You can exclude tasks
gradle build --exclude-task test
https://docs.gradle.org/current/userguide/command_line_interface.html#sec:command_line_executing_tasks
the different way to disable test tasks in the project is:
tasks.withType(Test) {enabled = false}
this behavior needed sometimes if you want to disable tests in one of a project(or the group of projects).
This way working for the all kind of test task, not just a java 'tests'. Also, this way is safe. Here's what I mean
let's say: you have a set of projects in different languages:
if we try to add this kind of record in main build.gradle:
subprojects{
.......
tests.enabled=false
.......
}
we will fail in a project when if we have no task called tests
Reference
To exclude any task from gradle use -x command-line option. See the below example
task compile << {
println 'task compile'
}
task compileTest(dependsOn: compile) << {
println 'compile test'
}
task runningTest(dependsOn: compileTest) << {
println 'running test'
}
task dist(dependsOn:[runningTest, compileTest, compile]) << {
println 'running distribution job'
}
Output of: gradle -q dist -x runningTest
task compile
compile test
running distribution job
Hope this would give you the basic
In The Java Plugin:
$ gradle tasks
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
testClasses - Assembles test classes.
Verification tasks
------------------
test - Runs the unit tests.
Gradle build without test you have two options:
$ gradle assemble
$ gradle build -x test
but if you want compile test:
$ gradle assemble testClasses
$ gradle testClasses
Please try this:
gradlew -DskipTests=true build