问题描述
我正在Spring Boot中开发一个简单的应用程序.它是在本地开发的(并且可以使用):
I was working on a simple application in Spring Boot. It was developed locally (and it works) with:
- Gradle,
- H2数据库,其连接属性在位于项目根目录的
application.properties
中设置 - Maven文件夹结构(
src/main/groovy
,src/main/resources
等)
- Gradle,
- H2 database with connection properties set in
application.properties
placed on project's root - Maven folders structure (
src/main/groovy
,src/main/resources
, etc.)
现在是时候将其部署到Openshift上了,因此我需要使用MySQL设置创建其他生产配置,但是我不知道将其放置在哪里以及如何使用它.
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.
所以我的问题是:
- 我应该怎么做才能拥有两个不同的配置(
development
和production
)? - 配置文件放在哪里?
- 我是否需要更改
build.gradle
中的某些内容? - 如何使用开发或生产配置构建该应用?
- 如何使用开发或生产配置运行该应用?
- 创建多个环境配置的最佳实践是什么?
- What should I do to have two different configurations (
development
andproduction
)? - Where to put the configuration files?
- Do I have to change something in the
build.gradle
? - How to build the app with a development or production config?
- How to run the app with a development or production config?
- What are the best practices for creating multiple environment configurations?
我是一个前端开发人员,所有这些后端内容对我来说都不是显而易见的,因此请在您的答案中加以考虑.
I'm rather a frontend dev and all these backend stuff are not obvious for me, so please consider it in your answers.
这是我当前的 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()
}
推荐答案
根据您的情况,您可以使用配置文件来实现它.您可以在此处.对于每个配置文件,您可以具有特定的应用程序属性文件(名为 application-%PROFILE_NAME%.properties
,例如 application-prod.properties
, .yml
配置文件也是如此),并且您必须指定要使用的配置文件,然后才能通过命令行开关启动应用程序,例如:
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:
--spring.profiles.active=prod
与您的 application.properties
文件位于同一位置.
Just in the same place as your application.properties
file.
否,您不需要修改构建脚本.由于运行应用程序需要所有特定的配置,而不是构建.
No, you don't need to modify your build script. Since all specific configurations are needed for running your application, not for building.
您不需要使用某些特定的配置来构建它,只需运行它即可.
You don't need to build it with some specific configuration, just run it with it.
如前所述-只需指定启动应用程序时要使用的配置文件即可.
As it was said earlier - just specify what profile to use when starting the application.
对于我来说,如果您使用spring-可以使用概要文件以及特定于概要文件的配置和bean定义.
As for me, if you use a spring - to use profiles and profile specific configuration and bean-definitions.
这篇关于Spring Boot + Gradle-在哪里放置环境配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!