maven springboot如何将jar包打包到指定目录

 更新时间:2021年12月18日 15:33:06   作者:烤鸭的世界我们不懂  
这篇文章主要介绍了maven springboot如何将jar包打包到指定目录,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

如何将jar包打包到指定目录

今天分享一下springboot将jar包打包到指定目录下。

由于之前上线都是一个打包到一个jar,由于服务多了,1个包100多M,哪怕是小版本上线都需要重新上传jar包。

1.目的

将不常用的比如spring,druid等不常用打包到lib目录,这样每次上线不需要上传这些。第三方或者常改动的还打包到本身的jar包内,每次上线都会新打包。

这样原来的100多M的jar包,可以变成2、3M。

如图所示:

原来的打包方式

改后的方式:

2.修改pom

简单解释一下,includes标签内就是打入jar包第三方jar。将上面的2M的包解压缩后,就是includes的包。

如图所示。

excludeGroupIds和excludeArtifactIds 是配置不在lib目录里的包,由于java启动加载的机制是优先加载jar包,再加载外部目录,如果jar包都存在两个地方,这样配置就没有意义了,每次还是得重新发布lib目录,所以将includes中的包,再在excludeGroupIds 和 excludeArtifactIds 配置上

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.5.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>nothing</groupId>
                        <artifactId>nothing</artifactId>
                    </include>
                    <include>
                        <groupId>com.etc</groupId>
                        <artifactId>etc-manage-api</artifactId>
                    </include>
                    <include>
                        <groupId>com.etc</groupId>
                        <artifactId>etc-manage-core</artifactId>
                    </include>
                    <include>
                        <groupId>com.etc</groupId>
                        <artifactId>etc-manage-rpc-api</artifactId>
                    </include>
                    <include>
                        <groupId>com.sinoiov.etc.apollo</groupId>
                        <artifactId>apollo-spring-boot-starter</artifactId>
                    </include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <excludeGroupIds>
                            com.sinoiov.etc.apollo
                        </excludeGroupIds>
                        <excludeArtifactIds>
                            etc-manage-api,etc-manage-core,etc-manage-rpc-api
                        </excludeArtifactIds>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3.修改启动脚本

原脚本

java -jar etc-manage-service-basic-2.2.0.jar

现脚本 (如果相对目录不好用,就用绝对目录试试)

java Dloader.path=../lib  -jar etc-manage-service-basic-2.2.0.jar

jar包外指定配置文件及原理

解决方案

修改maven的pom.xml文件

不拷贝资源文件

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>*</exclude>
        </excludes>
        <filtering>true</filtering>
    </resource>
</resources>

修改打包方式

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
    </configuration>
</plugin>

运行

假设application.properties和application-{profile}.properties都在/tmp/temp/config,jar文件在/tmp/temp

java -Dloader.path=file:///tmp/temp/config,demo-1.0.jar -jar demo-1.0.jar

原理

对比jar包中MANIFEST.MF文件在`ZIP配置前后的区别

配置前:

Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication

配置后:

Main-Class: org.springframework.boot.loader.PropertiesLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication

发现是类加载器变了,查看org.springframework.boot.loader包下所有加载器实现:

这里写图片描述

查看五个类描述:官方文档

JarLauncher

Launcher for JAR based archives. This launcher assumes that dependency jars are included inside a /BOOT-INF/lib directory and that application classes are included inside a /BOOT-INF/classes directory.

WarLauncher

Launcher for WAR based archives. This launcher for standard WAR archives. Supports dependencies in WEB-INF/lib as well as WEB-INF/lib-provided, classes are loaded from WEB-INF/classes.

PropertiesLauncher

Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible and more amenable to creating well-behaved OS-level services than a model based on executable jars.
Looks in various places for a properties file to extract loader settings, defaulting to application.properties either on the current classpath or in the current working directory. The name of the properties file can be changed by setting a System property loader.config.name (e.g. -Dloader.config.name=foo will look for foo.properties. If that file doesn't exist then tries loader.config.location (with allowed prefixes classpath: and file: or any valid URL). Once that file is located turns it into Properties and extracts optional values (which can also be provided overridden as System properties in case the file doesn't exist):
loader.path: a comma-separated list of directories (containing file resources and/or nested archives in .jar or .zip or archives) or archives to append to the classpath. BOOT-INF/classes,BOOT-INF/lib in the application archive are always used
loader.main: the main method to delegate execution to once the class loader is set up. No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.

spring-boot-maven-plugin帮助

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 最全总结SpringBean的作用域管理

    最全总结SpringBean的作用域管理

    今天给大家详细总结了SpringBean的作用域管理,文中有非常详细的图文介绍以及代码示例,对正在学习java的小伙伴们还很有帮助,需要的朋友可以参考下
    2021-05-05
  • 线程池的七大核心参数以及常用的四种线程池详解

    线程池的七大核心参数以及常用的四种线程池详解

    这篇文章主要介绍了线程池的七大核心参数以及常用的四种线程池使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-05-05
  • MyBatis-Plus的apply用法小结

    MyBatis-Plus的apply用法小结

    apply方法是一个非常有用的功能,apply方法允许用户直接在QueryWrapper或LambdaQueryWrapper中添加原生SQL片段,本文就详细的介绍一下apply方法,感兴趣的可以了解一下
    2024-10-10
  • java和matlab画多边形闭合折线图示例讲解

    java和matlab画多边形闭合折线图示例讲解

    由于要将“哈密顿回路问题(TSP)”的求解中间结果表示出来,查了一下使用程序画多边形图形。现在在总结一下,这个图是“由给定节点首尾相连的”闭合多边形
    2014-02-02
  • idea将项目上传到Gitee的图文过程

    idea将项目上传到Gitee的图文过程

    这篇文章主要介绍了idea将项目上传到Gitee上,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • java.sql.SQLException: connection holder is null错误解决办法

    java.sql.SQLException: connection holder is null错误解决办法

    这篇文章主要给大家介绍了关于java.sql.SQLException: connection holder is null错误的解决办法,这个错误通常是由于连接对象为空或未正确初始化导致的,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • springboot中如何去整合shrio实例分享

    springboot中如何去整合shrio实例分享

    这篇文章主要介绍了springboot中如何去整合shrio实例分享的相关资料,需要的朋友可以参考下
    2023-08-08
  • java中volatile关键字的作用详解

    java中volatile关键字的作用详解

    这篇文章主要介绍了java中volatile关键字的作用详解,volatile可以保证,若一个线程改变了某块内存的值,其他线程是可见的,以至于其他线程能及时更新这块内存,需要的朋友可以参考下
    2023-09-09
  • Java简单统计字符串中汉字,英文字母及数字数量的方法

    Java简单统计字符串中汉字,英文字母及数字数量的方法

    这篇文章主要介绍了Java简单统计字符串中汉字,英文字母及数字数量的方法,涉及java针对字符串的遍历、编码转换、判断等相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • Mybatis查不到数据查询返回Null问题

    Mybatis查不到数据查询返回Null问题

    mybatis突然查不到数据,查询返回的都是Null,但是 select count(*) from xxx查询数量,返回却是正常的。好多朋友遇到这样的问题不知所措,下面小编通过本教程简单给大家说明下
    2016-08-08

最新评论