日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

004-集成maven和Spring boot的profile功能打包

發(fā)布時(shí)間:2023/12/14 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 004-集成maven和Spring boot的profile功能打包 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考地址:https://blog.csdn.net/lihe2008125/article/details/50443491

一、主要目標(biāo)

  1、通過(guò)mvn在命令行中打包時(shí),可以指定相應(yīng)的profile。

  2、需使用了maven的profile功能

  3、使用了Spring Boot的profile功能

二、profile簡(jiǎn)介

  什么是profile,解決什么問(wèn)題呢?  

  一般在開(kāi)發(fā)項(xiàng)目的時(shí)候要有多個(gè)環(huán)境,如開(kāi)發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境,配置文件一般不同。當(dāng)要向各個(gè)環(huán)境發(fā)布程序時(shí),需要人工處理這些配置文件。有了profile,只要在maven打包時(shí)使用下面命令即可。

mvn clean package -Dmaven.test.skip=true -P prod

  -P prod 就是告訴maven要使用名字為prod的profile來(lái)打包,即所有的配置文件都使用生產(chǎn)環(huán)境(prod:生產(chǎn);test:測(cè)試;dev:開(kāi)發(fā))。

?三、實(shí)現(xiàn)過(guò)程

  maven支持profile功能,當(dāng)使用maven profile打包時(shí),可以打包指定目錄和指定文件,且可以修改文件中的變量。

  spring boot也支持profile功能,只要在application.properties文件中指定spring.profiles.active=xxx 即可,其中xxx是一個(gè)變量,當(dāng)maven打包時(shí),修改這個(gè)變量即可。

3.1、父pom增加profile配置

在maven的父pom中增加如下配置 : <profiles><profile><id>dev</id><properties><profileActive>dev</profileActive></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><profileActive>test</profileActive></properties></profile><profile><id>prod</id><properties><profileActive>properties</profileActive></properties></profile></profiles>

方式一、基本打包

測(cè)試環(huán)境:springboot 1.5.1下

在build中配置,下文 中會(huì)有詳細(xì)解釋

