springboot maven依赖jar包外置
**********************
示例
*****************
controller 层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello");
return "success";
}
}
**********************
依赖jar包默认打包位置
项目打包
解压 hello.jar:依赖jar包默认存放在BOOT-INF/lib目录中
**********************
依赖jar包外置
pom.xml(build部分)
<build>
<finalName>hello</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<!-- 项目jar包中排除依赖jar包 -->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
</plugin>
<!-- 设置项目jar包依赖jar包的classpath -->
<!-- classpath必须设置,否则会报错 -->
<!-- mainClass可不设置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<mainClass>com.example.demo.DemoApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 依赖jar包复制到 target/lib目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<outputDirectory>${build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
项目打包
依赖jar包复制到了target/lib目录中
解压 hello.jar
BOOT-INF/lib 目录中只有spring-boot-jarmode-layertools-2.5.5.jar包
META-INF/MANIFEST.MF 文件
Manifest-Version: 1.0
Created-By: Maven Jar Plugin 3.2.0
Build-Jdk-Spec: 16
Class-Path: lib/spring-boot-starter-web-2.5.5.jar lib/spring-boot-starte
r-2.5.5.jar lib/spring-boot-2.5.5.jar lib/spring-boot-autoconfigure-2.5
.5.jar lib/spring-boot-starter-logging-2.5.5.jar lib/logback-classic-1.
2.6.jar lib/logback-core-1.2.6.jar lib/log4j-to-slf4j-2.14.1.jar lib/lo
g4j-api-2.14.1.jar lib/jul-to-slf4j-1.7.32.jar lib/jakarta.annotation-a
pi-1.3.5.jar lib/snakeyaml-1.28.jar lib/spring-boot-starter-json-2.5.5.
jar lib/jackson-databind-2.12.5.jar lib/jackson-annotations-2.12.5.jar
lib/jackson-core-2.12.5.jar lib/jackson-datatype-jdk8-2.12.5.jar lib/ja
ckson-datatype-jsr310-2.12.5.jar lib/jackson-module-parameter-names-2.1
2.5.jar lib/spring-boot-starter-tomcat-2.5.5.jar lib/tomcat-embed-core-
9.0.53.jar lib/tomcat-embed-el-9.0.53.jar lib/tomcat-embed-websocket-9.
0.53.jar lib/spring-web-5.3.10.jar lib/spring-beans-5.3.10.jar lib/spri
ng-webmvc-5.3.10.jar lib/spring-aop-5.3.10.jar lib/spring-context-5.3.1
0.jar lib/spring-expression-5.3.10.jar lib/spring-boot-configuration-pr
ocessor-2.5.5.jar lib/lombok-1.18.20.jar lib/slf4j-api-1.7.32.jar lib/s
pring-core-5.3.10.jar lib/spring-jcl-5.3.10.jar
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.example.demo.DemoApplication
Spring-Boot-Version: 2.5.5
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
使用测试
# cd target
E:\java\IdeaProjects\springboot maven 依赖jar包>cd target
# 运行jar包, localhost:8080/hello, 控制台输出
E:\java\IdeaProjects\springboot maven 依赖jar包\target>java -jar hello.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.5)
2021-09-30 20:58:11.860 INFO 12256 --- [ main] o.s.boot.SpringApplication : Starting application using Java 15
on LAPTOP-D73GD8TE with PID 12256 (started by 28401 in E:\java\IdeaProjects\springboot maven 依赖jar包\target)
2021-09-30 20:58:11.872 INFO 12256 --- [ main] o.s.boot.SpringApplication : No active profile set, falling bac
k to default profiles: default
2021-09-30 20:58:13.170 INFO 12256 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8
080 (http)
2021-09-30 20:58:13.186 INFO 12256 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-09-30 20:58:13.186 INFO 12256 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache T
omcat/9.0.53]
2021-09-30 20:58:13.186 INFO 12256 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.23] of the A
pache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30]
2021-09-30 20:58:13.186 INFO 12256 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native librar
y [1.2.23] using APR version [1.7.0].
2021-09-30 20:58:13.186 INFO 12256 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sen
dfile [true], accept filters [false], random [true], UDS [false].
2021-09-30 20:58:13.186 INFO 12256 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprC
onnector [false], useOpenSSL [true]
2021-09-30 20:58:13.201 INFO 12256 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [
OpenSSL 1.1.1c 28 May 2019]
2021-09-30 20:58:13.279 INFO 12256 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebAp
plicationContext
2021-09-30 20:58:13.279 INFO 12256 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initia
lization completed in 1331 ms
2021-09-30 20:58:13.699 INFO 12256 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (h
ttp) with context path ''
2021-09-30 20:58:13.714 INFO 12256 --- [ main] o.s.boot.SpringApplication : Started application in 2.303 secon
ds (JVM running for 2.7)
2021-09-30 20:58:19.766 INFO 12256 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServ
let 'dispatcherServlet'
2021-09-30 20:58:19.766 INFO 12256 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherSe
rvlet'
2021-09-30 20:58:19.766 INFO 12256 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
hello
依赖jar外置,项目可正常运行