使用appassembler-maven-plugin构建发布包

本文介绍如何使用Maven的appassembler插件自动生成适用于Linux和Windows系统的执行文件。通过配置pom.xml文件,可以轻松地为不同平台构建包含启动脚本的应用程序包,并调整JVM参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用该包的特点是可以自动生成两套执行文件:linux、windows

主要通过pom.xml配置并辅以编译命令完成构建:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.com</groupId>
	<artifactId>qiuyin</artifactId>
	<version>0.0.1</version>
	<name>test</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>appassembler-maven-plugin</artifactId>
				<version>2.0.0</version>
				<configuration>
					<!-- 生成linux, windows两种平台的执行脚本 -->
					<platforms>
						<platform>unix</platform>
						<platform>windows</platform>
					</platforms>
					<!-- 包存放的根目录 -->
					<assembleDirectory>${project.build.directory}/${project.name}</assembleDirectory>
					<!-- 打包的jar,以及maven依赖的jar存放目录 -->
					<repositoryName>lib</repositoryName>
					<!-- lib目录中jar的存放规则,默认是${groupId}/${artifactId}的目录格式,flat表示直接把jar放到lib目录 -->
					<!-- 可执行脚本的存放目录 -->
					<binFolder>bin</binFolder>
					<!-- 配置文件的存放目录 -->
					<configurationDirectory>conf</configurationDirectory>
					<!-- 拷贝配置文件到上面的目录中 -->
					<copyConfigurationDirectory>true</copyConfigurationDirectory>
					<!-- 从哪里拷贝配置文件 (默认src/main/config) -->
					<configurationSourceDirectory>src/main/config</configurationSourceDirectory>
					<repositoryLayout>flat</repositoryLayout>
					<encoding>UTF-8</encoding>
					<logsDirectory>logs</logsDirectory>
					<tempDirectory>tmp</tempDirectory>
					<programs>
						<program>
							<!-- 启动类 -->
							<mainClass>cn.com.qiuyin.TestApplication</mainClass>
							<jvmSettings>
								<extraArguments>
									<extraArgument>-server</extraArgument>
									<!-- 设定程序运行期间最大可占用的内存大小。如果程序运行需要占用更多的内存,超出了这个设置值,就会抛出OutOfMemory异常。 -->
									<extraArgument>-Xmx1G</extraArgument>
									<!-- 设定程序启动时占用内存大小。一般来讲,大点,程序会启动的快一点,但是也可能会导致机器暂时间变慢。 -->
									<extraArgument>-Xms1G</extraArgument>
								</extraArguments>
							</jvmSettings>
						</program>
					</programs>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

构建命令:mvn clean package appassembler:assemble

若是使用eclipse/myeclipse,右键点击项目,run as ->maven build……

在下图所示的地方输入命令:

构建好的文件会在开发工具,工作空间的项目目录下,文件名与项目名相同,目录结构如下:

要执行的文件在bin目录下:

可以看到.bat的文件可以直接在windows下执行,运行项目,而linux下需要给另一个文件授权:

chmod 755 /……(自己的目录)/项目名/bin/项目名-application

然后就可以在linux中运行这个文件了

 

最后记录一下运维相关的内容:

防火墙关闭或开放端口
        iptables -t filter -I INPUT 5 -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
        service iptables save
        iptables -t filter -D INPUT 5
    前面提到的授权
    vi /etc/rc.local
        export JAVA_HOME=/usr/java/jdk1.8.0_144
        nohup /……(自己的目录)/项目名/bin/项目名-application &

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值