欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解maven中profiles使用實現(xiàn)

 更新時間:2022年02月07日 08:36:42   作者:bbird2018  
本文主要介紹了maven中profiles使用實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用的場景

常常遇到一些項目中多環(huán)境切換的問題。比如在開發(fā)過程中用到開發(fā)環(huán)境,在測試中使用測試環(huán)境,在生產(chǎn)中用生產(chǎn)環(huán)境的情況。springboot中提供了 spring.profile.active的方式來實現(xiàn)多環(huán)境的切換,通過設(shè)置環(huán)境變量和啟動參數(shù)的方式。但是這樣做終究不能一勞永逸,要么需要修改yml文件,要么需要記得啟動的時候帶上參數(shù)。而利用maven的profiles,可以減少很多工作。讓我們通過幾個例子一步步的掌握使用maven的profiles屬性。

快速上手

pom.xml文件設(shè)置

<profiles>
        <profile>
            <!--不同環(huán)境Profile的唯一id-->
            <id>dev</id>
            <properties>
                <!--profiles.active是自定義的字段(名字隨便起),自定義字段可以有多個-->
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
</profiles>

目錄結(jié)構(gòu)

application.yml

spring:
  profiles:
    active: @profiles.active@

application-dev.yml中代碼如下

server:
  port: 7091

其他幾個文件我只是把端口號進行了修改,方便打包看不同的效果。

maven打包與激活profiles

你可以執(zhí)行命令

mvn clean package -Ptest

然后啟動jar包,可以看到j(luò)ar包啟動的是test的配置,如果換成-Pdev啟動的就是dev包的端口。

默認啟動方式

如果不帶-Ptest,啟動的是 prod的端口。因為在profiles中我們看到有配置默認的選項。

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

settings.xml中使用activeProfiles指定

<activeProfiles>  
     <activeProfile>profileTest1</activeProfile>  
</activeProfiles>  

通過IDEA的可視化的方式

當然如果使用IDEA工具進行開發(fā),還可以使用可視化的方式進行打包。

更高級的玩法

通過和pom結(jié)合的方式設(shè)置動態(tài)參數(shù)

如果我們希望通過docker-maven-plugin插件,把編譯好的jar打包成docker并且傳入相應(yīng)的開發(fā)、測試、生產(chǎn)的服務(wù)器中去。這個時候,我們就需要根據(jù)不同的條件去傳入不同的服務(wù)器。

在profiles中我們可以做以下定義

 <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.id>dev</profile.id>
                <docker.host>http://dev.demo.com:2375</docker.host>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.id>test</profile.id>
                <docker.host>http://test.demo.com375</docker.host>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profile.id>prod</profile.id>
                <docker.host>http://prod.demo.com:2375</docker.host>
            </properties>
        </profile>
    </profiles>

而在build控件中我們可以使用以下配置

<build>
  <plugins>
     <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.1.0</version>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <imageName>demo/${project.artifactId}</imageName>
                    <imageTags>
                        <imageTag>${project.version}-${current.time}</imageTag>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <forceTags>true</forceTags>
                    <dockerHost>${docker.host}</dockerHost>
                    <forceTags>true</forceTags>
                    <baseImage>java:8</baseImage>
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
  </plugins>
</build>

其中 ${project.artifactId} 和${project.version}是關(guān)于節(jié)點下面和的引用。${current.time}是在build-helper-maven-plugin定義的,我們回頭再研究。

${docker.host}則是我們在profiles中定義的,可以隨著我們選擇不同的profile,把jar包build成不同的docker鏡像,并傳入指定服務(wù)器。

通過和yml結(jié)合設(shè)置動態(tài)參數(shù)

除了可以在pom中設(shè)置動態(tài)參數(shù),使得其根據(jù)profile的不同選擇不同的參數(shù)。還可以通過設(shè)置不同的profile,讓yml選擇不同的參數(shù)。這點和快速上手的例子有點相似。具體如下:

設(shè)置profiles

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.id>dev</profile.id>
                <eureka.url>http://127.0.0.1:8001/eureka</eureka.url>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.id>test</profile.id>
                <eureka.url>http://base-registry:8001/eureka</eureka.url>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profile.id>prod</profile.id>
                <eureka.url>http://base-registry:8001/eureka</eureka.url>
            </properties>
        </profile>
        <profile>
            <id>new</id>
            <properties>
                <profile.id>new</profile.id>
                <eureka.url>http://base-registry:8001/eureka</eureka.url>
            </properties>
        </profile>
    </profiles>

