Maven-assemble打包使用案例

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

项目结构目录如下:
[/size]

[img]http://dl2.iteye.com/upload/attachment/0113/2161/1ba1923b-00cd-3f52-9c5b-45a5ad64527f.png[/img]

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

<?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>


[size=medium]
然后在主pom.xml中,加入如下一段内容:
[/size]


<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>


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

[img]http://dl2.iteye.com/upload/attachment/0113/2163/844be858-3b20-3bb5-9620-f49fb44abb02.png[/img]

[size=medium]
将zip包解压后,如下:
[/size]


[img]http://dl2.iteye.com/upload/attachment/0113/2165/0667fa6f-64dc-3cc5-93a8-488f3b74c389.png[/img]


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

参考资料:
[url]http://www.cnblogs.com/JerryWang1991/p/3936378.html[/url]
[url]http://skydream.iteye.com/blog/437937[/url]
[url]http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html[/url]
[/size]
[b][color=green][size=large]
最后欢迎大家扫码关注微信公众号:我是攻城师(woshigcs),我们一起学习,进步和交流!(woshigcs)
本公众号的内容是有关搜索和大数据技术和互联网等方面内容的分享,也是一个温馨的技术互动交流的小家园,有什么问题随时都可以留言,欢迎大家来访!
[/size][/color][/b]
[img]http://dl2.iteye.com/upload/attachment/0104/9948/3214000f-5633-3c17-a3d7-83ebda9aebff.jpg[/img]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值