Java 如何在某个弹簧配置文件中禁用飞路?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44287821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable flyway in a certain spring profile?
提问by gstackoverflow
Now I have a spring-boot app which uses MsSQL server. And we use flyway for migrations.
现在我有一个使用 MsSQL 服务器的 spring-boot 应用程序。我们使用 flyway 进行迁移。
I want to add an additional profile for tests. I want to generate tables from entity classes instead of using flyway.
我想为测试添加一个额外的配置文件。我想从实体类而不是使用 flyway 生成表。
I tried smth to write like this in application.yaml
我试着在 application.yaml 中这样写
spring:
profiles: test
jpa:
generate-ddl: true
hibernate:
datasource:
url: jdbc:h2:mem:test_db;MODE=MSSQLServer
username: sa
password:
but flyway starts anyway
但无论如何都会开始飞行
采纳答案by Stanislav
This answer works with Spring Boot 1.X version. If you are looking for answer for Spring Boot 2.X, you should see the below answer.
此答案适用于 Spring Boot 1.X 版本。如果您正在寻找Spring Boot 2.X 的答案,您应该会看到以下答案。
There is a property available for spring-boot to disable flyway if it's needed flyway.enabled
which is true by default.
如果需要flyway.enabled
,spring-boot 有一个属性可用于禁用 flyway ,默认情况下为 true。
You can have a profile specific configuration, in your case it should be named as application-test.yml
. This configuration can disable flyway if profile is active. You just have to declare it as follows:
您可以拥有特定于配置文件的配置,在您的情况下,它应命名为application-test.yml
. 如果配置文件处于活动状态,此配置可以禁用飞行路线。您只需按如下方式声明它:
flyway:
enabled: false
And if you specify test profile in common configuration, just add it to it's root.
如果您在通用配置中指定测试配置文件,只需将其添加到它的根目录中即可。
回答by Todd
FYI, for anybody who comes here looking for this, the property name has changed for Spring Boot 2.0:
仅供参考,对于来这里寻找此内容的任何人,Spring Boot 2.0 的属性名称已更改:
For application.properties
format:
对于application.properties
格式:
spring.flyway.enabled=false
For application.yml
format:
对于application.yml
格式:
spring:
flyway:
enabled: false
Update:To disable flyway in a specific profile, you can put that property in the properties file specific to that profile. For instance, if your profile is called "abc", you can put it in application-abc.properties
. Check out Spring's documentation on Profile-specific propertiesfor more clarity on how to name the files. Generally, the format is application-{profileName}.properties
.
更新:要在特定配置文件中禁用 flyway,您可以将该属性放在特定于该配置文件的属性文件中。例如,如果您的个人资料名为“abc”,您可以将其放在application-abc.properties
. 查看Spring 关于 Profile-specific properties 的文档,以更清楚地了解如何命名文件。通常,格式为application-{profileName}.properties
.
回答by Jonathan JOhx
JIC the official documentation with current spring boot 2.x : Data migration propertiesand have a look on tag # FLYWAYyou will find many properties that can help you.
JIC 当前 spring boot 2.x 的官方文档: 数据迁移属性并查看标记# FLYWAY您会发现许多可以帮助您的属性。
spring.flyway.enabled=false # Whether to enable flyway.
回答by gstackoverflow
Here eample of application.yaml
It defines 2 profiles:
1. enable_flyway_profile
- enables flyway
2. disable_flyway_profile
- disables flyway
这里的示例application.yaml
定义了 2 个配置文件:
1. enable_flyway_profile
- 启用飞行路线
2. disable_flyway_profile
- 禁用飞行路线
spring:
profiles:
active: "enable_flyway_profile"
flyway:
enable: true
....
---
spring:
profiles:
active: "disable_flyway_profile"
flyway:
enable: false
....