zoukankan      html  css  js  c++  java
  • 利用maven-assembly-plugin加载不同环境所需的配置文件及使用场景

    背景:

      如何加载不同环境的配置文件已经成了势在必行的,我们通常利用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>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 时指定了环境信息,也就相对于制定了变量的值。

    可以观察到  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



      

  • 相关阅读:
    62. Unique Paths
    24. Swap Nodes in Pairs
    83. Remove Duplicates from Sorted List
    21. Merge Two Sorted Lists
    141. Linked List Cycle
    268. Missing Number
    191. Number of 1 Bits
    231. Power of Two
    9. Palindrome Number
    88. Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/lianshan/p/7348093.html
Copyright © 2011-2022 走看看