【问题标题】:Compile dependencies dynamically based on spring.profiles.active基于spring.profiles.active动态编译依赖
【发布时间】:2018-05-30 10:07:32
【问题描述】:

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

在部署到 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。帮助!

【问题讨论】:

  • spring.profiles.active在哪里定义?
  • @ToYonos,它在 src_server/main/resources/application.yml 中。
  • 所以getProperty "spring.profiles.active" 不是真的吗?这不是读取application.yml 的方法?
  • 上面的示例代码出现错误。我已编辑问题以包含错误消息。
  • 您正在创建 WAR 文件吗?请注意,Gradle 对 application.yml 文件一无所知。另外,您确定应该在哪里定义 spring.profiles.active 吗?据我记得,application.yml 允许您根据spring.profiles.active 的值指定不同的值。

标签: java gradle build.gradle


【解决方案1】:

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

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

所以我只需要这样做:

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 当我必须在本地运行时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多