`
qindongliang1922
  • 浏览: 2152448 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
7265517b-f87e-3137-b62c-5c6e30e26109
证道Lucene4
浏览量:116483
097be4a0-491e-39c0-89ff-3456fadf8262
证道Hadoop
浏览量:124716
41c37529-f6d8-32e4-8563-3b42b2712a50
证道shell编程
浏览量:58722
43832365-bc15-3f5d-b3cd-c9161722a70c
ELK修真
浏览量:70512
社区版块
存档分类
最新评论

Maven-assemble打包使用案例

    博客分类:
  • JAVA
阅读更多

在开发Maven项目中,我们可能有各种各样的打包需求,当然最简单的就是,把整个Application打包成一个jar,这是Spring-boot项目中,常见的打包项目,有些项目可能比较复杂,还需要打包一些外部的配置的文件,或者自定义的shell脚本或者bat命令等,这时候使用assemble命令就比较方便了。

项目结构目录如下:





除了标准的创建maven项目会自带
src/main/java
src/main/resource
src/test/java
外,我们还自定义了bin,conf,assemble目录,其中bin目录用来存放项目启动,停止的一些脚本,conf存放了一些
外部配置文件,外部文件比较灵活,比如参数改了,不需要重新编译,只需要重启这个项目即可,assemble存放了我们打包的描述文件package.xml,内容如下:

<?xml version="1.0"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <!--项目标识,设置的话,生成后的zip文件会加上此后缀-->
    <id></id>
    <!--打包格式-->
    <formats>
        <format>zip</format>
    </formats>
    <!--是否包含根目录文件夹-->
    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>

        <!--<fileSet>-->
            <!--<directory>${project.basedir}\src\main\resources</directory>-->
            <!--<outputDirectory>\</outputDirectory>-->
            <!--<includes>-->
                <!--<include>some/path</include>-->
            <!--</includes>-->
            <!--<excludes>-->
                <!--<exclude>some/path1</exclude>-->
            <!--</excludes>-->
        <!--</fileSet>-->

        <!--自定义文件描述集-->
        <fileSet>
            <!--自定义脚本目录打包-->
            <directory>${project.basedir}\src\main\bin</directory>
            <outputDirectory>\bin</outputDirectory>
            <includes>
                <include>*.*</include>
            </includes>
            <!--设置权限-->
            <fileMode>0755</fileMode>
        </fileSet>

        <fileSet>
            <!--外部配置文件打包-->
            <directory>${project.basedir}\src\main\config</directory>
            <outputDirectory>\config</outputDirectory>
            <includes>
                <include>*.*</include>
            </includes>
            <fileMode>0644</fileMode>
        </fileSet>
    </fileSets>

    <dependencySets>

        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!-- 将scope为runtime的依赖包打包到lib目录下。 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

 </assembly>



然后在主pom.xml中,加入如下一段内容:


  <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!--<appendAssemblyId>true</appendAssemblyId>-->
                    <descriptors> <!--描述文件路径-->
                        <descriptor>src/main/assemble/package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-zip</id>
                        <!-- 绑定到package生命周期阶段上 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 绑定到package生命周期阶段上 -->
                            <goal>single</goal>
                        </goals>

                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>



至此,我们在执行mvn clean package 时,就会发现target目录下,多了一个zip的压缩包:





将zip包解压后,如下:







我们就可以直接将压缩包,传到linux上执行脚本启动了,需要注意的是,在windows下编辑的脚本文件,在linux上可能执行不成功,大家可以下载一个dos2unix的工具,对脚本格式重新修改即可。

参考资料:
http://www.cnblogs.com/JerryWang1991/p/3936378.html
http://skydream.iteye.com/blog/437937
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html


最后欢迎大家扫码关注微信公众号:我是攻城师(woshigcs),我们一起学习,进步和交流!(woshigcs)
本公众号的内容是有关搜索和大数据技术和互联网等方面内容的分享,也是一个温馨的技术互动交流的小家园,有什么问题随时都可以留言,欢迎大家来访!

  • 大小: 13.2 KB
  • 大小: 2.8 KB
  • 大小: 2.1 KB
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics