概述
前言
通常为了更方便的部署我们的服务,我们会使用一些打包插件将编译后的代码进行打包。这里介绍两个常用的对Spring Boot 项目打包的插件,appassembler-maven-plugin,以及maven-assembly-plugin。
appassembler-maven-plugin
appassembler-maven-plugin插件可以对项目进行打包并且生成可执行的shell脚本文件。使用的方法十分简单,在pom.xml文件中引入插件:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<configurationDirectory>conf</configurationDirectory>
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<useWildcardClassPath>true</useWildcardClassPath>
<repositoryLayout>flat</repositoryLayout>
<repositoryName>lib</repositoryName>
<binFileExtensions>
<unix>.sh</unix>
</binFileExtensions>
<daemons>
<daemon>
<mainClass>启动类的路径</mainClass>
<commandLineArguments>
<commandLineArgument>argument_one</commandLineArgument>
</commandLineArguments>
<!-- JVM参数设置 -->
<jvmSettings>
<maxMemorySize>2048</maxMemorySize>
</jvmSettings>
<!-- 生成后台程序 -->
<generatorConfigurations>
<generatorConfiguration>
<generator>jsw</generator>
<includes>
<include>linux-x86-32</include>
<include>linux-x86-64</include>
<include>windows-x86-32</include>
<include>windows-x86-64</include>
<include>macosx-universal-32</include>
<include>macosx-universal-64</include>
</includes>
<configuration>
<property>
<name>configuration.directory.in.classpath.first</name>
<value>conf</value>
</property>
<property>
<name>wrapper.logfile</name>
<value>logs/日志文件名称</value>
</property>
<property>
<name>wrapper.console.format</name>
<value>PTM</value>
</property>
<property>
<name>wrapper.logfile.format</name>
<value>PTM</value>
</property>
<property>
<name>wrapper.logfile.maxsize</name>
<value>10m</value>
</property>
<property>
<name>wrapper.logfile.maxfiles</name>
<value>5</value>
</property>
</configuration>
</generatorConfiguration>
</generatorConfigurations>
<platforms>
<platform>jsw</platform>
</platforms>
<id>名称</id>
</daemon>
</daemons>
</configuration>
<executions>
<execution>
<id>build-deploy-dir</id>
<phase>package</phase>
<goals>
<goal>generate-daemons</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
maven-assembly-plugin
maven-assembly-plugin 可以将代码进行压缩。同样需要在pom.xml中引入插件;
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<executions>
<execution>
<!-- 将打包绑定到maven的package阶段-->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>名称</finalName>
<appendAssemblyId>false</appendAssemblyId>
<!-- 解释器,指定解释文件用来指示详细打包动作 -->
<descriptors>
<!-- 解释文件的路径 -->
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
在resources文件夹下新建assembly.xml文件:
<assembly
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<!-- 压缩包的格式 -->
<formats>
<format>tar.gz</format>
</formats>
<!-- 指定文件集 -->
<fileSets>
<fileSet>
<!-- 目标路径 -->
<directory>target/generated-resources/appassembler/jsw/名称/lib</directory>
<!-- 输出路径 -->
<outputDirectory>lib</outputDirectory>
<excludes>
<exclude>maven-metadata-appassembler.xml</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>target/generated-resources/appassembler/jsw/名称/conf</directory>
<outputDirectory>conf</outputDirectory>
<filtered>true</filtered>
<includes>
<include>wrapper.conf</include>
</includes>
</fileSet>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>conf</outputDirectory>
<filtered>true</filtered>
<excludes>
<exclude>com/</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>target/generated-resources/conf</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<fileSet>
<directory>target/generated-resources/appassembler/jsw/名称/bin</directory>
<outputDirectory>bin</outputDirectory>
<!-- linux文件的权限 -->
<fileMode>0755</fileMode>
<lineEnding>keep</lineEnding>
</fileSet>
<fileSet>
<directory>src/main/shell</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<lineEnding>keep</lineEnding>
</fileSet>
<fileSet>
<directory>target/generated-resources/appassembler/jsw/名称/bin</directory>
<outputDirectory>logs</outputDirectory>
<excludes>
<exclude>*</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
19.08.30更新
appassembler-maven-plugin 插件中如何配置JVM参数:
默认可以通过initialMemorySize,配置内存的初始大小, maxMemorySize配置分配的最大的内存,maxStackSize配置分配的栈的最大的空间。其余JVM参数可以配置在extraArguments下。样例如下:
<jvmSettings>
<initialMemorySize>20M</initialMemorySize>
<maxMemorySize>200M</maxMemorySize>
<maxStackSize>128M</maxStackSize>
<extraArguments>
<extraArgument>-DXms=4096</extraArgument>
<extraArgument>-DXmn=1536</extraArgument>
<extraArgument>-DXX:SurvivorRatio=6</extraArgument>
<extraArgument>-Djava.net.preferIPv4Stack=true</extraArgument>
<extraArgument>-Djava.rmi.server.hostname=ip</extraArgument>
<extraArgument>-Dcom.sun.management.jmxremote</extraArgument>
<extraArgument>-Dcom.sun.management.jmxremote.port=port</extraArgument>
<extraArgument>-Dcom.sun.management.jmxremote.authenticate=false</extraArgument>
<extraArgument>-Dcom.sun.management.jmxremote.ssl=false</extraArgument>
</extraArguments>
</jvmSettings>
更多配置参考appassembler-maven-plugin文档
最后
以上就是顺利睫毛为你收集整理的Spring Boot 的打包插件使用的全部内容,希望文章能够帮你解决Spring Boot 的打包插件使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复