springboot-assembly自定义打包全过程
springboot-assembly自定义打包
我们打包项目的时候,一般只会打出一个可执行jar。有时,这个jar需要附带启动脚本或外置配置文件。此时就可以使用assembly自定义打包。
使用场景:按照自己的需求将需要的文件打包到自己指定的目录。
配置maven-assembly-plugin
放到要打包的pom.xml中
<build> <finalName>sb-my</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--<version>1.3.3.RELEASE</version>--> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> <executions> <execution> <goals> <!--repackage,其在Maven的package生命周期阶段,能够将mvn package生成的软件包,再次打包为可执行的软件包,并将mvn package生成的软件包重命名为*.original--> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <!--appendAssemblyId: 打包后的包名是否包含assembly的id名,assembly的配置在后面介绍;--> <id>make-assembly</id> <!--execution:phase加入package后,则在执行maven package时就可以调用maven-assembly-plugin插件定义的打包方式。--> <phase>package</phase> <goals> <!--只执行一次--> <goal>single</goal> </goals> </execution> </executions> <configuration> <finalName>sb-my-${version}</finalName> <archive> <manifest> <mainClass>cn.demo.Application</mainClass> </manifest> </archive> <descriptors> <!--descriptor:引用的assembly配置文件,可以用多个,即可以同时打包多个格式的包;--> <descriptor>src/main/resources/assembly/assembly.xml</descriptor> </descriptors> <appendAssemblyId>false</appendAssemblyId> </configuration> </plugin> </plugins> </build>
自定义打包配置文件assembly.xml
在src/main/resources/assembly/下新建assembly.xml
<assembly> <id>assembly</id> <formats> <!-- 打包成tar.gz格式,如果需要打包成zip格式,填写zip --> <format>tar.gz</format> </formats> <!-- 如果为true,打包出来的文件结构第一层为pom.xml里面定义的artifactId-version --> <includeBaseDirectory>true</includeBaseDirectory> <!--fileSets 可以设置文件被打包的具体配置。可以过滤哪些文件被打包--> <fileSets> <fileSet> <!--资源目录--> <directory>doc</directory> <includes> <!--过滤哪些文件被打包--> <include>*.sh</include> </includes> <!-- 输出到当前目录 --> <outputDirectory>.</outputDirectory> <!-- 所有文件权限为755 --> <fileMode>0755</fileMode> </fileSet> <fileSet> <directory>doc/sql</directory> <includes> <include>*.sql</include> </includes> <outputDirectory>./doc/sql</outputDirectory> </fileSet> <fileSet> <directory>doc</directory> <includes> <include>*.docx</include> </includes> <outputDirectory>./doc</outputDirectory> </fileSet> <!-- <fileSet> <directory>src/main/resources/</directory> <includes> <include>application.properties</include> <include>java.env</include> </includes> <outputDirectory>.</outputDirectory> </fileSet>--> </fileSets> <files> <file> <source>src/main/resources/application.properties</source> <!--重命名文件--> <destName>config.properties</destName> <outputDirectory>.</outputDirectory> </file> <file> <!--jar文件是直接从target目录下拷贝然后输出到当前包中的--> <source>target/sb-my.jar</source> <outputDirectory>.</outputDirectory> </file> </files> </assembly>
效果
springboot基于assembly插件打包
assembly打包的优点
让springboot能够加载jar外的配置文件。
方便提供一个服务化的启动脚本。
pom定义
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <!--<configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration>--> <executions> <execution> <id>bundle</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <!-- <appendAssemblyId>false</appendAssemblyId> --> <descriptors> <descriptor>${basedir}/src/main/build/assembly.xml</descriptor> </descriptors> <!-- 打包后的文件生成目录--> <!--<outputDirectory>${project.build.directory}/dist/</outputDirectory>--> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <!--main启动类--> <mainClass>com.test.WorkWebApplication</mainClass> <!--生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/--> <classpathPrefix>lib/</classpathPrefix> <addClasspath>true</addClasspath> </manifest> <manifestEntries> <Class-Path>conf/</Class-Path> </manifestEntries> </archive> <!--过滤掉不希望包含在jar中的文件--> <excludes> <!-- 排除不需要的文件夹(路径是jar包内部的路径) --> <exclude>**/assembly/</exclude> </excludes> </configuration> </plugin> </plugins> </build>
说明:assembly.xml存放于src/main/build目录,此外用于发布的config、bin目录及文件都放到这个目录;
具体内容:
<assembly> <id>1.0</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!-- config files --> <fileSet> <directory>${basedir}/src/main/resources</directory> <outputDirectory>/conf</outputDirectory> <includes> <include>*.xml</include> <include>*.properties</include> <include>*.yml</include> <include>*.key</include> <include>dev/*</include> <include>prod/*</include> <include>test/*</include> </includes> <fileMode>0644</fileMode> <lineEnding>unix</lineEnding> </fileSet> <fileSet> <directory>target</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <!-- scripts --> <fileSet> <directory>${basedir}/src/main/build/bin</directory> <includes> <include>*.sh</include> </includes> <fileMode>0755</fileMode> <outputDirectory>/bin</outputDirectory> </fileSet> <!-- executable jar --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>${project.artifactId}-${project.version}.jar</include> <!--<include>${project.artifactId}-${project.version}-jar-with-dependencies.jar</include>--> </includes> <fileMode>0755</fileMode> </fileSet> </fileSets> </assembly>
执行mvn clean package -Dmaven.test.skip=true 打包
出现如下错误:
因为是在windows上执行assembly任务,但是它包含了相对于linux的路径。
解决方案:将outputDirectory节点直接闭合;或是在linux目录前加一个点。或者改成${file.separator}
因此将上述脚本改成如下
<assembly> <id>1.0</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!-- config files --> <fileSet> <directory>src/main/resources/</directory> <outputDirectory>${file.separator}conf</outputDirectory> </fileSet> <fileSet> <directory>bin/</directory> <outputDirectory>${file.separator}</outputDirectory> </fileSet> <fileSet> <directory>target</directory> <outputDirectory>${file.separator}</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <!-- scripts --> <fileSet> <directory>${basedir}/src/main/build/bin</directory> <includes> <include>*.sh</include> </includes> <fileMode>0755</fileMode> <outputDirectory>${file.separator}bin</outputDirectory> </fileSet> <!-- executable jar --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>${file.separator}</outputDirectory> <includes> <include>${project.artifactId}-${project.version}.jar</include> <!--<include>${project.artifactId}-${project.version}-jar-with-dependencies.jar</include>--> </includes> <fileMode>0755</fileMode> </fileSet> </fileSets> </assembly>
继续执行打包命令,格式如下:
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot中的@RequestMapping注解的用法示例
@RequestMapping注解是SpringBoot中最常用的注解之一,它可以帮助开发者定义和处理HTTP请求,本篇文章我们将详细为大家介绍如何使用SpringBoot中的@RequestMapping注解,感兴趣的同学跟着小编一起来学习吧2023-06-06Spring Boot 中的 @HystrixCommand 注解原理及使用方法
通过使用 @HystrixCommand 注解,我们可以轻松地实现对方法的隔离和监控,从而提高系统的可靠性和稳定性,本文介绍了Spring Boot 中的@HystrixCommand注解是什么,其原理以及如何使用,感兴趣的朋友跟随小编一起看看吧2023-07-07
最新评论