1. 程式人生 > >利用maven-assembly-plugin加載不同環境所需的配置文件

利用maven-assembly-plugin加載不同環境所需的配置文件

參考 oal 階段 配置 信息 需求 lifecycle ima jar

背景:

  如何加載不同環境的配置文件已經成了實在必行的,我們通常利用profile進行,詳情參見我上篇博客 http://www.cnblogs.com/lianshan/p/7347890.html,但是單單的profile實在無法滿足我們的需求,我們將它與maven-assembly-plugin,結合起來,來實現配置分離的問題。

profile不同環境參數設置

  此參數的配置與上篇幾乎相同,不過是在properties的屬性聲明將其與assembly結合起來,具體示例如下。

<profiles>
        <profile>
            <id>
dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env.devMode>dev</env.devMode> <skipAssemblyDEV>true</skipAssemblyDEV> <
skipAssemblyUAT>false</skipAssemblyUAT> <skipAssemblyPROD>false</skipAssemblyPROD> </properties> </profile> <profile> <id>uat</id> <activation> <activeByDefault>
false</activeByDefault> </activation> <properties> <env.devMode>uat</env.devMode> <skipAssemblyDEV>false</skipAssemblyDEV> <skipAssemblyUAT>true</skipAssemblyUAT> <skipAssemblyPROD>false</skipAssemblyPROD> </properties> </profile> </profiles>

我定義了三個環境的相同的四個變量,我在mvn clean package -P dev 時指定了環境信息,也就相對於制定了變量的值。

可以觀察到  skipAssemblyDEV 、skipAssemblyUAT、 skipAssemblyPROD 這三個值都是排他的,聰明的小夥伴已經可能意識到了,一定有三個地方使用了這三個變量,並將他們指向了指定的配置文件。

好我們來觀察assembly的相關配置。

<!--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加載不同環境所需配置文件。

具體的構建完成包結構信息如下,關於target 此塊只是tar.gz 與assembly有關,其余的為其他插件使用生成,生成的包結構亦如下:

技術分享  技術分享

最詳細的信息請參考apache官網:https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html



  

利用maven-assembly-plugin加載不同環境所需的配置文件