简体   繁体   English

使用Spring Boot Profile和Gradle管理依赖项

[英]Managing dependencies with spring boot profiles and Gradle

I created a java project that will use spring boot and Gradle. 我创建了一个使用Spring Boot和Gradle的Java项目。 I would like to configure profiles, for the different environment (development on my local machine, systemtest for integration test on server farm machine etc). 我想针对不同的环境(在本地计算机上进行开发,在服务器场计算机上进行集成测试的systemtest等)配置配置文件。 I would use h2 in memory database for development environment and SqlServer for systemtest environment. 我将在内存数据库中将h2用于开发环境,将SqlServer用于系统测试环境。 In build.gradle I defined the following dependencies 在build.gradle中,我定义了以下依赖项

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web-services")
    compile('org.springframework.boot:spring-boot-starter-actuator')
    runtime('com.h2database:h2:1.4.195')
    runtime('com.microsoft.sqlserver:mssql-jdbc')
}

I created a application.yml file, application-development.yml and application-systemtest.yml where I would put common properties and environment specific properties. 我创建了一个application.yml文件,application-development.yml和application-systemtest.yml,在其中放置通用属性和特定于环境的属性。 The file application-systemtest.yml defines the connecction parameters for sql server 文件application-systemtest.yml定义了sql server的连接参数

spring:
  datasource:
    url: jdbc:sqlserver://<host>,1433;databaseName=MYDB
    username: myuser
    password: mypass
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
  jpa:
    show-sql: true
    hibernate:
      dialect: org.hibernate.dialect.SQLServer2012Dialect

I would also create an uber-jar and select the profile as a launch parameter, ie 我还将创建一个uber-jar并选择配置文件作为启动参数,即

java -Dspring.profiles.active=systemtest -jar <my uber jar>

The development profiles starts fine and I am running on h2 in memory database. 开发配置文件开始正常,我在内存数据库的h2上运行。 When trying systemtest profile, spring boot fails to load contexts and fails. 尝试使用systemtest配置文件时,spring boot无法加载上下文并且失败。 This is caused by spring boot finding h2 dependency and trying to configure datasource defined in application-systemtest.yml 这是由于Spring Boot找到h2依赖项并尝试配置application-systemtest.yml中定义的数据源引起的

So I modified the build.gradle dependencies closure 所以我修改了build.gradle依赖项关闭

def profile = project.findProperty('spring.profiles.active')

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web-services")
    compile('org.springframework.boot:spring-boot-starter-actuator')
    if (profile == 'development') {
        runtime('com.h2database:h2:1.4.195')
    } else {
        runtime('com.microsoft.sqlserver:mssql-jdbc')
    }
}

This time spring boot start correctly. 这次弹簧靴正确启动。 Don't like very much this solution as I have to handle the profile configuration partly with Gradle. 我不太喜欢这个解决方案,因为我必须使用Gradle部分处理配置文件配置。 I would like to know if there is a way to configure spring boot so that profile is completely managed within itself, resolving h2 in development environment and sqlserver in systemtest environment, leaving Gradle unaware of spring profiles. 我想知道是否有一种配置spring boot的方法,以便在自己内部完全管理配置文件,从而解决了开发环境中的h2和systemtest环境中的sqlserver的问题,使Gradle不了解spring配置文件。

How to solve this problem ? 如何解决这个问题呢 ?

It wouldn't be advisable to have a different artifact / binary depending on the DB product. 建议不要使用不同的工件/二进制文件,具体取决于数据库产品。 Try to configure the datasources / Spring profiles as suggested by @M. 尝试按照@M的建议配置数据源/ Spring配置文件。 Deinum to prevent the datasource to be configured as H2 when using a different DB. 使用Deinum防止在使用其他DB时将数据源配置为H2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM