maven assembly打包生成Java应用启动脚本bat和sh的方法
作者:靖节先生 时间:2023-09-16 03:06:54
1. maven插件介绍
springboot应用通过maven插件appassembler-maven-plugi生成启动脚本bat和sh。根据官网介绍,这个插件主要用于生成启动 java应用程序的脚本,能将项目依赖jar能够打包目录中,并且它们加入启动脚本类路径中。
主要命令
appassembler:assemble 打包项目并且配置bin 启动脚本,可以理解为使用类似spring-boot-maven-plugin打包出来项目,可以通过java -jar 方式启动项目,但是不支持stop、satus、restart这些操作,比较原始。
appassembler:create-repository 创建一个 appassembler 存储库,就是将工程打成jar
appassembler:generate-daemons 生成基于 JSW 的守护进程包装器,大多数人都是使用这个。
参考文档
appassembler-maven-plugin 插件为 SpringBoot 项目生成启动脚本
https://www.jb51.net/article/191442.htm
2. 生成启动脚本
2.1 创建springboot应用
2.2 创建启动测试类
MavenAssemblyDemoApplication
package com.zrj.maven.assembly.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* MavenAssemblyDemoApplication
*
* @author zrj
* @since 20221117
*/
@RestController
@SpringBootApplication
public class MavenAssemblyDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MavenAssemblyDemoApplication.class, args);
}
/**
* Hello World!
* If you open a web browser to localhost:8080, you should see the following output:
*
* @see 1 https://docs.spring.io/spring-boot/docs/2.7.5/reference/htmlsingle/
*/
@RequestMapping("/")
String home() {
long currentTime = System.currentTimeMillis();
String helloTime = currentTime + ":Hello World!";
System.out.println(helloTime);
return helloTime;
}
}
2.3 maven依赖配置
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 https://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.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zrj</groupId>
<artifactId>maven-assembly-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>maven-assembly-demo</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>
<!-- appassembler-maven-plugin插件自动生成java项目启动脚本sh或者bat-->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.1.0</version>
<!-- 如果不配置 generate-daemons,则打包命令为 mvn clean package appassembler:assemble -->
<!-- 如果配置了 generate-daemons,打包命令可以是 mvn clean package 也可以是 mvn clean package appassembler:assemble -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- flat与lib共同决定将项目用的的所有jar包复制到lib目录下 -->
<repositoryLayout>flat</repositoryLayout>
<!--从哪里copy配置文件-->
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<!--是否copy配置文件-->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!--生成的项目的目录位置,这里的client是项目的名称,你可以根据你的需要自己随便命名-->
<assembleDirectory>${project.build.directory}/client</assembleDirectory>
<!--配置文件存放在conf目录路径-->
<configurationDirectory>conf</configurationDirectory>
<!-- 打包的jar,以及maven依赖的jar放到这个目录里面 -->
<repositoryName>lib</repositoryName>
<!-- 可执行脚本的目录 -->
<binFolder>bin</binFolder>
<!-- 生成linux, windows两种平台的执行脚本 -->
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<!-- 针对不同平台生成不同类型的启动脚本 -->
<binFileExtensions>
<unix>.sh</unix>
<windows>.bat</windows>
</binFileExtensions>
<!--编码,日志-->
<encoding>UTF-8</encoding>
<logsDirectory>logs</logsDirectory>
<tempDirectory>tmp</tempDirectory>
<!--java启动参数-->
<extraJvmArguments>-Xms128m</extraJvmArguments>
<programs>
<program>
<!--指定主类,脚本名。会生成shell/bat两种类型,也可用platforms指定运行平台-->
<mainClass>com.zrj.maven.assembly.demo.MavenAssemblyDemoApplication</mainClass>
<!-- 生成的脚本文件的名称,比如start.sh,你也可以根据你的需要命名成其他名字 -->
<name>start</name>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. 测试验证
3.1 执行命令
执行命令或者使用idea
执行命令:maven install
idea:maven-lifecycle依次执行clean,install
3.2 生成脚本
|—— target
| |—— client
| | |—— bin
| | | |—— start.sh # linux平台下的启动脚本
| | | |—— start.bat # windows平台下的启动脚本
| |—— conf
| | |—— application.properties # 项目配置文件
| |—— lib
| | |—— 各种依赖jar包
| |—— logs
| |—— tmp
3.3 启动脚本验证
./start.bat 或者./start.sh
浏览器访问:http://localhost:8080/
刷新页面:1668682483758:Hello World!
来源:https://blog.csdn.net/m0_37583655/article/details/127909634
标签:maven,assembly,打包
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现的MD5加密功能与用法示例
2023-06-11 09:08:21
C# 调用API函数弹出映射网络驱动器对话框问题
2023-03-25 15:41:05
Android使用自定义View实现横行时间轴效果
2022-12-25 11:24:42
![](https://img.aspxhome.com/file/2023/6/139386_0s.jpg)
使用IDEA搭建ssm框架的详细图文教程
2021-12-04 10:37:20
![](https://img.aspxhome.com/file/2023/4/94744_0s.jpg)
Springboot+Poi导入Excel表格实现过程详解
2023-06-07 04:26:03
Winform实现将网页生成图片的方法
2022-09-06 13:39:31
![](https://img.aspxhome.com/file/2023/8/76388_0s.jpg)
Java Socket实现多线程通信功能示例
2022-11-29 03:24:59
mongo分布式锁Java实现方法(推荐)
2023-06-30 21:28:43
![](https://img.aspxhome.com/file/2023/8/78248_0s.jpg)
C# FileStream实现多线程断点续传
2022-06-19 06:50:56
Unity3D使用Shader实现腐蚀消失
2022-01-07 20:57:59
![](https://img.aspxhome.com/file/2023/1/89751_0s.gif)
如何基于java实现Gauss消元法过程解析
2023-12-15 21:51:08
![](https://img.aspxhome.com/file/2023/3/66953_0s.png)
Android7.0中关于ContentProvider组件详解
2023-10-30 19:48:29
Java多线程产生死锁的必要条件
2023-04-02 00:20:24
linux环境下java程序打包成简单的hello world输出jar包示例
2023-11-26 11:11:37
![](https://img.aspxhome.com/file/2023/8/60378_0s.png)
C#统计C、C++及C#程序代码行数的方法
2022-10-04 16:19:37
![](https://img.aspxhome.com/file/2023/8/113778_0s.png)
c#发送请求访问外部接口的实例
2023-05-25 17:43:30
C#中ListView用法实例
2021-10-15 06:10:24
![](https://img.aspxhome.com/file/2023/2/79102_0s.png)
使用java反射将结果集封装成为对象和对象集合操作
2022-03-11 18:30:26
C# WinForm窗体编程中处理数字的正确操作方法
2022-11-21 04:57:10
C#判断页面中的多个文本框输入值是否有重复的实现方法
2022-10-30 19:41:56