Ali
Ali

Reputation: 267107

Gradle compileOnly doesn't make Lombok available in tests

My build.gradle file adds Lombok as a dependency, via the following:

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'

This works fine for all 'normal' application code - but in tests, if I try to use any Lombok annotations, I get errors that its not available.

Why is that? I would've thought that the test compile classpath should extend from the regular classpath - as it needs to do extensive testing of all regular code?

Changing

compileOnly 'org.projectlombok:lombok'

to

implementation 'org.projectlombok:lombok'

makes the error go away - but I want to understand why the test classpath doesn't extend / inherit the regular classpath?

Upvotes: 2

Views: 1578

Answers (1)

Answer is: compileOnly don't work at test time. That's why you need add dependency at test time.

So here link to documentation lombok https://projectlombok.org/setup/gradle

This is copy of text: Gradle without a plugin If you don't want to use the plugin, gradle has the built-in compileOnly scope, which can be used to tell gradle to add lombok only during compilation. Your build.gradle will look like:

repositories {
    mavenCentral()
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.24'
    annotationProcessor 'org.projectlombok:lombok:1.18.24'
    
    testCompileOnly 'org.projectlombok:lombok:1.18.24'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
}

This is the link to gradle documentation. How tests are forming. https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph

Upvotes: 4

Related Questions