简体   繁体   English

Spring Boot + Gradle - 在哪里放置环境配置?

[英]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: 它是在本地开发的(并且有效):

  • Gradle, 摇篮,
  • H2 database with connection properties set in application.properties placed on project's root 具有在application.properties设置的连接属性的H2数据库放置在项目的根目录中
  • Maven folders structure ( src/main/groovy , src/main/resources , etc.) Maven文件夹结构( src/main/groovysrc/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: 所以我的问题是:

  1. What should I do to have two different configurations ( development and production )? 我应该怎么做才能有两种不同的配置( developmentproduction )?
  2. Where to put the configuration files? 在哪里放配置文件?
  3. Do I have to change something in the build.gradle ? 我是否必须在build.gradle更改某些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. 我是一个前端开发者,所有这些后端的东西对我来说并不明显,所以请在你的答案中考虑它。

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.

相关问题 Gradle Spring Boot自定义配置 - Gradle Spring Boot Custom Configuration 在 Gradle 构建中使用“实现”配置指定的依赖项未放入 Spring 引导胖 jar - Dependencies specified with `implementation` configuration in Gradle build not put into Spring Boot fat jar 在Spring Boot中放置@Bean的位置? - Where to put @Bean in Spring Boot? 用于gradle intellij的环境变量-Spring Boot - environment variable used with gradle intellij - spring boot 无法使用 gradle 生成 Spring 引导配置元数据 - Cannot generate Spring Boot configuration metadata with gradle spring 引导配置中列表的环境变量 - Environment variables for list in spring boot configuration 将值设置/放入Spring-boot的Spring环境中以供以后使用 - Set/Put value into Spring environment of Spring-boot for using later 正确设置 Azure 管道 -> Gradle -> JUnit -> Z38008DD81C2F4AF17985ECF6DZCE引导环境变量 - Setting Azure Pipelines -> Gradle -> JUnit -> Spring Boot environment variables correctly Gradle Spring Boot将环境变量传递给嵌入式vuejs应用 - gradle spring boot pass environment variables to embedded vuejs app 如何在离线环境中使用 gradle 为 Spring 启动创建可执行 Jar - How to create an executable Jar for Spring Boot using gradle in an offline environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM