项目构建总是报错:
错误提示1:java:重复类
或
错误提示2:Internal error in the mapping processor: java.lang.RuntimeException: javax.annotation.processing.FilerException: Attempt to recreate a file
排查发现build/generated/sources/annotationProcessor路径下会生成2个同名类导致,
IDEA和Gradle会各自构建生成文件造成冲突。
(起初以为是mapstruct与lombok冲突导致,后附mapstruct与lombok的引入配置)
解决方案:
IDEA-》设置-》构建、执行、部署-》构建工具-》Gradle
使用此工具构建/运行:IntelliJ IDEA
使用此工具运行测试:IntelliJ IDEA
Clean后再重新构建即可
环境:
Jdk17+Spring boot 3.3.13+Gradle
build.gradle文件内容:
plugins {id 'java'id 'org.springframework.boot' version '3.3.13'id 'io.spring.dependency-management' version '1.1.7'
}group = 'xxxx'
version = '0.0.1-SNAPSHOT'java {toolchain {languageVersion = JavaLanguageVersion.of(17)}
}tasks.withType(JavaCompile).configureEach {options.encoding = "UTF-8"
}configurations {compileOnly {extendsFrom annotationProcessor}
}repositories {maven {url 'https://maven.aliyun.com/repository/public'}mavenCentral()
}dependencies {// Spring Boot 核心依赖implementation 'org.springframework.boot:spring-boot-starter-web'implementation 'org.springframework.boot:spring-boot-starter-aop'annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'compileOnly 'org.projectlombok:lombok'annotationProcessor 'org.projectlombok:lombok'//略...implementation 'org.mapstruct:mapstruct:1.5.5.Final' annotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'//略...
}tasks.named('test') {useJUnitPlatform()
}