【问题标题】:How to remove a dependency in Gradle depending on the Spring Boot runtime environment?如何根据 Spring Boot 运行时环境删除 Gradle 中的依赖项?
【发布时间】:2014-12-12 11:32:50
【问题描述】:

不确定这是 Gradle 问题还是 Spring Boot 问题,但这里是……

我在 Spring Boot 应用程序中使用 Spring 安全性和 LDAP。

我的 build.gradle 中有以下依赖项:

compile 'org.springframework.security:spring-security-ldap:3.2.4.RELEASE'
compile 'org.apache.directory.server:apacheds-server-jndi:1.5.5'

其中的第二个提供了仅在开发期间才需要的嵌入式 LDAP 服务器。

我已经建立了一个 SB @Profile 并将一个 LDIF 文件配置/加载到具有 @Profile('development') 注释的类中的嵌入式服务器中。

问题是:不处于dev模式时如何去除第二个依赖?

我在我的 config/application.yml 文件中建立了 spring.profiles.active 属性,因此:

spring:
  profiles:
    active: development

我可以引用 spring.profiles.active 以便以某种方式排除不需要的依赖项吗?

【问题讨论】:

  • 不在开发模式下是什么意思?其他模式是如何准备的?战争?命令行?是什么决定的?

标签: gradle spring-boot


【解决方案1】:

为了后代,我最终做了什么......

在我的 build.grade 文件的顶部:

def readActiveProfile() {
    final config = new org.yaml.snakeyaml.Yaml().loadAll(new File('config/application.yml').newReader())
    final defaultPart = config?.take(1)
    defaultPart?.spring?.profiles?.active
}

final activeProfile = readActiveProfile() ?: ['development']

这会读取我保存外部化设置的配置文件(其中之一是定义活动配置文件的设置)。

然后,在依赖项部分:

compile 'org.springframework.security:spring-security-ldap:3.2.4.RELEASE'
if( ! ('production' in activeProfile))
    compile 'org.apache.directory.server:apacheds-server-jndi:1.5.5'

这对我的目的来说足够好,但感觉不太对;我假设会有一种更惯用的“Gradle 方式”来做到这一点。

【讨论】:

  • 忘了说,我在 buildscript 的依赖项中添加了classpath 'org.yaml:snakeyaml:1.14'
  • 请您向我们展示您的全部/大部分build.gradle 文件好吗?d
猜你喜欢
  • 1970-01-01
  • 2014-11-26
  • 2022-01-17
  • 2017-07-05
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
相关资源
最近更新 更多