簡體   English   中英

使用 Maven 配置構建 Spring Boot Profile-specific Properties

[英]Spring Boot Profile-specific Properties build using Maven Configuration

如何使用 maven 為 spring boot 應用程序構建環境特定的 war 文件。我創建了 3 個配置文件配置文件,放置在 src\/main\/resource\/ 文件夾中。

  1. 應用程序.prod.properties<\/li>
  2. 應用程序.dev.properties<\/li>
  3. 應用程序.test.properties<\/li><\/ol>

    我可以通過在 VM 參數選項卡中使用值“-Dspring.profiles.active=dev”指定所需的配置文件類型來運行應用程序,同時將項目作為 Spring Boot 應用程序執行。

    在這里,作為 Spring Boot 應用程序運行時,我可以指定所需的配置文件。 以同樣的方式,當我需要使用不同的配置文件進行 MAVEN 安裝時。 是否有任何方法可以將配置文件指定為 Maven 安裝目標的運行配置中的 VM 參數列表的一部分。

    我有不接觸現有java代碼的限制。<\/strong>

    我正在使用 STS IDE、Spring boot 1.5.2.RELEASE 版本、Java 1.8 和 oracle db。

    以同樣的方式也幫助我如何在 Jenkins 中配置配置文件。

    我的配置文件配置有兩個要求。

    1. 在 STS IDE 中將應用程序作為帶有 VM args 的 spring boot 應用程序運行。 使用以下 VM ARGS -Dspring.profiles.active=dev<\/code><\/li><\/ol>
      塊引用

      <\/blockquote>

      (這里我在開發環境中本地啟動 SpringBootApp 時遇到異常)。


      應用程序無法啟動


      描述:

      無法確定數據庫類型 NONE 的嵌入式數據庫驅動程序類

      行動:

      如果您想要一個嵌入式數據庫,請在類路徑中放置一個受支持的數據庫。 如果您有要從特定配置文件加載的數據庫設置,您可能需要激活它(配置文件“dev”當前處於活動狀態)。

      塊引用

      1. 如何使用 maven install 通過動態指定配置文件來生成戰爭文件來做同樣的事情。我找不到解決方案。<\/li><\/ol>"

在主application.properties文件中,將spring.profiles.active設置為@myActiveProfile@ (或您希望的任何名稱)

spring.profiles.active=@myActiveProfile@

將application.properties文件添加為已過濾的資源,以便在構建階段替換占位符myActiveProfile

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>

將配置文件部分添加到您的pom.xml

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <myActiveProfile>dev</myActiveProfile>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <myActiveProfile>prod</myActiveProfile>
        </properties>
    </profile>
</profiles>

基本上,您可以在執行特定目標時指定maven配置文件。 例如, mvn install -P profileName 在配置文件中,您可以創建一個屬性,可以在運行目標時從命令行傳遞,如mvn install -DmyActiveProfile=foo

希望這可以幫助。

有用的網址

如何使用maven配置文件設置彈簧活動配置文件 https://maven.apache.org/guides/introduction/introduction-to-profiles.html

在這里,首先我建議將屬性文件重命名為application-prod.properties,application-dev.properties和application-test.properties。

其次,maven安裝目標是編譯和構建項目。

如果你還想運行你的應用程序,當你安裝時我建議使用spring-boot-maven-plugin。

你可以使用類似下面的maven命令

mvn clean install spring-boot:run

以下是一些鏈接供您參考

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html

https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

文件結構:

/src/main/資源

=>

應用程序屬性:

spring.profiles.active:@spring.profiles.active@'

應用程序-dev.properties

應用程序-prod.properties

IDE-Eclipse:

Right click on the project=>Run As=>Run Configuration=>Arguments=>VM Arguments:-Dspring.profiles.active=dev

命令:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM