You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot 1.5升级2.+遇Gradle注解处理器警告问题求助

解决Spring Boot 2.x升级中Gradle注解处理器类路径警告问题

你在升级Spring Boot版本时碰到的注解处理器类路径警告,大概率是依赖配置方式不对,再加上propdeps插件的干扰导致的。我来帮你梳理下具体的解决思路和修改方案:

问题根源

Spring Boot 2.x对注解处理器的依赖配置有明确规范:注解处理器类必须用annotationProcessor配置,compileOnly仅用于让IDE识别注解类(不参与编译打包逻辑),而implementation完全不适合注解处理器场景——因为注解处理器只在编译阶段生效,不需要被包含在运行时类路径中。另外你引入的propdeps系列插件会干扰Gradle对注解处理器依赖的识别逻辑,这也是警告一直无法消除的核心原因之一。

具体修改步骤

  1. 移除propdeps相关插件:Spring Boot 2.x自带的dependency-management插件已经能处理依赖作用域管理,propdeps属于冗余配置,会打乱注解处理器的配置生效逻辑。
  2. 正确配置注解处理器依赖:同时保留compileOnly(给IDE识别用)和annotationProcessor(给编译阶段用),两者缺一不可。
  3. 清理冗余配置:去掉那些注释掉的无效尝试,保持配置整洁易维护。

修改后的完整Gradle配置

buildscript {
    ext {
        springBootVersion = '2.0.2.RELEASE'
    }
    repositories {
        mavenCentral()
        maven { url 'http://repo.spring.io/plugins-release' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'
apply plugin: 'maven'
//apply plugin: 'war'

//processResources {
//    expand(project.properties)
//}

version = '0.0.1'
sourceCompatibility = 1.8

//war {
//    baseName = 'goolge-places'
//    // version = '0.5.0'
//}

bootJar {
    launchScript()
}

//springBoot {
//    executable = true
//}

repositories {
    jcenter()
    mavenCentral()
    /* // Private Oracle Maven repo
    maven {
        credentials {
            username '****'
            password '****'
        }
        name "maven.oracle.com"
        url('https://maven.oracle.com/')
    }
    */
}

// Change the output directory for the main source set back to the old path
// Gradle 4.0 change Deprecated. gone in version 5.0
//sourceSets.main.output.classesDir = new File(buildDir, "classes/main")

dependencies {
    runtime('org.springframework.boot:spring-boot-devtools')
    compile 'com.google.maps:google-maps-services:0.1.20'
    compile('org.springframework.boot:spring-boot-starter-actuator')
    // compile('org.springframework.boot:spring-boot-actuator-docs')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.data:spring-data-rest-hal-browser')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    //compileOnly("com.h2database:h2")
    compile 'org.springframework.data:spring-data-oracle:1.2.1.RELEASE'
    // compile 'org.springframework.metrics:spring-metrics:latest.release'
    // compile 'io.prometheus:simpleclient_common:latest.release'

    // Libs folder for oracle JBDC
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(dir: 'lib', include: ['*.jar'])

    // https://mvnrepository.com/artifact/joda-time/joda-time
    compile group: 'joda-time', name: 'joda-time', version: '2.9.9'

    // 正确配置Spring Boot配置处理器
    compileOnly "org.springframework.boot:spring-boot-configuration-processor:${springBootVersion}"
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor:${springBootVersion}"

    // 正确配置Lombok
    compileOnly('org.projectlombok:lombok')
    annotationProcessor('org.projectlombok:lombok')

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
    // https://mvnrepository.com/artifact/org.mockito/mockito-core
    testCompile "org.mockito:mockito-core:2.+"

    /* // Oracle DB Driver is in private Oracle Maven repo
    // I've created the maven settings.xml with hashed password
    // but it, "Gradle" dose not read settings.xml so I have set the
    //credentials above in the repositories section but now I get other errors
    // this fixed the auth errors but now I'm still getting a error
    // saying that the files do not exists
    compile('com.oracle.jdbc:ojdbc8:12.2.0.1')
    */
    // compile('org.jooq:jool:0.9.12')
}

compileJava.dependsOn(processResources)

task removeOut(type: Delete) {
    //noinspection GroovyAssignabilityCheck
    delete 'out'
}

验证方法

执行以下命令清理并重新编译项目,检查警告是否消失:

./gradlew clean compileJava

这样配置后,Gradle会正确识别注解处理器的作用域,不会再把它们放到运行时类路径中,警告自然就消除了。

内容的提问来源于stack exchange,提问作者Apps 4 U

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:23:53