【发布时间】:2021-12-07 17:51:57
【问题描述】:
您好。我一直在尝试使用 apache maven 程序集插件来生成一个包含我的应用程序所有依赖项的胖 jar。但是在执行 java -jar target/cache-maven-0.0.0-jar-with-dependencies.jar 时会生成以下输出:
错误:无法找到或加载主类缓存。SpringBootCacheExampleApplication
这可能是 MANIFEST.MF 中的问题:
Class-Path: spring-boot-starter-actuator-2.5.5.jar spring-boot-starter
-2.5.5.jar spring-boot-2.5.5.jar spring-context-5.3.10.jar spring-aop
-5.3.10.jar spring-expression-5.3.10.jar spring-boot-autoconfigure-2.
5.5.jar spring-boot-starter-logging-2.5.5.jar logback-classic-1.2.6.j
ar logback-core-1.2.6.jar slf4j-api-1.7.32.jar log4j-to-slf4j-2.14.1.
jar log4j-api-2.14.1.jar jul-to-slf4j-1.7.32.jar jakarta.annotation-a
pi-1.3.5.jar spring-core-5.3.10.jar spring-jcl-5.3.10.jar snakeyaml-1
.28.jar spring-boot-actuator-autoconfigure-2.5.5.jar spring-boot-actu
ator-2.5.5.jar jackson-databind-2.12.5.jar jackson-annotations-2.12.5
.jar jackson-core-2.12.5.jar jackson-datatype-jsr310-2.12.5.jar micro
meter-core-1.7.4.jar HdrHistogram-2.1.12.jar LatencyUtils-2.0.3.jar s
pring-boot-starter-webflux-2.5.5.jar spring-boot-starter-json-2.5.5.j
ar jackson-datatype-jdk8-2.12.5.jar jackson-module-parameter-names-2.
12.5.jar spring-boot-starter-reactor-netty-2.5.5.jar reactor-netty-ht
tp-1.0.11.jar netty-codec-http-4.1.68.Final.jar netty-common-4.1.68.F
inal.jar netty-buffer-4.1.68.Final.jar netty-transport-4.1.68.Final.j
ar netty-codec-4.1.68.Final.jar netty-handler-4.1.68.Final.jar netty-
codec-http2-4.1.68.Final.jar netty-resolver-dns-4.1.68.Final.jar nett
y-resolver-4.1.68.Final.jar netty-codec-dns-4.1.68.Final.jar netty-re
solver-dns-native-macos-4.1.68.Final-osx-x86_64.jar netty-transport-n
ative-unix-common-4.1.68.Final.jar netty-transport-native-epoll-4.1.6
8.Final-linux-x86_64.jar reactor-netty-core-1.0.11.jar netty-handler-
proxy-4.1.68.Final.jar netty-codec-socks-4.1.68.Final.jar spring-web-
5.3.10.jar spring-beans-5.3.10.jar spring-webflux-5.3.10.jar reactor-
core-3.4.10.jar reactive-streams-1.0.3.jar joda-time-2.9.2.jar
Build-Jdk-Spec: 1.8
Created-By: Maven Archiver 3.5.0
Main-Class: cache.SpringBootCacheExampleApplication
如您所见,它不包含像 spring maven 插件生成的基本信息:
Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: cache-maven
Implementation-Version: 0.0.0
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
Start-Class: cache.SpringBootCacheExampleApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.5.5
Created-By: Maven Jar Plugin 3.2.0
Main-Class: org.springframework.boot.loader.JarLauncher```
这是我的 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>
<groupId>cache</groupId>
<artifactId>cache-maven</artifactId>
<packaging>jar</packaging>
<version>0.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--<sourceDirectory>src/main/java/cache</sourceDirectory>-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!--<mainClass>${start-class}</mainClass>-->
<mainClass>cache.SpringBootCacheExampleApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath> <mainClass>cache.SpringBootCacheExampleApplication</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我的项目结构:
<pre>cache-service/
pom.xml
mvnw
src/
main/
java/
cache/
Customer.java
SpringBootCacheExampleApplication.java (main)
service/
CustomerController.java
resources/
application.yml
target/
cache-maven-0.0.0.jar
cache-maven-0.0.0-jar-with-dependencies.jar
cache-maven-0.0.0.jar.original
archive-tmp/
.
classes/
application.yml
cache/
Customer.class
SpringBootCacheExampleApplication.class
service/
CustomerController.class
generated-sources/
annotations/
.
maven-archiver/
pom.properties
maven-status/
maven-compiler-plugin/
compile/
default-compile/
createdFiles.lst
inputFiles.lst<pre>
任何帮助都会非常有价值。谢谢。
【问题讨论】:
-
为什么不使用 spring-boot-maven-plugin? docs.spring.io/spring-boot/docs/current/maven-plugin/reference/…
-
谢谢。如果你在插件部分阅读我的 pom.xml,你会发现它确实是我使用的第一个插件。
-
哦,关于 spring boot maven 插件,据我所知,它不会生成包含所有依赖项的 fat jar,这与 maven 程序集插件不同。但是 spring maven 插件可以很好地生成一个没有依赖关系的简单 jar。
标签: spring-boot maven maven-assembly-plugin