英文:
@Slf4j Lombok annotation not working with Java + Gradle
问题
I'm fairly new to Java and trying to implement a logger with @Slf4j annotation provided by lombok. From my understanding it should be fairly simple, just provide the annotation to the class and it will create the method for that class but it seems to not work.
My code looks like this:
package MyApp;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MyApp {
public static void main(String[] args) {
log.info("hello"); // IDEA gives this error: Cannot resolve method 'info(String)'
}
}
When trying to run the code i get this:
error: package org.slf4j does not exist
@Slf4j
^
My build.gradle looks like this:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.logging.log4j:log4j:2.20.0'
compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
I have already tried enabling annotations.
Also, I saw this implementation working in another project and i tryed simply copying the dependencies, but it still didn't work.
英文:
I'm fairly new to Java and trying to implement a logger with @Slf4j annotation provided by lombok. From my understanding it should be fairly simple, just provide the annotation to the class and it will create the method for that class but it seems to not work.
My code looks like this:
package MyApp;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MyApp {
public static void main(String[] args) {
log.info("hello"); IDEA gives this error: Cannot resolve method 'info(String)'
}
}
When trying to run the code i get this:
error: package org.slf4j does not exist
@Slf4j
^
My build.gradle looks like this:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.logging.log4j:log4j:2.20.0'
compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
I have already tried enabling annotations.
Also, I saw this implementation working in another project and i tryed simply copying the dependencies, but it still didn't work.
答案1
得分: 2
@Slf4j注解用于另一个日志库。您有Log4j库的依赖关系。然后,您应该使用@Log4j Lombok注解。
另一方面,如果您想使用Slf4j,请将依赖项更改为org.slf4j。
英文:
The @Slf4j annotation is for another logging library. You have a dependency for Log4j library. Then you should use @Log4j Lombok annotation.
On the other hand if you want to use Slf4j then change the dependency to org.slf4j
答案2
得分: 0
如果您正在使用IntelliJ作为您的集成开发环境(IDE),请在您的gradle文件中也包含lombok插件:
https://plugins.gradle.org/plugin/io.freefair.lombok
如果使用Eclipse,您需要手动运行lombok的jar文件以解决此问题。
详细说明在此处可用链接。
英文:
If you are using IntelliJ as your IDE, please include lombok plugin also in your gradle file:
https://plugins.gradle.org/plugin/io.freefair.lombok
In case of Eclipse, you need to manually run the lombok jar file to resolve this issue.
Detailed description is available here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论