使用appassembler-maven-plugin插件生成启动脚本

appassembler-maven-plugin可以自动生成跨平台的启动脚本,省去了手工写脚本的麻烦,而且还可以生成jsw的后台运行程序。

插件网址:http://www.mojohaus.org/appassembler/appassembler-maven-plugin/

appassembler的配置比较简单,在pom.xml的配置文件加入插件配置。


生成启动脚本之前,需要有一个启动的类,示例如下

package com.mall;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main
{
	public static void main(String[] args)
	{
		SpringApplication.run(Main.class, args);
	}
}

一:生成可执行的启动脚本

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>appassembler-maven-plugin</artifactId>
	<version>1.10</version>
	<configuration>
		<!-- 生成linux, windows两种平台的执行脚本 -->
		<platforms>
			<platform>windows</platform>
			<platform>unix</platform>
		</platforms>
		<!-- 根目录 -->
		<assembleDirectory>${project.build.directory}/mall</assembleDirectory>
		<!-- 打包的jar,以及maven依赖的jar放到这个目录里面 -->
		<repositoryName>lib</repositoryName>
		<!-- 可执行脚本的目录 -->
		<binFolder>bin</binFolder>
		<!-- 配置文件的目标目录 -->
		<configurationDirectory>conf</configurationDirectory>
		<!-- 拷贝配置文件到上面的目录中 -->
		<copyConfigurationDirectory>true</copyConfigurationDirectory>
		<!-- 从哪里拷贝配置文件 (默认src/main/config) -->
		<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
		<!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录 -->
		<repositoryLayout>flat</repositoryLayout>
		<encoding>UTF-8</encoding>
		<logsDirectory>logs</logsDirectory>
		<tempDirectory>tmp</tempDirectory>
		<programs>
			<program>
				<id>mall</id>
				<!-- 启动类 -->
				<mainClass>com.mall.Main</mainClass>
				<jvmSettings>
					<extraArguments>
						<extraArgument>-server</extraArgument>
						<extraArgument>-Xmx2G</extraArgument>
						<extraArgument>-Xms2G</extraArgument>
					</extraArguments>
				</jvmSettings>
			</program>
		</programs>
	</configuration>
</plugin>

执行 mvn package appassembler:assemble

执行完成之后,在target/mall目录就有可执行脚本


二:生成后台服务程序

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>appassembler-maven-plugin</artifactId>
	<version>1.10</version>
	<configuration>
		<!-- 根目录 -->
		<assembleDirectory>${project.build.directory}/mall</assembleDirectory>
		<!-- 打包的jar,以及maven依赖的jar放到这个目录里面 -->
		<repositoryName>lib</repositoryName>
		<!-- 可执行脚本的目录 -->
		<binFolder>bin</binFolder>
		<!-- 配置文件的目标目录 -->
		<configurationDirectory>conf</configurationDirectory>
		<!-- 拷贝配置文件到上面的目录中 -->
		<copyConfigurationDirectory>true</copyConfigurationDirectory>
		<!-- 从哪里拷贝配置文件 (默认src/main/config) -->
		<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
		<!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录 -->
		<repositoryLayout>flat</repositoryLayout>
		<encoding>UTF-8</encoding>
		<logsDirectory>logs</logsDirectory>
		<tempDirectory>tmp</tempDirectory>
		<daemons>
			<daemon>
				<id>mall</id>
				<mainClass>com.mall.Main</mainClass>
				<platforms>
					<platform>jsw</platform>
				</platforms>
				<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>
						<configuration>
							<property>
								<name>configuration.directory.in.classpath.first</name>
								<value>conf</value>
							</property>
							<property>
								<name>wrapper.ping.timeout</name>
								<value>120</value>
							</property>
							<property>
								<name>set.default.REPO_DIR</name>
								<value>lib</value>
							</property>
							<property>
								<name>wrapper.logfile</name>
								<value>logs/wrapper.log</value>
							</property>
						</configuration>
					</generatorConfiguration>
				</generatorConfigurations>
				<jvmSettings>
					<!-- jvm参数 -->
					<systemProperties>
						<systemProperty>com.sun.management.jmxremote</systemProperty>
						<systemProperty>com.sun.management.jmxremote.port=1984</systemProperty>
						<systemProperty>com.sun.management.jmxremote.authenticate=false</systemProperty>
						<systemProperty>com.sun.management.jmxremote.ssl=false</systemProperty>
					</systemProperties>
				</jvmSettings>
			</daemon>
		</daemons>
	</configuration>
</plugin>
执行mvn clean package appassembler:generate-daemons
执行完成之后,在target\generated-resources\appassembler\jsw\mall目录里面就有后台运行的程序

[localhost bin]# ./mall
Usage: ./mall { console | start | stop | restart | status | dump }


你可能感兴趣的:(maven)