Article Directory
background
When packaging a project, usually only an executable jar file needs to be generated. However, sometimes it is necessary to attach startup scripts or external configuration files to the jar file, at which point assembly can be used for custom packaging. This tool can pack the required files into the specified directory according to your own needs. Usage scenarios include but are not limited to:
- According to different environments, the files are packaged into tar.gz or zip format.
- Extract the configuration files in the Spring Boot project to the external config directory to facilitate configuration modification at runtime.
- Move the startup jar file in the Spring Boot project to the specified directory for easy deployment and operation.
- Copy the run.sh file to the specified directory, which contains the commands to start, stop and restart the service.
Table of contents
source directory
Below is the directory of our project .
├─assembly
│ │ assembly.xml
│ ├─bin
│ run.sh
│
├─logs
│ laker.log
├─src
│ ├─main
│ ├─java
│ │ └─com
│ │ └─laker
│ │ └─admin
│ │ Application.java
│ │
│ └─resources
│ │ application-local.yml
│ │ application-prod.yml
│ │ application.yml
The expectations are packaged into the resulting directory below .
result directory
easyAdmin
│ README.md
| easyAdmin.jar
│ run.sh
├─ logs
│
├─ config
│ │ application-local.yml
│ │ application.yml
│ │ logback.xml
│
└─ web
└─ admin
└─ admin
└─ component
└─ config
| pear.config.yml
└─ view
└─ flow
└─ sys
index.html
login.html
accomplish
The first steppom.xml
is to add in the project maven-assembly-plugin
.
Configure the assembly.xml file path
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The second step custom assembly.xml
configuration
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 可自定义,这里指定的是项目环境 -->
<!-- easy-admin-1.0.0-local-1.0.0.RELEASE.tar.gz -->
<id>${profileActive}-${project.version}</id>
<!-- 打包的类型,如果有N个,将会打N个类型的包 -->
<formats>
<!-- <format>tar.gz</format>-->
<format>zip</format>
</formats>
<!-- 如果为true,打包出来的文件结构第一层为pom.xml里面定义的artifactId-version -->
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--
0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644->即用户具有读写权限,组用户和其它用户具有只读权限;
-->
<!-- 脚本 打包进压缩包 -->
<!-- 将assembly/bin目录下的所有文件输出到打包后的bin目录中 -->
<fileSet>
<directory>${basedir}/assembly/bin</directory>
<fileMode>0755</fileMode>
<!-- 输出到当前目录 -->
<outputDirectory>.</outputDirectory>
<includes>
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
<!-- 配置 打包进压缩包 -->
<!-- 指定输出target/classes中的配置文件到config目录中 -->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<!-- <include>application.yaml</include>-->
<include>application-${profileActive}.yaml</include>
<!-- <include>mapper/**/*.xml</include>-->
<!-- <include>static/**</include>-->
<!-- <include>templates/**</include>-->
<include>*.xml</include>
<!-- <include>*.properties</include>-->
</includes>
</fileSet>
<fileSet>
<directory>${basedir}/target/classes</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>application.yaml</include>
</includes>
</fileSet>
<!-- 将项目启动jar打包到目录中 -->
<fileSet>
<directory>${basedir}/target</directory>
<fileMode>0755</fileMode>
<!-- 输出到当前目录 -->
<outputDirectory>.</outputDirectory>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
<!-- 包含根目录下的文件 -->
<fileSet>
<directory>${basedir}</directory>
<includes>
<!-- <include>NOTICE</include>-->
<!-- <include>LICENSE</include>-->
<!-- <include>*.md</include>-->
<include>README.md</include>
</includes>
</fileSet>
<!-- 复制前端文件 -->
<fileSet>
<directory>${basedir}/web</directory>
</fileSet>
</fileSets>
</assembly>
The third steppom.xml
is to add maven profiles
configuration to the project
<!-- 1:local(默认) 本地 2:dev 开发环境 3:test 测试环境 4:uat 用户验收测试 5.pro:生产环境-->
<profiles>
<profile>
<id>local</id>
<properties>
<profileActive>local</profileActive>
</properties>
<activation>
<!-- 配置为默认值 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
The fourth step is to modify the activation in the project application.yaml
as@profileActive@
spring:
profiles:
# maven启用的profile,默认是local
active: @profileActive@
project packaging
Method 1 Use the IDEA tool to package, select the corresponding profiles, and then clean package
Method 2 Use the maven command to package
mvn clean package
mvn clean package -P prod -DskipTests
mvn clean package -Pprod
mvn clean package -DprofileActive=prod
// 等价于第一个命令
mvn clean package -P local
deploy
After packaging local, a compressed package will be generated in the target directory
easyAdmin-local-1.0.0.zip
easyAdmin-local-1.0.0.tar.gz
Linux decompression zip or tar.gz
upzip easyAdmin-local-1.0.0.zip
tar -zxvf easyAdmin-local-1.0.0.tar.gz
directory after decompression
easyAdmin
│ README.md
| easyAdmin.jar
│ run.sh
├─ logs
│
├─ config
│ │ application-local.yml
│ │ application.yml
│ │ logback.xml
│
└─ web
└─ admin
└─ admin
└─ component
└─ config
| pear.config.yml
└─ view
└─ flow
└─ sys
index.html
login.html
start up
sh run.sh start 启动服务
sh run.sh stop 停止服务
sh run.sh restart 重启服务
verify
Visit the address in the browser:http://ip:8080/admin/login.html
See the overall configuration source code
reference
- https://github.com/geekidea/spring-boot-assembly