始终很好奇开源软件的启动脚本是怎么写进去的,直到最近看到seata pox.xml 才理解到有个appassembler-maven-plugi插件非常简单。能够用过简略xml配置,就能够将我的项目打包并且生成多个平台的启动脚本,非常简单、实用,上面我全方位带大家去了如何去应用它。

介绍插件

依据官网介绍,这个插件次要用于生成启动 java 应用程序的脚本,能将我的项目依赖jar可能打包目录中,并且它们退出启动脚本类门路中。
反对平台

  • Unix-variants
  • Windows NT (Windows 9x is NOT supported)
  • Java Service Wrapper (JSW) 能够很不便得在各个平台(windows,linux,max os)治理Java过程,治理JVM,启动进行,开机启动,治理内存溢出的异样 标准版还能够发谬误日志email,检测死锁。官网阐明

次要命令:
appassembler:assemble 打包我的项目并且配置bin 启动脚本,能够了解为应用相似spring-boot-maven-plugin打包进去我的项目,能够通过java -jar 形式启动我的项目,然而不反对stop、satus、restart这些操作,比拟原始。
appassembler:create-repository 创立一个 appassembler 存储库,就是将工程打成jar
appassembler:generate-daemons 生成基于 JSW 的守护过程包装器,大多数人都是应用这个,重点讲。

appassembler:assemble

 <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>appassembler-maven-plugin</artifactId>                <version>2.1.0</version>                <configuration>                    <platforms>                        <platform>unix</platform>                        <platform>windows</platform>                    </platforms>                    <!--包的寄存路劲-->                    <assembleDirectory>${project.build.directory}/${project.name}</assembleDirectory>                    <repositoryName>lib</repositoryName>                    <!--启动脚本目录-->                    <binFolder>bin</binFolder>                    <!--配置文件门路-->                    <configurationDirectory>conf</configurationDirectory>                    <!--是否copy配置文件-->                    <copyConfigurationDirectory>true</copyConfigurationDirectory>                    <!--从哪里copy配置文件-->                    <configurationSourceDirectory>src/main/resources</configurationSourceDirectory>                    <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>                    <!--flag 示意间接将jar放到lib 外面-->                    <repositoryLayout>flat</repositoryLayout>                    <encoding>UTF-8</encoding>                    <logsDirectory>logs</logsDirectory>                    <tempDirectory>tmp</tempDirectory>                    <programs>                        <program>                            <mainClass>tk.shenyifeng.demo.DemoApplication</mainClass>                            <id>demo</id>                            <jvmSettings>                                <extraArguments>                                    <extraArgument>-server</extraArgument>                                    <extraArgument>-Xms256M</extraArgument>                                    <extraArgument>-Xmx256M</extraArgument>                                    <extraArgument>-Xss512k</extraArgument>                                    <extraArgument>-Xloggc:@BASEDIR@/logs/demo_gc.log</extraArgument>                                    <extraArgument>-verbose:gc</extraArgument>                                    <extraArgument>-XX:+HeapDumpOnOutOfMemoryError</extraArgument>                                    <extraArgument>-XX:HeapDumpPath=@BASEDIR@/logs/java_heapdump.hprof</extraArgument>                                </extraArguments>                            </jvmSettings>                        </program>                    </programs>                </configuration>            </plugin>

运行命令 mvn package appassembler:assemble 就能够在target/工程名/下看见打包好的工程,进入bin 目录能够看见有unix、windows 两大平台的运行脚本,然而这个脚本只有启动工程,不反对stop、restart 。

appassembler:generate-daemons

这个才是最重要的命令,应用插件都是应用这个生成能够启动、进行的脚本,不便在多个平台部署。上面间接贴上xml

            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>appassembler-maven-plugin</artifactId>                <version>2.1.0</version>                <configuration>                    <platforms>                        <platform>unix</platform>                        <platform>windows</platform>                    </platforms>                    <!--包的寄存路劲-->                    <assembleDirectory>${project.build.directory}/${project.name}</assembleDirectory>                    <repositoryName>lib</repositoryName>                    <!--启动脚本目录-->                    <binFolder>bin</binFolder>                    <!--配置文件门路-->                    <configurationDirectory>conf</configurationDirectory>                    <!--是否copy配置文件-->                    <copyConfigurationDirectory>true</copyConfigurationDirectory>                    <!--从哪里copy配置文件-->                    <configurationSourceDirectory>src/main/resources</configurationSourceDirectory>                    <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>                    <binFileExtensions>                        <unix>.sh</unix>                        <windows>.bat</windows>                    </binFileExtensions>                    <!--flag 示意间接将jar放到lib 外面-->                    <repositoryLayout>flat</repositoryLayout>                    <encoding>UTF-8</encoding>                    <logsDirectory>logs</logsDirectory>                    <tempDirectory>tmp</tempDirectory>                    <daemons>                        <daemon>                            <id>demo</id>                            <mainClass>tk.shenyifeng.demo.DemoApplication</mainClass>                            <platforms>                                <platform>jsw</platform>                            </platforms>                             能够通过generatorConfigurations 设置脚本平台                            <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>                                    </includes>                                </generatorConfiguration>                            </generatorConfigurations>                            <jvmSettings>                                <extraArguments>                                    <extraArgument>-server</extraArgument>                                    <extraArgument>-Xms256M</extraArgument>                                    <extraArgument>-Xmx256M</extraArgument>                                    <extraArgument>-Xss512k</extraArgument>                                    <extraArgument>-Xloggc:logs/demo_gc.log</extraArgument>                                    <extraArgument>-verbose:gc</extraArgument>                                    <extraArgument>-XX:+HeapDumpOnOutOfMemoryError</extraArgument>                                    <extraArgument>-XX:HeapDumpPath=logs/java_heapdump.hprof</extraArgument>                                </extraArguments>                                >                            </jvmSettings>                        </daemon>                    </daemons>                    <programs>                        <program>                            <mainClass>tk.shenyifeng.demo.DemoApplication</mainClass>                            <id>demoApp</id>                        </program>                    </programs>                </configuration>            </plugin>

在命令行执行

mvn clean package appassembler:generate-daemons

打包的文件夹在参数 target 设置,如果没有默认值 ${project.build.directory}/generated-resources/appassembler , 所以生成文件假放在 target\generated-resources\appassembler\ 下。
留神在windows 下执行改脚本须要应用管理员,不然会呈现

wrapper | OpenSCManager failed - 回绝拜访。 (0x5)

首先执行 ,装置服务

demo.bat install

装置胜利,就能够应用 start 运行服务了

demo.bat start

留神这个插件和spring-boot-maven-plugin 不兼容,会呈现无奈找到主类异样,次要还是因为可执行的jar格局在BOOT-INF/classes中打包应用程序类。这意味着当可执行jar用作依赖项时,无奈找到它们。遇到这种状况在打包是,去除spring-boot-maven-plugin 即可

我的项目曾经放在GitHub,有趣味本人去查看。