美文网首页我爱编程
利用assembly插件分环境打包配置文件

利用assembly插件分环境打包配置文件

作者: Nisus_Liu | 来源:发表于2018-05-24 22:35 被阅读0次

首先, 郑重的吐槽下度娘, 能找到的资料, 都是说针对打包tar.gz的, 招了好一会儿, 才找到针对war包.
开始并不顺利, 因为在父子模块的场景下, 有war,有jar. 最后发现打包的lib里包含war包. 这样不是重复打包吗, 而且, 并不能打成标准war包结构.

好啦, 现在上正解.

工程结构:

parent          //pom
    | common   //jar
    | dao      //jar
    | web      //war

针对中这种结构的工程, 网上的tar.gz的做法并不适用. 主要是: 1)会将web模块的war包打进lib文件中; 2)目录结构不是war包应有的结构. 前者是因为maven自带的编译打包工具会先执行打包操作, 这样轮到assembly打包时会将打好的war包打进lib文件夹中.

针对上文的工程结构, 需要:

  1. parentpom中统一配置自带的编译插件. 主要是指定编译的jdk版本.(否则默认会使用1.5的. 不会的需要补充学习下).
  2. jar模块如果需要分环境打包配置文件, 则不需要任何多余的配置, 使用的默认的jar打包即可.
  3. web模块是重点. 下文的配置都是针对web的配置. 通常web工程更多的需要分环境打包配置文件.

web配置文件结构

1527171764196.png

我这里的思路是: resources下可以放置每个环境都需要的公共配置文件. 分环境的放在./src/main/assembly/下.

1 web模块的pom.xml中的profiles配置

<profiles>
        <!--profile1: 开发环境 dev-->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>env</name>
                    <value>dev</value>
                </property>
            </activation>

            <!--定制键值对环境变量
            !重要: 这里才是将env=dev的变量传入下面的assembly.xml中, 用于给${env}赋值, 上面的
<property>下面的那name,value并没有什么乱用, 我也不知道别人为什么用这个!-->
            <properties>
                <env>dev</env>
            </properties>

            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <configuration>
                            <!-- maven assembly插件需要一个描述文件 来告诉插件包的结构以及打包所需的文件来自哪里 -->
                            <descriptors>
                                <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
                            </descriptors>
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

        </profile>
        <!--profile2: 测试环境 test-->
        <profile>
            <id>test</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>test</value>
                </property>
            </activation>

            <properties>
                <env>test</env>
            </properties>

            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <configuration>
                            <!-- 发布模式使用的maven assembly插件描述文件 -->
                            <descriptors>
                                <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
                            </descriptors>
                            <!-- 如果一个应用的包含多个deploy模块,如果使用同样的包名, 如果把它们复制的一个目录中可能会失败,所以包名加了 artifactId以示区分 -->
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <!-- scm 要求 release 模式打出的包放到顶级目录下的target子目录中 -->
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <!--prod环境略-->

2 web模块的pom.xml中引入assembly插件

<build>
        <plugins>
            <!-- deploy模块的packaging通常是jar,如果项目中没有java 源代码或资源文件,加上这一段配置使项目能通过构建 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>true</addMavenDescriptor>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <!-- 这是最新版本,推荐使用这个版本 -->
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>assemble</id>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <!--绑定package生命周期-->
                        <phase>package</phase>
                    </execution>
                </executions>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId> <!-- 设为 FALSE, 防止 WAR 包名加入 assembly.xml 中的 ID -->
                    <attach>false</attach>
                </configuration>
            </plugin>
        </plugins>
    </build>

3 配置定制assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>web</id>
    <formats>
        <format>war</format>
        <!--生成同结构的目录, 方便查看, 可选-->
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <!--打war包的文件配置-->
    <fileSets>
        <fileSet>
            <directory>${project.build.outputDirectory}</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>
        <!--分环境打包配置文件-->
        <!-- ${env} 的值由 -P 的参数传递进来, 如:-Pdev, 那么, ${env} 的值就是 dev -->
        <fileSet>
            <directory>${project.basedir}/src/main/assembly/${env}</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>
        <!--公共配置文件-->
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>
        <!-- 将 webapp 下的文件输出到 WAR 包 -->
        <fileSet>
            <directory>${project.basedir}/src/main/webapp</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>


    <dependencySets>
        <!-- 将项目依赖的JAR包输出到 WEB-INF/lib -->
        <dependencySet>
            <outputDirectory>WEB-INF/lib</outputDirectory>
            <!--经测试可以不需要-->
            <!--<excludes>-->
                <!--<exclude>*.war</exclude>-->
            <!--</excludes>-->
            <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
    </dependencySets>
</assembly>

这里采用的方案是, 不同环境公用同一个assembly.xml文件.

4 执行打包的命令

各环境对应的命令:

mvn clean package
mvn clean package -P dev
mvn clean package -P test
mvn clean package -P prod

后记

分环境打包的分支控制策略有两种:

  1. 通过profile配置不同的环境下的配置, 然后打包的时候通过参数-P 环境id定位到对应profile, 就会执行该profile下的所有配置, 包括自定义的properties(这里的propertiesprofile外层的效果一样). 本文采用的就是这个思路.
    这种方案的好处是, 可以方便的定义默认打包环境.

  2. 不同环境的配置文件分别放在不同的路径下, 打包配置中用${env}动态执行路径, 然后打包时带上参数: -Denv=环境名称, 即动态手动指定环境名, 定位到对应路径下的配置文件. 这种方案看似简单好实施, 但有个缺陷就是不方便执行默认打包环境.

最后, 再次重复一个坑点:
下图中两种property效果完全不同的, 上面的我完全不知道有什么鸟用, 下面的才是关键.

properties

相关文章

网友评论

    本文标题:利用assembly插件分环境打包配置文件

    本文链接:https://www.haomeiwen.com/subject/qxuejftx.html