构建没有依赖项的 Spring Boot jar 文件的最简单方法是什么? 基本上我应该能够将依赖项 jar 文件保存在单独的文件夹中。
目前我正在使用 spring boot maven 插件,但是,它会创建一个包含所有依赖项的 Fat jar 文件。
根本不使用
spring-boot-maven-plugin
并使用JAR包装。这样构建就不会将依赖项打包到 JAR 中。
spring-boot-maven-plugin 可以选择重新打包,将依赖项放入其中(制作 uber jar)
您可以禁用重新打包或使重新打包的 .jar 与其他分类器一起使用 [2]
将 pom.xml 中的构建条目更改为
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependency_jar</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
在目标文件夹中,将有一个 dependency_jar 文件夹,其中包含所有依赖项 jar,以及“project_name.jar”(fat jar)和“project_name.jar.original”(代码的 jar 文件)
下面是我在 How to Create an Executable JAR with Maven 上找到的解决方案, 您只需要将这些放入您的插件中即可。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
org.baeldung.executable.ExecutableMavenJar
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
我经常遇到这个问题,但每次都需要一些时间才能解决。我将在这里分享我的解决方案。
我这次的场景是:我有一个SpringBoot项目,有多个模块。每个模块都可能存在依赖关系。因为项目不大,所以想直接用jar包来启动我的服务。但是每次推送fat-jar包都非常慢,因为它的大小是100Mb。我尝试从包产品中删除除我的项目模块之外的所有jar包。我使用以下插件:
<!-- package special class -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactSet>
<includes>
<!-- my project group id -->
<include>com.example.your-group-id</include>
</includes>
</artifactSet>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
<excludeGroupIds>
<!-- my project group id -->
com.example.your-group-id
</excludeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
这样,他就可以将所有依赖移动到
target/libs
目录中,而我的jar包包含了我项目中的所有模块,因为它们可能会随着每次部署而更新,现在,jar文件只有700kb。希望可以帮到大家:)