Home > Software engineering >  Gradle not seeing "log" variable definition?
Gradle not seeing "log" variable definition?

Time:01-19

Gradle v7.3.3

I'm trying to use the The Java Platform Plugin, and I have this so far in my platform build.gradle file

artifactId = "my-java-platform"
group = "com.mycompany.platform"
version = "1.0.0"

dependencies {
  constraints {
    ...
    api "org.slf4j:slf4j-log4j12:1.7.9"
    api "org.projectlombok:lombok:1.16.18"
    ...
  }
}

I did a ./gradlew publishToMavenLocal and see the resulting pom.xml file with those 2 dependencies.

Then in my application's build.gradle file I have

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
  implementation platform(group: "com.company.platform", name: "my-java-platform", version: "1.0.0")
  annotationProcessor platform(group: "com.company.platform", name: "my-java-platform", version: "1.0.0")

  compileOnly group: "org.slf4j", name: "slf4j-log4j12"
  compileOnly group: "org.projectlombok", name: "lombok"
  ...
}

One of my applications source code has

package com.mycompany.common

import java.util.TimeZone;

import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ObjectMapperConfiguration {
    private static ObjectMapper objectMapper;

    /**
     * Static only
     */
    private ObjectMapperConfiguration() {}

    /**
     * Work with Spring to configure the ObjectMapper
     */
    public static ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        objectMapper = builder.createXmlMapper(false).build();
        configureObjectMapper(objectMapper);
        log.info("The ObjectMapperConfiguration has run");
        return objectMapper;
    }
    ...
}

But I get

$ ./gradlew clean build

> Task :compileJava FAILED
/Users/.../src/main/java/com/company/common/ObjectMapperConfiguration.java:39: error: cannot find symbol
        log.info("The ObjectMapperConfiguration has run");
        ^
  symbol:   variable log
  location: class com.company.common.ObjectMapperConfiguration

I understand that the log variable is defined in the @Slf4j annotation? If so why am I getting the error? Thanks!

CodePudding user response:

The Lombok magic is implemented via an annotation processor.

See here for the recommended Lombok Gradle config

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

CodePudding user response:

The application's build.gradle file, the one that uses the platform, should look like this

dependencies {
    implementation platform(group: "com.mycompany.platform", name: "my-java-platform", version: "1.0.0")
    annotationProcessor platform(group: "com.company.platform", name: "my-java-platform", version: "1.0.0")

    annotationProcessor group: "org.projectlombok", name: "lombok"
    ...
}
  •  Tags:  
  • Related