gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:49:39 25 4
gpt4 key购买 nike

我当时正在使用 Spring Boot 开发一个简单的应用程序。它是在本地开发的(并且有效):

  • Gradle ,
  • application.properties 中设置了连接属性的 H2 数据库放置在项目的根目录上
  • Maven 文件夹结构(src/main/groovysrc/main/resources 等)

现在是我想将它部署到 Openshift 的时候了,所以我需要使用 MySQL 设置创建一个额外的生产配置,但我不知道将它放在哪里以及如何使用它。

所以我的问题是:

  1. 我应该怎么做才能拥有两种不同的配置(开发生产)?
  2. 将配置文件放在哪里?
  3. 我是否必须更改 build.gradle 中的某些内容?
  4. 如何使用开发或生产配置构建应用程序?
  5. 如何使用开发或生产配置运行应用程序?
  6. 创建多个环境配置的最佳做法是什么?

我是一名前端开发人员,所有这些后端内容对我来说并不明显,所以请在您的回答中考虑这一点。

这是我当前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)?

在您的情况下,您可以使用配置文件来实现它。你可以阅读它here .对于每个配置文件,您可以拥有 specific application properties文件(名为application-%PROFILE_NAME%.properties,如application-prod.properties.yml配置文件也是如此)和你必须指定你使用的配置文件,然后你通过命令行开关启动你的应用程序,例如:

--spring.profiles.active=prod

Where to put the configuration files?

就在您的 application.properties 文件的同一位置。

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?

对于我来说,如果您使用 Spring - 使用配置文件和配置文件特定的配置和 bean 定义。

关于java - Spring Boot + Gradle - 在哪里放置环境配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com