two*_*ods 32 java spring spring-boot
我有application.yml,application-dev.yml和application-dev.yml
mvn spring-boot:run -Dspring.profiles.active=dev它不起作用,我不能选择使用开发配置文件mvn spring-boot:run.我该如何选择?java -jar XXX.jar --spring.profiles.active=dev工作,我尝试-Dspring.profiles.active=dev但它不起作用.在我的项目中,我使用java -jar XXX.jar它运行,但如果我java -jar XXX.jar --spring.profiles.active=dev用来选择开发配置文件,控制台打印这么多日志并警告我从未见过用过java -jar XXX.jar,并告诉我APPLICATION FAILED TO START 那么如何解决两个问题呢?谢谢〜
fra*_*cis 54
我不确定我是否完全理解这个问题,但我会尝试通过提供有关Spring Boot中的配置文件的一些细节来回答.
对于您的#1示例,根据文档,您可以使用Spring Boot Maven插件选择配置文件-Drun.profiles.
编辑:对于Spring Boot 2.0+ run已重命名为spring-boot.run
mvn spring-boot:run -Drun.profiles=dev
Run Code Online (Sandbox Code Playgroud)
http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html
在#2示例中,您将在jar名称后面定义活动配置文件.您需要在运行jar的名称之前提供JVM参数.
java -jar -Dspring.profiles.active=dev XXX.jar
Run Code Online (Sandbox Code Playgroud)
基本信息:
你提到你有一个application.yml和一个application-dev.yml.使用配置文件运行dev实际上将加载两个配置文件.值from application-dev.yml将覆盖由提供的相同值,application.yml但yml将加载两个文件中的值.
还有多种方法可以定义活动配置文件.
您可以像-Dspring.profiles.active在运行jar时一样定义它们.您还可以使用SPRING_PROFILES_ACTIVE环境变量或spring.profiles.active系统属性设置配置文件.
更多信息可以在这里找到:http: //docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles
小智 10
如果您使用的是Spring Boot Maven插件,请运行:
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
Run Code Online (Sandbox Code Playgroud)
(https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html)
由于 Spring Boot v2+
我已经用 Spring Boot v2.3.5.RELEASE 验证过
使用 Spring Boot Maven 插件
您可以提供这样的命令行参数:
mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"
您可以像这样提供JVM 参数:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
java -jar
java -Dspring.profiles.active=dev -jar app.jar
(虚拟机参数)
或者
java -jar app.jar --spring.profiles.active=dev (程序参数)
2021 年 4 月首次更新
我不时地回到这个问题。似乎这里发生了 Windows vs *nix 的事情。
该建议是使用mvn spring-boot:run -Dspring-boot.run.profiles=dev出于某种原因,这不起作用与Windows 7的终端(命令行提示符)至少不适合我。我还没有测试过 Windows 10 终端。但是它确实适用于 Babun(已停产),但不适用于 Git-Bash。还没有测试过 Babun 和 Git-Bash 都建立在上面的 Cygwin。Windows 10 和 Linux 上的 WSL 应该都可以正常工作。
2021 年 4 月第二次更新
我今天升级到使用 Spring Boot v2.4.5 后,问题似乎消失了。
mvn spring-boot:run -Dspring-boot.run.profiles=dev现在工作正常。
小智 6
您不需要三个.yml文件。您可以有一个application.yml文件,并在同一档案中写特定于档案的属性,其中每个档案节之间用3个连字符(---)隔开。
接下来,要选择当前的活动配置文件,您也可以在application.yml文件中指定它,如下所示:
spring:
profiles:
active:
- local
Run Code Online (Sandbox Code Playgroud)
但是,如果您设置环境变量,则此配置将被覆盖,例如:SPRING_PROFILES_ACTIVE = dev
这是您所需的样本文件:
# include common properties for every profile in this section
server.port: 5000
spring:
profiles:
active:
- local
---
# profile specific properties
spring:
profiles: local
datasource:
url: jdbc:mysql://localhost:3306/
username: root
password: root
---
# profile specific properties
spring:
profiles: dev
datasource:
url: jdbc:mysql://<dev db url>
username: <username>
password: <password>
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您使用的是 maven,请在 pom.xml 中定义您的配置文件,如下所示
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdbc.url>dbUrl</jdbc.url>
<jdbc.username>dbuser</jdbc.username>
<jdbc.password>dbPassword</jdbc.password>
<jdbc.driver>dbDriver</jdbc.driver>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<jdbc.url>dbUrl</jdbc.url>
<jdbc.username>dbuser</jdbc.username>
<jdbc.password>dbPassword</jdbc.password>
<jdbc.driver>dbDriver</jdbc.driver>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<jdbc.url>dbUrl</jdbc.url>
<jdbc.username>dbuser</jdbc.username>
<jdbc.password>dbPassword</jdbc.password>
<jdbc.driver>dbDriver</jdbc.driver>
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)
默认情况下,即如果未选择配置文件,则将始终使用本地配置文件。
要在 Spring Boot 2.xx 中选择特定的配置文件,请使用以下命令。
mvn spring-boot:run -Dspring-boot.run.profiles=dev
Run Code Online (Sandbox Code Playgroud)
如果要使用特定配置文件的属性构建/编译,请使用以下命令。
mvn clean install -Pdev -DprofileIdEnabled=true
Run Code Online (Sandbox Code Playgroud)
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
**来源- ** https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html
基本上,当您的项目中存在多个 application-{environment}.properties 时,这是需要的。默认情况下,如果您在命令行中传递了 -Drun.profiles 或在中传递了 activeByDefault true
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
Run Code Online (Sandbox Code Playgroud)
没有像上面这样定义的内容,默认情况下它会选择 application.properties,否则您需要通过附加 -Drun.profiles={dev/stage/prod} 来选择。
长话短说
mvn spring-boot:run -Drun.profiles=dev
您可以根据一个application.properties(yml)中的配置文件指定属性,如下所示。然后
mvn clean spring-boot:run -Dspring.profiles.active=dev应该正确运行。这个对我有用
| 归档时间: |
|
| 查看次数: |
55223 次 |
| 最近记录: |