<resources><resource><directory>src/main/resources</directory><filtering>true</filtering><excludes><exclude>application-dev.properties</exclude><exclude>application-test.properties</exclude><exclude>application-produce.properties</exclude></excludes></resource><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>application.properties</include><include>application-${profiles.active}.properties</include><include>**/*.xml</include></includes></resource><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources>

?

因可能版本較低,需要配置?maven-resources-plugin,否則變量不替換,但是更換了springboot 2.0.4后不用配置既可以

<plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><delimiters><!-- 使用${..}作為占位符 --><delimiter>${*}</delimiter></delimiters><!-- 使用默認(rèn)的占位符(@..@) --><useDefaultDelimiters>true</useDefaultDelimiters></configuration></plugin></plugins>

?

方式二、自定義配置的打包

1、配置好:assembly/depolyment.xml

<?xml version="1.0" encoding="UTF-8"?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"><id>dist</id><formats><format>zip</format><format>dir</format></formats><includeBaseDirectory>false</includeBaseDirectory><fileSets><fileSet><directory>src/main/bin</directory><outputDirectory>bin/</outputDirectory></fileSet><!--<fileSet>--><!--<directory>src/main/resources</directory>--><!--<outputDirectory>/</outputDirectory>--><!--</fileSet>--><fileSet><directory>${project.build.directory}</directory><outputDirectory>/</outputDirectory><includes><include>*.jar</include></includes></fileSet></fileSets><dependencySets><dependencySet><outputDirectory>lib</outputDirectory><scope>runtime</scope><excludes><exclude>${groupId}:${artifactId}</exclude></excludes></dependencySet></dependencySets> </assembly> View Code

2、多環(huán)境配置

方式2.1、文件后綴方式

  在需要打包的子項(xiàng)目pom設(shè)置

<build><resources><resource><!--指定打包時(shí)需要特殊處理的目錄文件--><directory>src/main/resources</directory><!--處理文件是時(shí),需要變量替換--><filtering>true</filtering><!--打包時(shí),排除文件--><excludes><exclude>application.properties</exclude><exclude>application-dev.properties</exclude><exclude>application-test.properties</exclude><exclude>application-prod.properties</exclude></excludes></resource><resource><!--指定打包時(shí)需要特殊處理的目錄文件--><directory>src/main/resources</directory><!--處理文件是時(shí),需要變量替換--><filtering>true</filtering><!--打包時(shí),包含文件--><includes><include>application.properties</include><include>application-${profileActive}.properties</include></includes></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin><!--打jar包--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.6</version><configuration><archive><manifest><!--jar入口累--><mainClass>com.jd.bt.gateway.ZuulApplication</mainClass><!-- classpath路徑 --><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix></manifest><manifestEntries><Class-Path>./</Class-Path></manifestEntries></archive><excludes><!--注意從編譯結(jié)果目錄開(kāi)始算目錄結(jié)構(gòu)--><!--<exclude>/*.yml</exclude>--><!--<exclude>/*.properties</exclude>--><!--<exclude>/*.xml</exclude>--><!--<exclude>/*.txt</exclude>--></excludes></configuration></plugin><!--自定義打包--><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><finalName>${artifactId}</finalName><!-- not append assembly id in release file name --><appendAssemblyId>false</appendAssemblyId><descriptors><descriptor>src/main/assembly/depolyment.xml</descriptor></descriptors></configuration><executions><execution><id>dist</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>

  在resources配置管理boot 文件

  工程中有4個(gè)文件:

    application.properties, 包含通用配置的文件。文件中有spring.profiles.active=@profileActive@的屬性。spring boot的profile是通過(guò)spring.profiles.active屬性來(lái)配置的,這里的profileActive和上面coolpro工程中配置的profileActive屬性名要一致。這樣,在運(yùn)行mvn命令時(shí),maven就會(huì)幫我們將@profileActive@替換成指定的profile。

    application-dev.properties, 當(dāng)mvn -P dev時(shí), 需要打包這個(gè)文件。

    application-test.properties,?當(dāng)mvn -P test時(shí), 需要打包這個(gè)文件。

    application-prod.properties,?當(dāng)mvn -P prod時(shí), 需要打包這個(gè)文件。

  

application.properties

spring.profiles.active=@profileActive@ server.port=8040

application-dev.properties?

server.port=8050

……

方式2.2、以文件夾方式

  在需要打包的子項(xiàng)目pom設(shè)置

<build><resources><!--文件夾--><resource><!--指定打包時(shí)需要特殊處理的目錄文件--><directory>src/main/resources</directory><!--處理文件是時(shí),需要變量替換--><filtering>true</filtering><!--打包時(shí),排除文件--><excludes><exclude>dev_conf/*</exclude><exclude>test_conf/*</exclude><exclude>prod_conf/*</exclude></excludes></resource><resource><!--指定打包時(shí)需要特殊處理的目錄文件--><directory>src/main/resources/${profileActive}_conf</directory><!--處理文件是時(shí),需要變量替換--><filtering>true</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin><!--打jar包--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.6</version><configuration><archive><manifest><!--jar入口累--><mainClass>com.jd.bt.gateway.ZuulApplication</mainClass><!-- classpath路徑 --><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix></manifest><manifestEntries><Class-Path>./</Class-Path></manifestEntries></archive><excludes><!--注意從編譯結(jié)果目錄開(kāi)始算目錄結(jié)構(gòu)--><!--<exclude>/*.yml</exclude>--><!--<exclude>/*.properties</exclude>--><!--<exclude>/*.xml</exclude>--><!--<exclude>/*.txt</exclude>--></excludes></configuration></plugin><!--自定義打包--><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><finalName>${artifactId}</finalName><!-- not append assembly id in release file name --><appendAssemblyId>false</appendAssemblyId><descriptors><descriptor>src/main/assembly/depolyment.xml</descriptor></descriptors></configuration><executions><execution><id>dist</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>

  在resources

    

  log4j.xml:共有文件

  env.properties:

env.profile.active=@profileActive@

  注意:使用$符號(hào),不能替換,maven的maven-resources-plugin使用@替換的符號(hào)。

3.4、打包即可

mvn clean package -Dmaven.test.skip=true -P dev -e

?

轉(zhuǎn)載于:https://www.cnblogs.com/bjlhx/p/9092773.html

總結(jié)

以上是生活随笔為你收集整理的004-集成maven和Spring boot的profile功能打包的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。