Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use SLF4J annotation in maven compilation

I'm setting my first steps into a SpringBoot maven application. It has a root pom file, a module with a pom file and a Java class.

This is the root pom.xml file of my project.

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myapp</groupId>
    <artifactId>dummy</artifactId>
    <version>${revision}</version>
    <packaging>pom</packaging>

    <properties>
        <!-- Project revision -->
        <revision>local-SNAPSHOT</revision>

        <!-- Dependency versions -->
        <lombok-version>1.18.38</lombok-version>
        <springboot-version>3.3.10</springboot-version>
        <springjdbc-version>6.2.9</springjdbc-version>
    </properties>

    <modules>
        <module>dummy-service</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                    <proc>none</proc>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>license-maven-plugin</artifactId>
                    <version>1.14</version>
                    <configuration>
                        <includedScopes>compile</includedScopes>
                        <excludedGroups>com.myapp.*</excludedGroups>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <source>21</source>
                        <target>21</target>
                        <encoding>UTF-8</encoding>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok-version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

This root pom has 1 module, the dummy-service. This is the pom file of my dummy-service module.

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.trax</groupId>
    <artifactId>scheduler</artifactId>
    <version>${revision}</version>
  </parent>

  <groupId>com.trax.scheduler</groupId>
  <artifactId>scheduler-service</artifactId>
  <name>scheduler-service</name>

  <dependencies>
    <!-- SpringBoot -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>${springboot-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <version>${springboot-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>${springboot-version}</version>
    </dependency>

    <!-- spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${springjdbc-version}</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok-version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

Inside this dummy-service module I have a class DataLoaderConfig

package com.myapp.dummy.service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DataLoaderConfig {


  public void execute() {
    log.info("Executing...");
  }
}

However this runs successfully in IntelliJ, I have an issue during maven compilation:

cannot find symbol [ERROR] symbol: variable log

I verified that the Maven pom has the lombok dependency, also that during build the annotation processor is executed, but still the issue occurs. I guess I'm doing something wrong, but the question is what exactly went wrong?

like image 788
jonasaud Avatar asked Aug 31 '25 03:08

jonasaud


2 Answers

Looks like <proc>none</proc> within maven-compiler-plugin's configuration prevents annotation processing. I was able to compile your project successfully after removing it.

From maven-compiler-plugin documentation:

<proc> Sets whether annotation processing is performed or not. Only applies to JDK 1.6+ If not set, both compilation and annotation processing are performed at the same time.

Allowed values are:

  • none - no annotation processing is performed.

  • only - only annotation processing is done, no compilation.

  • full - annotation processing and compilation.

full is the default. Starting with JDK 21, this option must be set explicitly. [...]

like image 149
HSon Avatar answered Sep 02 '25 16:09

HSon


I recommend to use the spring-boot-starter-parent. Simplify your root pom:

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.1</version>
    <relativePath/> </parent>

<groupId>com.myapp</groupId>
<artifactId>dummy</artifactId>
<version>local-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>dummy-service</module>
</modules>

<properties>
    <java.version>21</java.version>
</properties>

In this case you don't need the manual dependency versions and the entire <build> section because the parent handles it.

And your dummy-service/pom.xml:

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.myapp</groupId>
    <artifactId>dummy</artifactId>
    <version>local-SNAPSHOT</version>
</parent>

<artifactId>dummy-service</artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

It doesn't need redundant version tags from your dependencies in this case. They're inherited from the new root pom.

This way'll be aligned with standard Spring Boot practices, making it cleaner and easier to maintain.

like image 30
kapandron Avatar answered Sep 02 '25 15:09

kapandron