使用spring-boot:run时,spring-boot命令行属性是否可用? [英] Are spring-boot command line properties available when using spring-boot:run?

查看:114
本文介绍了使用spring-boot:run时,spring-boot命令行属性是否可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题:

如何配置环境配置文件,甚至只是配置文件的替代名称 spring-boot:运行吗?

How can you configure environment profiles, or even just an alternative configuration file name for spring-boot:run?

长版:

是的,我阅读了文档. http://docs.spring.io/spring-boot/docs/current/reference/html/index.html

Yes, I read the doc. http://docs.spring.io/spring-boot/docs/current/reference/html/index.html

我在src/main/resources/application-mysql.properties中有我的应用程序配置设置:

I have my application configuration settings in src/main/resources/application-mysql.properties:

# Specify the DBMS
spring.jpa.database = MYSQL

# Other Mysql config properties

spring.jpa.hibernate.ddl-auto=create

有一个对应的application-hsql.properties,其中包含与Hsql相同的配置选项集. 没有application.poperties

There is a corresponding application-hsql.properties, which contains the same set of configuration options for Hsql. There is no application.poperties

有一个相应的import.sql:

There is a corresponding import.sql:

insert into users(name, email) values ('User One',  'one@email.com')
insert into users(name, email) values ('Two User', 'two@email.com')

单元测试仅用于检查存储库中这些用户的存在.

The unit tests exist only to check for the presence of these users in the repo.

我相信,每当使用MySql配置运行测试时,这些行都会添加到users表中.当使用hsql运行测试时,mysql db应该不受影响. 我要在运行之间手动删除用户表,因为我想手动查看它何时存在.

I believe it to be true that whenever the test is run using the MySql configuration, those rows are added to the users table. When the test is run with hsql, the mysql db should be unaffected. I am manually dropping the users table between runs, because I want to manually see when it exists.

1)如果我运行mvn test,则测试将使用配置的db:

1) If I run mvn test, the tests use the configured db:

mvn -Dspring.profiles.active=mysql clean test 

产生mysql数据库行和

Produces mysql database rows and

mvn -Dspring.profiles.active=hsql clean test 

不是.

2)如果我制作一个程序包,然后运行生成的jar文件,则可以指定配置文件名:

2) If I make a package, and then run the resulting jar file, I am able to specify a config file name:

java -jar -Dspring.profiles.active=mysql  ./target/app.jar

3)如果我使用spring-boot:run进行运行,则只会发现application.properties中的属性(此测试方案中不存在).

3) If I run with spring-boot:run, only properties in application.properties (which doesn't exist in this test scenario) are discovered.

mvn -Dspring.profiles.active=mysql clean spring-boot:run

spring-boot:run与启动单元测试和启动jar相比,在启动方面有什么不同? db config是一个示例,但是从理论上讲,我希望能够在本地运行应用程序与生产配置时指定一组dev config.

What does spring-boot:run do differently in launching than running unit tests and kicking off the jar? The db config is one example, but in theory I'd like to be able to specify a set of dev configs when the application is being run locally vs. a production configuration.

推荐答案

Maven spring-boot插件派生了一个新进程,因此您需要通过

The Maven spring-boot plugin forks a new process so you'll need to send any extra parameters to it via jvmArguments, e.g.:

mvn spring-boot:run -Drun.jvmArguments =-Dspring.profiles.active = mysql"

mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=mysql"

这篇关于使用spring-boot:run时,spring-boot命令行属性是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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