2

我使用弹簧引导1.5.2,并使用配置文件,但我发现了一件很奇怪的事情。弹簧引导始终使用相同的配置文件

我的春天启动的资源文件夹是这样的: enter image description here

CONFIGS在application.yml

spring: 
    profiles: 
    active: @[email protected] 

应用dev.yml

spring: 
    profiles: dev 
    datasource: 
     driver-class-name: com.mysql.jdbc.Driver 
     url: jdbc:mysql://localhost:3306/db1 
     username: root 
     password: 
server: 
    port: 8080 

应用test.yml

spring: 
    profiles: test 
    datasource: 
    driver-class-name: com.mysql.jdbc.Driver 
    url: jdbc:mysql://localhost:3306/db2 
    username: root 
    password: 

server: 
    port: 8081 

我的pom.xml,只包含资源部分和配置文件部分。

<!-- profile --> 
<profiles> 
    <profile> 
     <id>dev</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <properties> 
      <build.profile.id>dev</build.profile.id> 
      <profileActive>dev</profileActive> 
     </properties> 
    </profile> 
    <profile> 
     <id>test</id> 
     <properties> 
      <build.profile.id>test</build.profile.id> 
      <profileActive>test</profileActive> 
     </properties> 
    </profile> 
    <profile> 
     <id>prod</id> 
     <properties> 
      <build.profile.id>prod</build.profile.id> 
      <profileActive>prod</profileActive> 
     </properties> 
    </profile> 
</profiles> 


<resources> 
     <resource> 
      <directory>src/main/java</directory> 
      <excludes> 
       <exclude>**/*.java</exclude> 
      </excludes> 
     </resource> 
     <resource> 
      <directory>src/main/resources</directory> 
      <filtering>true</filtering> 
      <includes> 
       <include>application-${profileActive}.yml</include> 
       <include>application.yml</include> 
       <include>templates/*</include> 
      </includes> 
     </resource> 

    </resources> 

我现在想用测试概,发现一切正常,@[email protected]已经更换到test;

mvn clean package -Dmaven.test.skip=true -Ptest 

看起来好像一切正​​常。

enter image description here

但是当我尝试运行jar,它总是使用开发轮廓,虽然application.yml显示了我们现在使用的test or prod轮廓。

enter image description here

我不知道哪里是错误的,我阳明CONFIGS。我尝试将所有配置文件配置包含在一个application.yml文件中。但应用程序仍然使用dev配置文件。

完全CONFIGS在一个application.yml文件

spring: 
    profiles: 
    active: @[email protected] 

--- 
spring: 
    profiles: dev 
    datasource: 
    driver-class-name: com.mysql.jdbc.Driver 
    url: jdbc:mysql://localhost:3306/db1 
    username: root 
    password: 

server: 
    port: 8080 

--- 
spring: 
    profiles: test 
    datasource: 
    driver-class-name: com.mysql.jdbc.Driver 
    url: jdbc:mysql://localhost:3306/db2 
    username: root 
    password: 

server: 
    port: 8081 

--- 
spring: 
    profiles: prod 

server: 
    port: 9000 

最后,我尝试使用性能文件,所有我的configs的正常工作,当我运行我的应用程序可以使用正确的个人资料。

而现在,我只想知道我的yml配置有什么问题。

在此先感谢!

+0

你可以把所有的东西放在一个Git仓库中,让你很容易看到_exactly_你如何配置东西? –

+0

我刚刚创建了一个与您定义的完全相同的简单项目。一切工作如预期。你可以尝试在intelliJ之外运行jar吗? – Patrick

回答

1

终于,我找到了原因运行任务。

我的项目是一个多模块Maven项目。

一个模块,我使用yml格式,

等是property格式。

当我制作两个模块都是yml格式时,一切正常。

感谢大家!谢谢!

2

尝试运行你的罐子像

java -jar -Dspring.profiles.active=test yourapp.jar 

或更换@ profileActive @上你所需要的配置文件的名称。

0

问题可能在于,当spring启动读取您的application.yml时,它已经决定回退到默认配置文件。从那里开始,我认为,切换配置文件已经晚了。

你可以尝试的是在名为“bootstrap.yml”的配置文件中定义你的spring.profiles.active属性。这可能对你有用。

0

我将spring-boot-starter-web的描述复制为附加依赖项,以便能够启动一个小应用程序。

一切按预期工作。如果我运行mvn clean package -Ptesttest配置文件将被激活。与其他配置文件相同。我把所有的配置文件放在一个yml文件中,注释掉<include>application-${profileActive}.yml</include>,它也可以工作。

当我像父母文件系统路径一样执行JAR文件时,一切都按预期工作。当我使用另一个配置文件同时执行另一个Maven构建并重新启动应用程序时,将使用新的配置文件。

如果您使用的mvn clean packagedev之外的其他配置文件,任何application-dev.yml都不应该在生成的JAR中。如果没有clean以及具有不同Maven配置文件的不同构建,它们将全部位于JAR中。

你使用哪个操作系统?是否有可能在您的JAR文件中包含文件句柄并且不会被替换?但是这应该在Maven构建中发出信号。

+0

谢谢,我会再试一次。 – diligent

0

我会亲自执行以下操作:

1)从application.yaml删除此(因为你可以设置一个JVM属性用于此)

spring: 
    profiles: 
    active: @[email protected] 

2)删除轮廓特定头从你的每个配置文件特定的yaml文件。您不需要它,因为该配置文件基于文件后缀,例如删除此:

spring: 
    profiles: dev 

3)奥列格说,刚与Dspring.profiles.active =无论