共计 3934 个字符,预计需要花费 10 分钟才能阅读完成。
Springboot 项目配置文件、依赖分离打包 (一)
使用 maven-assembly-plugin 进行配置分离
assembly.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?> | |
<assembly> | |
<!-- 可自定义,这里指定的是项目环境 --> | |
<!-- xxx.tar.gz --> | |
<id>${name}</id> | |
<!-- 打包的类型,如果有 N 个,将会打 N 个类型的包 --> | |
<formats> | |
<format>tar.gz</format> | |
<format>zip</format> | |
</formats> | |
<includeBaseDirectory>true</includeBaseDirectory> | |
<fileSets> | |
<!-- 配置文件打包 - 打包至 config 目录下 --> | |
<fileSet> | |
<directory>src/main/resources/</directory> | |
<outputDirectory>config</outputDirectory> | |
<fileMode>0644</fileMode> | |
<includes> | |
<include>application.yml</include> | |
<include>*.xml</include> | |
<include>*.properties</include> | |
</includes> | |
</fileSet> | |
<!-- 启动文件目录 --> | |
<fileSet> | |
<directory>${basedir}/src/bin</directory> | |
<outputDirectory>bin</outputDirectory> | |
<fileMode>0755</fileMode> | |
<includes> | |
<include>**.sh</include> | |
<include>**.bat</include> | |
</includes> | |
</fileSet> | |
</fileSets> | |
<dependencySets> | |
<dependencySet> | |
<!-- 依赖库 --> | |
<outputDirectory>lib</outputDirectory> | |
<scope>runtime</scope> | |
<fileMode>0755</fileMode> | |
<excludes> | |
<exclude>${project.groupId}:${project.artifactId}</exclude> | |
</excludes> | |
</dependencySet> | |
<dependencySet> | |
<outputDirectory>boot</outputDirectory> | |
<fileMode>0755</fileMode> | |
<includes> | |
<include>${project.groupId}:${project.artifactId}</include> | |
</includes> | |
</dependencySet> | |
</dependencySets> | |
</assembly> |
Pom.xml 文件配置
pom 文件中 build 属性的配置
<build> | |
<!-- 打包后的启动 jar 名称 --> | |
<finalName>message</finalName> | |
<plugins> | |
<!-- 用于排除 jar 中依赖包 --> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
<configuration> | |
<layout>ZIP</layout> | |
<includes> | |
<!-- 项目启动 jar 包中排除依赖包 --> | |
<include> | |
<groupId>non-exists</groupId> | |
<artifactId>non-exists</artifactId> | |
</include> | |
</includes> | |
</configuration> | |
</plugin> | |
<!-- 将依赖 cp 到 lib 目录下 --> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-dependency-plugin</artifactId> | |
<version>3.1.0</version> | |
<executions> | |
<execution> | |
<phase>prepare-package</phase> | |
<goals> | |
<goal>copy-dependencies</goal> | |
</goals> | |
<configuration> | |
<outputDirectory>target/lib</outputDirectory> | |
<overWriteReleases>false</overWriteReleases> | |
<overWriteSnapshots>false</overWriteSnapshots> | |
<overWriteIfNewer>true</overWriteIfNewer> | |
<includeScope>compile</includeScope> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
<!-- maven 编译 --> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<!-- 不同版本需要制定具体的版本进行编译 --> | |
<source>${java.version}</source> | |
<target>${java.version}</target> | |
</configuration> | |
</plugin> | |
<!-- 打包时跳过测试 --> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-surefire-plugin</artifactId> | |
<version>2.22.1</version> | |
<configuration> | |
<skipTests>true</skipTests> | |
</configuration> | |
</plugin> | |
<!-- 将项目中代码文件打成 jar 包 --> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-jar-plugin</artifactId> | |
<version>3.1.0</version> | |
<configuration> | |
<excludes> | |
<!-- 打包后的 jar 包中不包括配置文件 --> | |
<!-- 通常是指 classpath 下目录下的文件,这样可以避免编写时的找不到相应文件 --> | |
<exclude>*.xml</exclude> | |
<exclude>*.properties</exclude> | |
<exclude>*.yml</exclude> | |
</excludes> | |
<archive> | |
<manifest> | |
<!-- 项目启动类 --> | |
<mainClass>xx.xxx.Application</mainClass> | |
<!-- 依赖的 jar 的目录前缀 --> | |
<classpathPrefix>../lib/</classpathPrefix> | |
<addClasspath>true</addClasspath> | |
</manifest> | |
<!-- 将 config 目录加入 classpath 目录 --> | |
<manifestEntries> | |
<Class-Path>../config/</Class-Path> | |
</manifestEntries> | |
</archive> | |
</configuration> | |
</plugin> | |
<!-- 打包插件 --> | |
<plugin> | |
<artifactId>maven-assembly-plugin</artifactId> | |
<version>3.1.0</version> | |
<configuration> | |
<descriptors> | |
<descriptor>src/main/assembly/assembly.xml</descriptor> | |
</descriptors> | |
</configuration> | |
<executions> | |
<execution> | |
<id>make-assembly</id> | |
<phase>package</phase> | |
<goals> | |
<goal>single</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> |
运行
1. 直接运行 java -jar xxxx.jar 即可
2. 编写 shell/bash 脚本
在 bin 目录下编写脚本文件如下:
shell
#! /bin/sh | |
HOME = '/opt/xxx/boot' | |
JAR_HOME = 'xxx.jar' | |
cd $HOME | |
nohup java -jar $JAR_HOME |
bash
@echo off | |
rem ====================================================================== | |
rem windows startup script | |
rem | |
rem ====================================================================== | |
rem startup jar | |
java -jar ../boot/xxx.jar | |
pause |
项目目录如下
正文完
发表至: java
2019-08-09