Spring Boot + Gradle-在哪里放置环境配置? [英] Spring Boot + Gradle - where to put environment configuration?

查看:54
本文介绍了Spring Boot + Gradle-在哪里放置环境配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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.

所以我的问题是:

  1. 我应该怎么做才能拥有两个不同的配置( development production )?
  2. 配置文件放在哪里?
  3. 我是否需要更改 build.gradle 中的某些内容?
  4. 如何使用开发或生产配置构建该应用?
  5. 如何使用开发或生产配置运行该应用?
  6. 创建多个环境配置的最佳实践是什么?
  1. What should I do to have two different configurations (development and production)?
  2. Where to put the configuration files?
  3. Do I have to change something in the build.gradle?
  4. How to build the app with a development or production config?
  5. How to run the app with a development or production config?
  6. 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()
}

推荐答案

我应该怎么做才能拥有两种不同的配置(开发和生产)?

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:

--spring.profiles.active=prod

配置文件放在哪里?

Where to put the configuration files?

与您的 application.properties 文件位于同一位置.

Just in the same place as your application.properties file.

我是否需要在build.gradle中进行某些更改?

Do I have to change something in the 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?

对于我来说,如果您使用spring-可以使用概要文件以及特定于概要文件的配置和bean定义.

As for me, if you use a spring - to use profiles and profile specific configuration and bean-definitions.

这篇关于Spring Boot + Gradle-在哪里放置环境配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