在开发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>
<?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>
<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