我們在profile中設(shè)置了一個eureka.url的屬性,就可以在yml中直接調(diào)用。

eureka:
  client:
    service-url:
      defaultZone: @eureka.url@
    registry-fetch-interval-seconds: 10
  instance:
    prefer-ip-address: true

在IDEA調(diào)試和啟動的時候,一般會報錯如下:

org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@’ that cannot start any token.

解決方法就是引入yaml.sankeyaml的jar包

<dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
</dependency>

打包不同的資源

在profile打包yml文件的時候,如果我們解壓了jar包,會發(fā)現(xiàn)還是把所有的application-profile.yml文件給打包進去了。這個可以通過設(shè)置打包參數(shù),只打包需要的application文件。

 <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>


    <build>
        <finalName>springmvc</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>prd/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${env}</directory>
            </resource>
        </resources>
    </build>

目錄結(jié)構(gòu)如下:

 到此這篇關(guān)于詳解maven中profiles使用實現(xiàn)的文章就介紹到這了,更多相關(guān)maven中profiles使用 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Spring Boot中初始化資源的幾種方式

    詳解Spring Boot中初始化資源的幾種方式

    這篇文章主要介紹了詳解Spring Boot中初始化資源的幾種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • java保證對象在內(nèi)存中唯一性的實現(xiàn)方法

    java保證對象在內(nèi)存中唯一性的實現(xiàn)方法

    這篇文章主要介紹了java如何保證對象在內(nèi)存中的唯一性,如果創(chuàng)建多個對象的話,可能會引發(fā)出各種各樣的問題,這時,就需要我們保證這個對象在內(nèi)存中的唯一性,需要的朋友可以參考下
    2019-06-06
  • Spring+SpringMVC+MyBatis深入學習及搭建(一)之MyBatis的基礎(chǔ)知識

    Spring+SpringMVC+MyBatis深入學習及搭建(一)之MyBatis的基礎(chǔ)知識

    這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學習及搭建(一)之MyBatis的基礎(chǔ)知識,需要的朋友可以參考下
    2017-05-05
  • Java集合框架之LinkedHashSet類解讀

    Java集合框架之LinkedHashSet類解讀

    這篇文章主要介紹了Java集合框架之LinkedHashSet類解讀,LinkedHashSet是HashSet的有序版本,它跨所有元素維護一個雙向鏈接的List,當需要維護迭代順序時,就使用這個類,當遍歷HashSet時,順序是不可預測的,需要的朋友可以參考下
    2023-09-09
  • Spring 校驗(validator,JSR-303)簡單實現(xiàn)方式

    Spring 校驗(validator,JSR-303)簡單實現(xiàn)方式

    這篇文章主要介紹了Spring 校驗(validator,JSR-303)簡單實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java事務(wù)管理學習之JDBC詳解

    Java事務(wù)管理學習之JDBC詳解

    這篇文章主要介紹了Java事務(wù)管理學習之JDBC的相關(guān)資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • idea2020安裝MybatisCodeHelper插件的圖文教程

    idea2020安裝MybatisCodeHelper插件的圖文教程

    這篇文章主要介紹了idea2020安裝MybatisCodeHelper插件的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • springboot實現(xiàn)學生管理系統(tǒng)

    springboot實現(xiàn)學生管理系統(tǒng)

    這篇文章主要為大家詳細介紹了springboot實現(xiàn)學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 最簡單的java生成word文檔方法

    最簡單的java生成word文檔方法

    這篇文章主要介紹了java生成word文檔最簡單的方法,首先說明,使用該方法時,盡量不要使用wps新建word文檔,經(jīng)測試,手機不能兼容,出現(xiàn)很多格式問題,office則手機可以很好的兼容,所以,本文以office做教程
    2021-08-08
  • SpringBoot2.x 整合 AntiSamy防御XSS攻擊的簡單總結(jié)

    SpringBoot2.x 整合 AntiSamy防御XSS攻擊的簡單總結(jié)

    本文主要對SpringBoot2.x集成AntiSamy防御XSS攻擊進行簡單總結(jié),其中SpringBoot使用的2.4.5版本,通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2021-08-08

最新評論