2017-01-31 32 views
0

我在pom.xml中定義的三個配置文件如何在application.properties彈簧啓動注入行家文件的屬性

<profiles> 
    <profile> 
     <id>development</id> 

     <properties> 
      <activatedProperties>development</activatedProperties> 
      <env.identifier>dev</env.identifier> 

     </properties> 

     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
    </profile> 

    <profile> 
     <id>qualification</id> 

     <properties> 
      <activatedProperties>qualification</activatedProperties> 
      <env.identifier>klif</env.identifier> 

     </properties> 
    </profile> 

    <profile> 
     <id>production</id> 

     <properties> 
      <activatedProperties>production</activatedProperties> 
      <env.identifier>prod</env.identifier> 

     </properties> 
    </profile> 
</profiles> 

我也修改了構建節點到這一點:

<build> 
    <finalName>myApplication</finalName> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <filtering>true</filtering> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <configuration> 
       <executable>true</executable> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

而且我創建了三個屬性文件並以此模式命名:application- [profileId] .properties。 在通用的application.properties中,我定義了屬性:

[email protected]@
以指定彈出引導使用哪個配置文件。 我還爲我的日誌文件定義了一個文件,如下所示:
logging.file=logs/[email protected]@.log
問題是,即使在指定配置文件時啓動我的應用程序時,我始終會將其作爲我的env.identifier開發者的值。 如何解決這個問題?由於

+0

你不應該這樣做。不要使用maven配置文件來創建多個工件。這基本上意味着你要生產一個未經測試的工件。只需在爲不同環境啓動應用程序時指定這些參數,而不是試圖使用maven配置文件。 –

回答

0

更新

您可能忽略的Maven插件資源:

<plugins> 


     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-resources-plugin</artifactId> 
     <version>2.7</version> 
     <configuration> 
      <delimiters> 
      <delimiter>@</delimiter> 
      </delimiters> 
      <useDefaultDelimiters>false</useDefaultDelimiters> 
     </configuration> 
     </plugin> 
     ... 
    </plugins> 

然後在你的命令,你只需要添加:

mvn resources:resources 
+0

你能解釋一下這個插件的功能嗎? – Habchi

+0

你可以在這裏https://maven.apache.org/plugins/maven-resources-plugin/。我始終將其與您已指定的某些資源上的結合使用。在你的屬性文件中,我認爲你有$ {env.properties}而不是@ env.properties @。試試看吧 –

+0

不好運氣。語法@ env.properties @是Spring引導的新語法。它不承認$ {}了 – Habchi