关于gradle创建Springboot项目无法读取application.xml配置文件

先贴以下我的build.gradle文件内容

// gradle 自身需求资源库 放头部
buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public' }// 加载其他Maven仓库
        mavenCentral()
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:2.1.1.RELEASE')// 加载插件,用到里面的函数方法
    }
}



apply from: "$rootDir/dependencies.gradle"
subprojects {

    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'idea'
// 使用spring boot 框架
    apply plugin: 'org.springframework.boot'
// 使用spring boot的自动依赖管理
    apply plugin: 'io.spring.dependency-management'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    // 执行项目中所使用的的资源仓库
    repositories {
        mavenLocal()
        maven { url 'https://maven.aliyun.com/repository/public' }
        mavenCentral()
    }


    dependencies {

        // 添加 spring-boot-starter-web 的依赖 必须 排除了security 根据自身需求
        implementation('org.springframework.boot:spring-boot-starter-web') {
            exclude group: 'org.springframework.security', module: 'spring-security-config'
        }
        // 添加 spring-boot-starter-test 该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依赖
       // testImplementation 'org.springframework.boot:spring-boot-starter-test'

        implementation libs.common
        implementation libs.test
    }
    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
}

重点要在dependneceise中贴入

implementation('org.springframework.boot:spring-boot-starter-web') {
    exclude group: 'org.springframework.security', module: 'spring-security-config'
}

其次是要声明我们使用

apply plugin: 'idea'
// 使用spring boot 框架
    apply plugin: 'org.springframework.boot'
// 使用spring boot的自动依赖管理
    apply plugin: 'io.spring.dependency-management'

这3个依赖,这样我的gradle创建的springboot项目就可以读取到配置文件了。(我是先删除了之前创建的properties文件,在创建了一个yml文件。然后就成功了。)

猜你喜欢

转载自blog.csdn.net/qq_46586512/article/details/126044534
今日推荐