appassemblermaven插件可以自动生成跨平台的启动脚本,省去了手工写脚本的麻烦,而且还可以生成jsw的后台运行程序。插件官网:http://www.mojohaus.org/appassembler/appassembler-maven-plugin/,官网介绍了该插件的详细用法。
appassembler的使用很简单,直接在pom文件中加入插件配置。
生成启动脚本之前,需要有一个启动的类:
1 package org.cellphone; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.context.annotation.ComponentScan; 6 7 @EnableAutoConfiguration 8 @ComponentScan("org.cellphone") 9 public class WarehouseWeb { 10 11 public static void main(String[] args) { 12 SpringApplication.run(WarehouseWeb.class, args); 13 } 14 }
1. 生成可执行的启动脚本
1 <plugin> 2 <groupId>org.codehaus.mojo</groupId> 3 <artifactId>appassembler-maven-plugin</artifactId> 4 <version>2.0.0</version> 5 <executions> 6 <execution> 7 <id>Make Assembly</id> 8 <phase>package</phase> 9 <goals> 10 <goal>assemble</goal> 11 </goals> 12 </execution> 13 </executions> 14 15 <configuration> 16 <!-- 打包的jar,以及maven依赖的jar放到这个目录里面 --> 17 <repositoryName>lib</repositoryName> 18 <!-- 可执行脚本的目录 --> 19 <binFolder>bin</binFolder> 20 <!-- 配置文件的目标目录 --> 21 <configurationDirectory>conf</configurationDirectory> 22 <!-- 拷贝配置文件到上面的目录中 --> 23 <copyConfigurationDirectory>true</copyConfigurationDirectory> 24 <!-- 从哪里拷贝配置文件 (默认src/main/config) --> 25 <configurationSourceDirectory>src/main/resources</configurationSourceDirectory> 26 <!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录 --> 27 <repositoryLayout>flat</repositoryLayout> 28 <encoding>UTF-8</encoding> 29 <logsDirectory>logs</logsDirectory> 30 <!-- 生成脚本的后缀 --> 31 <binFileExtensions> 32 <unix>.sh</unix> 33 </binFileExtensions> 34 <!-- 生成linux, windows两种平台的执行脚本 --> 35 <platforms> 36 <platform>windows</platform> 37 <platform>unix</platform> 38 </platforms> 39 40 <programs> 41 <program> 42 <!--指定主类,脚本名。会生成shell/bat两种类型,也可用platforms指定运行平台--> 43 <mainClass>org.cellphone.WarehouseWeb</mainClass> 44 <!-- 生成的脚本文件的名称,比如start.sh,你也可以根据你的需要命名成其他名字 --> 45 <name>WAREHOUSE-WEB</name> 46 </program> 47 </programs> 48 </configuration> 49 </plugin>
执行 mvn package appassembler:assemble,执行完成之后,在target/appassembler目录就有可执行脚本。
2. 生成后台服务程序
1 <plugin> 2 <groupId>org.codehaus.mojo</groupId> 3 <artifactId>appassembler-maven-plugin</artifactId> 4 <version>2.0.0</version> 5 <executions> 6 <execution> 7 <id>generate-jsw-scripts</id> 8 <phase>package</phase> 9 <goals> 10 <goal>generate-daemons</goal> 11 </goals> 12 </execution> 13 </executions> 14 15 <configuration> 16 <!-- 打包的jar,以及maven依赖的jar放到这个目录里面 --> 17 <repositoryName>lib</repositoryName> 18 <!-- 可执行脚本的目录 --> 19 <binFolder>bin</binFolder> 20 <!-- 配置文件的目标目录 --> 21 <configurationDirectory>conf</configurationDirectory> 22 <!-- 拷贝配置文件到上面的目录中 --> 23 <copyConfigurationDirectory>true</copyConfigurationDirectory> 24 <!-- 从哪里拷贝配置文件 (默认src/main/config) --> 25 <configurationSourceDirectory>src/main/resources</configurationSourceDirectory> 26 <!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录 --> 27 <repositoryLayout>flat</repositoryLayout> 28 <encoding>UTF-8</encoding> 29 <logsDirectory>logs</logsDirectory> 30 <daemons> 31 <daemon> 32 <id>WAREHOUSE-WEB</id> 33 <mainClass>org.cellphone.WarehouseWeb</mainClass> 34 <platforms> 35 <platform>jsw</platform> 36 </platforms> 37 <generatorConfigurations> 38 <generatorConfiguration> 39 <generator>jsw</generator> 40 <includes> 41 <include>linux-x86-32</include> 42 <include>linux-x86-64</include> 43 <include>windows-x86-32</include> 44 <include>windows-x86-64</include> 45 </includes> 46 <configuration> 47 <property> 48 <name>configuration.directory.in.classpath.first</name> 49 <value>conf</value> 50 </property> 51 <property> 52 <name>wrapper.ping.timeout</name> 53 <value>120</value> 54 </property> 55 <property> 56 <name>set.default.REPO_DIR</name> 57 <value>lib</value> 58 </property> 59 <property> 60 <name>wrapper.logfile</name> 61 <value>logs/wrapper.log</value> 62 </property> 63 </configuration> 64 </generatorConfiguration> 65 </generatorConfigurations> 66 <jvmSettings> 67 <!-- jvm参数 --> 68 <systemProperties> 69 <systemProperty>com.sun.management.jmxremote</systemProperty> 70 <systemProperty>com.sun.management.jmxremote.port=1984</systemProperty> 71 <systemProperty>com.sun.management.jmxremote.authenticate=false</systemProperty> 72 <systemProperty>com.sun.management.jmxremote.ssl=false</systemProperty> 73 </systemProperties> 74 <extraArguments> 75 <extraArgument>-server</extraArgument> 76 </extraArguments> 77 </jvmSettings> 78 </daemon> 79 </daemons> 80 </configuration> 81 </plugin>
执行mvn clean package appassembler:generate-daemons,执行完成之后,在target\generated-resources\appassembler\jsw\目录里面就有后台运行的程序。