背景:html
如何加載不一樣環境的配置文件已經成了勢在必行的,咱們一般利用profile進行,詳情參見我上篇博客 http://www.cnblogs.com/lianshan/p/7347890.html,可是單單的profile實在沒法知足咱們的需求,由於這實在是太簡單太單一了,咱們將它與maven-assembly-plugin,結合起來,來實現配置分離的問題。apache
profile不一樣環境參數設置maven
此參數的配置與上篇幾乎相同,不過是在properties的屬性聲明將其與assembly結合起來,具體示例以下。ide
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env.devMode>dev</env.devMode> <skipAssemblyDEV>false</skipAssemblyDEV> <skipAssemblyUAT>true</skipAssemblyUAT> <skipAssemblyPROD>true</skipAssemblyPROD> </properties> </profile> <profile> <id>uat</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <env.devMode>uat</env.devMode> <skipAssemblyDEV>true</skipAssemblyDEV> <skipAssemblyUAT>false</skipAssemblyUAT> <skipAssemblyPROD>true</skipAssemblyPROD> </properties> </profile> </profiles>
我定義了三個環境的相同的四個變量,我在mvn clean package -P dev 時指定了環境信息,也就相對於制定了變量的值。ui
能夠觀察到 skipAssemblyDEV 、skipAssemblyUAT、 skipAssemblyPROD 這三個值都是排他的,聰明的小夥伴已經可能意識到了,必定有三個地方使用了這三個變量,並將他們指向了指定的配置文件。this
好咱們來觀察assembly的相關配置。spa
<!--assembly test start--> <!--this include the xml assembly--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>make-assembly-dev</id> <!--The identifier of this execution for labelling the goals during the build 標誌--> <phase>package</phase> <!--The build lifecycle phase to bind the goals in this execution to 指定其編譯階段--> <goals> <goal>single</goal> <!--The goals to execute with the given configuration--> </goals> <configuration> <skipAssembly>${skipAssemblyDEV}</skipAssembly> <!--profile聲明參數調用--> <descriptors> <descriptor>src/main/assembly/dev/assembly.xml</descriptor> <!--加載指定的assembly配置文件--> </descriptors> </configuration> </execution> <execution> <id>make-assembly-uat</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <skipAssembly>${skipAssemblyUAT}</skipAssembly> <descriptors> <descriptor>src/main/assembly/uat/assembly.xml</descriptor> </descriptors> </configuration> </execution> <execution> <id>make-assembly-prod</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <skipAssembly>${skipAssemblyPROD}</skipAssembly> <descriptors> <descriptor>src/main/assembly/prod/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <!--assembly test end-->
首先咱們經過 profile聲明瞭相應的參數。插件
skipAssembly 的解釋以下,其聲明值true和false 代表咱們是否要執行下列聲明的配置文件
Flag allowing one or more executions of the assembly plugin to be configured as skipped for a particular build.
This makes the assembly plugin more controllable from profiles.
此時咱們再來觀察下assembly.xml的相關配置文件。
<assembly> <id>assembly-${env.devMode}</id> <!--輸出文件名--> <formats> <format>tar.gz</format> <!--打包文件結構--> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/assembly/bin</directory> <!-- 項目文件目錄--> <outputDirectory>bin</outputDirectory> <!--生成bin目錄--> <directoryMode>0755</directoryMode> <!--目錄執行權限--> <fileMode>0755</fileMode><!--文件執行權限--> </fileSet> <fileSet> <directory>src/main/assembly/uat/conf</directory> <outputDirectory>conf</outputDirectory> <directoryMode>0744</directoryMode> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>lib</directory> <outputDirectory>lib</outputDirectory> <directoryMode>0744</directoryMode> <fileMode>0644</fileMode> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory><!-- 依賴jar包放置目錄--> </dependencySet> </dependencySets> </assembly>
那麼這就是一個完整的利用assembly加載不一樣環境所需配置文件。code
具體的構建完成包結構信息以下,關於target 此塊只是tar.gz 與assembly有關,其他的爲其餘插件使用生成,生成的包結構亦以下:orm
最詳細的信息請參考apache官網:https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html