SpringBoot打包所有依赖jar包和配置文件全部分离


常规的springboot打包后,只生成一个jar包,配置文件和所有依赖包都在一起,当有一点点改动,上线时需要把整个包发布上去,有些不太方便。

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.0.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.mei.dubbo</groupId>
	<artifactId>SpringBootPackageTest</artifactId>
	<version>0.0.1</version>
	<name>SpringBootPackageTest</name>
 
	<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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<!-- 去除resources下的所有文件 -->
					<excludes>
						<exclude>**/*.properties</exclude>
						<exclude>**/*.xml</exclude>
						<exclude>**/*.yml</exclude>
						<exclude>static/**</exclude>
						<exclude>templates/**</exclude>
					</excludes>
				</configuration>
			</plugin>
 
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<layout>ZIP</layout>
					<includes>
						<!-- 打包时,本jar包不包含其他依赖包 , 否则打出的jar包还是很大 -->
						<include>
							<groupId>nothing</groupId>
							<artifactId>nothing</artifactId>
						</include>
					</includes>
				</configuration>
			</plugin>
 
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptors>
						<!-- 用于复制文件等配置 -->
						<descriptor>assembly.xml</descriptor>
					</descriptors>
					<finalName>${project.artifactId}</finalName>
					<appendAssemblyId>false</appendAssemblyId>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
 
	</build>
 
 
</project>


assembly.xml

<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>release</id>
	<includeBaseDirectory>true</includeBaseDirectory>
 
	<formats>
		<!-- 打出包的后缀, xxxx.tar.gz -->
		<format>tar.gz</format>
		<format>dir</format>
	</formats>
	<fileSets>
		<fileSet>
			<directory>target/classes</directory>
			<outputDirectory>config</outputDirectory>
			<includes>
				<include>*.yml</include>
				<include>*.xml</include>
				<include>*.properties</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>target/classes</directory>
			<outputDirectory>resources</outputDirectory>
			<includes>
				<include>static/**</include>
				<include>templates/**</include>
			</includes>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<!-- 将项目所有依赖包拷贝到发布包的lib目录下 -->
			<outputDirectory>lib</outputDirectory>
			<useProjectArtifact>false</useProjectArtifact>
		</dependencySet>
	</dependencySets>
 
 
</assembly>


项目结构如下:

20190815142204777.png


使用eclipse   maven  install 打包后,结构如下:

tar.gz 包中没有SpringBootPackageTest-0.0.1.jar包, 解压SpringBootPackageTest-0.0.1.tar.gz后需要手动考进去。

20190815142316634.png


20190815143229775.png


20190815143151222.png


20190815143212555.png



运行:  java -Dloader.path=./lib,config -jar  SpringBootPackageTest-0.0.1.jar

访问:20190815143015166.png



暂时简单测试没问题。这种方式只适合不带页面的后台程序运行(如前后端分离的后端程序),有页面的工程暂时没找到解决方法。


参考:

nohup java -jar xxx.jar --spring.config.location=application.yml  &




springboot jar

2021.05.29 18:19

https://www.meihaocloud.com.com/121.html , 欢迎转载,请在文章页标出原文连接 !


Copyright © 2020 千夕网 联系站长

粤公网安备 44030302001408号 粤ICP备19099833号-1