[英]Spring Boot + Gradle - where to put environment configuration?
I was working on a simple application in Spring Boot. 我正在使用Spring Boot中的一个简单应用程序。 It was developed locally (and it works) with:
它是在本地开发的(并且有效):
application.properties
placed on project's root application.properties
设置的连接属性的H2数据库放置在项目的根目录中 src/main/groovy
, src/main/resources
, etc.) src/main/groovy
, src/main/resources
等) Now it's the time when I'd like to deploy it to the Openshift, so I need to create an additional, production configuration with a MySQL settings, but I don't know where to put it and how to use it. 现在是我想将它部署到Openshift的时候,所以我需要创建一个带有MySQL设置的附加生产配置,但我不知道在哪里放置它以及如何使用它。
So my questions are: 所以我的问题是:
development
and production
)? development
和production
)? build.gradle
? build.gradle
更改某些build.gradle
? I'm rather a frontend dev and all these backend stuff are not obvious for me, so please consider it in your answers. 我是一个前端开发者,所有这些后端的东西对我来说并不明显,所以请在你的答案中考虑它。
This is the content of my current build.gradle
这是我当前
build.gradle
的内容
plugins {
id 'org.springframework.boot' version '1.5.3.RELEASE'
id 'java'
id 'groovy'
}
jar {
baseName = 'myproject'
version = '0.0.1-SNAPSHOT'
}
repositories {
jcenter()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile 'mysql:mysql-connector-java'
compile("com.h2database:h2")
compile("org.springframework.boot:spring-boot-starter-security")
compile('io.jsonwebtoken:jjwt:0.7.0')
compile localGroovy()
}
What should I do to have two different configurations (development and production)?
我应该怎么做才能有两种不同的配置(开发和生产)?
In your case, you can use a profiles to achieve it. 在您的情况下,您可以使用配置文件来实现它。 You can read about it here .
你可以在这里阅读它。 For each profile you can have specific application properties file (named
application-%PROFILE_NAME%.properties
, like application-prod.properties
, the same is true for .yml
configuration files) And you have to specify what profile yo use then you are starting your app via command line switch for example like so: 对于每个配置文件,您可以拥有特定的应用程序属性文件(名为
application-%PROFILE_NAME%.properties
,如application-prod.properties
, .yml
配置文件也是如此)并且您必须指定您使用的配置文件然后开始您的应用程序通过命令行开关,例如:
--spring.profiles.active=prod
Where to put the configuration files?
在哪里放配置文件?
Just in the same place as your application.properties
file. 与
application.properties
文件位于同一位置。
Do I have to change something in the build.gradle?
我是否必须在build.gradle中更改某些内容?
No, you don't need to modify your build script. 不,您不需要修改构建脚本。 Since all specific configurations are needed for running your application, not for building.
由于运行应用程序需要所有特定配置,因此不需要构建。
How to build the app with a development or production config?
如何使用开发或生产配置构建应用程序?
You don't need to build it with some specific configuration, just run it with it. 您不需要使用某些特定配置构建它,只需使用它运行它。
How to run the app with a development or production config?
如何使用开发或生产配置运行应用程序?
As it was said earlier - just specify what profile to use when starting the application. 如前所述 - 只需指定启动应用程序时要使用的配置文件。
What are the best practices for creating multiple environment configurations?
创建多个环境配置的最佳做法是什么?
As for me, if you use a spring - to use profiles and profile specific configuration and bean-definitions. 至于我,如果你使用spring - 使用配置文件和配置文件特定的配置和bean定义。
在此处概述的现有application.properties文件旁边创建一个单独的application-dev.properties
,以包含您的h2配置https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features- external-config.html #boot-features-external-config-profile-specific-properties然后在dev中运行时通过在启动应用程序时传入以下参数来激活它--spring.profiles.active=dev
你可以做到同样用于生产,即application-prod.properties
并在application.properties
保留通用配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.