java - 基于spring.profiles.active动态编译依赖

标签 java gradle build.gradle

有人可以指导我从哪里开始这项任务吗?

部署到 jboss 时,我只需排除 spring-boot-starter-tomcat 即可。

我想它会看起来像:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web"){
        if(getProperty "spring.profiles.active" == "qat")
            exclude module: "spring-boot-starter-tomcat"
    }
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

使用上面的示例,我收到错误:

Could not get unknown property 'spring.profiles.active' for DefaultExternalModuleDependency{group='org.springframework.boot', name='spring-boot-starter-web', version='null', configuration='default'} of type org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency.

也许我可以创建一个自定义任务来设置该任务的spring.profiles.active。帮助!

最佳答案

正如 Peter Ledbrook 提到的,gradle 在编译时无法访问 spring-boot 的 application.yml。并且依赖项在gradle生命周期的早期运行,在解决依赖项之前永远不会调用任务。

即使尝试依赖解析策略也是徒劳的。

所以我只需要这样做:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        if(System.getProperty("spring.profiles.active") == "qat"){
          exclude module: "spring-boot-starter-tomcat"
        }
    }
    compile("org.springframework.boot:spring-boot-starter-security")
    if(System.getProperty("spring.profiles.active") == "qat"){
        providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
    }
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

然后我将在部署到 jboss 时输入 gradle build -Dspring-profiles-active=qat。和 gradle bootRun -Dspring-profiles-active=dev 当我必须在本地运行时。

关于java - 基于spring.profiles.active动态编译依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50601943/

相关文章:

java - 将 TIF/TIFF 转换为 JPG : Bad endianness tag

java - RestTemplate POST 请求 RequestParams 和 RequestBody

java - Java 中比较器接口(interface)的构造函数

gradle - 与eachFile的Gradle Copy任务if子句不一致,我在做什么错?

java - 将函数应用于按流分组的嵌套列表

Android Studio 无法编译

java - 使用 JDK8 构建时出现 ParseException

git - gradle找不到git.exe

android - 重构程序包名称和重命名applicationID仍然使gradle失败

gradle - Gradle IntegrationTest目标失败,退出代码为137