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

目录
  • 如何将jar包打包到指定目录
    • 1.目的
    • 2.修改pom
    • 3.修改启动脚本
  • 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帮助

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

(0)

相关推荐

  • springboot jar包外置配置文件的解决方法

    为什么要搞个解决方案呢?按照网上教程,spring boot项目中,配置文件有优先级,其中,放在根目录下的config文件夹中优先级最高:根目录下次之:然后是resources/config/:resources/下优先级最低.而加载顺序刚好反过来,resources/下最先加载,而/config/最后加载.然后在命令行方式下,指定一下这个配置文件的路径,就可以了,云云. 也许事实就是如此的吧,但我试来试去,总不成功,系统死活都要读resources/下,这个优先级最低的配置文件. 这对于用 j

  • 关于Springboot打成JAR包后读取外部配置文件的问题

    Springboot的默认配置文件为:application.properties或者是application.yml 如果这两个配置文件都存在,不冲突的话,就互相补充.冲突的话,则properties优先级高. 当我们使用IDEA创建出一个Springboot项目上时,配置文件默认出现在classpath(也就是项目里的resources)目录下. Springboot的application.properties配置文件的加载路径优先级(从高到低): 工程根目录:./config/ 工程根目

  • Spring Boot 把配置文件和日志文件放到jar外部

    如果不想使用默认的application.properties,而想将属性文件放到jar包外面,可以使用如下两种方法: 只能设置全路径.因为Java -jar运行jar包时,无法指定classpath(无论通过参数还是环境变量,设置的classpath都会被覆盖). 方法1:命令行传参指定spring.config.location java -jar -Dspring.config.location=D:\zTest\config\config1.properties springbootre

  • Spring Boot 指定外部启动配置文件详解

    目录 使用spring boot默认的配置文件路径 指定外部的配置文件 特定配置 总结 默认的打包spring boot项目会把配置文件打到jar包中,有时候在测试时需要想修改某些配置项.这时除了可以用启动参数覆盖配置项以外,还可以指定外部的配置文件覆盖已有配置文件.在需要修改较多配置参数的时候会很方便. 使用spring boot默认的配置文件路径 默认的查找路径如下: 1.file:./config/ 2.file:./ 3.classpath:/config/ 4.classpath:/

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

    目录 如何将jar包打包到指定目录 1.目的 2.修改pom 3.修改启动脚本 jar包外指定配置文件及原理 解决方案 运行 原理 如何将jar包打包到指定目录 今天分享一下springboot将jar包打包到指定目录下. 由于之前上线都是一个打包到一个jar,由于服务多了,1个包100多M,哪怕是小版本上线都需要重新上传jar包. 1.目的 将不常用的比如spring,druid等不常用打包到lib目录,这样每次上线不需要上传这些.第三方或者常改动的还打包到本身的jar包内,每次上线都会新打包

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

    目录 如何将jar包打包到指定目录 1.目的 2.修改pom 3.修改启动脚本 jar包外指定配置文件及原理 解决方案 运行 原理 如何将jar包打包到指定目录 今天分享一下springboot将jar包打包到指定目录下. 由于之前上线都是一个打包到一个jar,由于服务多了,1个包100多M,哪怕是小版本上线都需要重新上传jar包. 1.目的 将不常用的比如spring,druid等不常用打包到lib目录,这样每次上线不需要上传这些.第三方或者常改动的还打包到本身的jar包内,每次上线都会新打包

  • SpringBoot项目运行jar包启动的步骤流程解析

    SpringBoot项目在开发中,方便快捷,有一点原因就是SpringBoot项目可以打jar包运行:把jar包直接扔服务器上,然后运行jar包就能访问项目接口了.下面介绍SpringBoot项目打jar包运行的步骤流程: 一.我们所熟悉的是在开发环境下,直接用开发工具来运行那个启动类,然后就能启动这个项目: 开发环境下启动项目 二. SpringBoot项目打jar包方法: [1]在cmd界面中,进入项目的本地存储地址 cmd命令下进入项目地址 [2]运行maven的打包命令,mvn clea

  • 详解docker部署SpringBoot及替换jar包的方法

    关于docker的安装和使用,可以看看之前这两篇文章.docker kubernetes dashboard安装部署详细介绍和Docker如何使用link建立容器之间的连接.这篇文章主要介绍如何在docker上部署springboot项目.关于如何创建springboot项目可以看看这篇文章IDEA上面搭建一个SpringBoot的web-mvc项目遇到的问题 本文主要介绍docker部署springboot的三种方式,分别是:入门方式.jar包替换部署的方式和脚本部署方式,一步步来手把手教程.

  • springboot项目以jar包运行的操作方法

    公司的springboot项目本来是打war包的,突然要求改成jar包,一路上碰到一些坑,在此记录一下. 一.pom文件配置 1.打包方式改成jar <packaging>jar</packaging> 2.配置可执行jar包的maven插件 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>sp

  • Springboot导入本地jar后 打包依赖无法加入的解决方案

    目录 Springboot导入本地jar后 打包依赖无法加入 1.本地jar包 2.pom文件添加自定义jar包导入 3.添加 项目引入本地jar包,并设置maven打包一并打入 pom文件中引入本地jar包依赖 在springboot的maven工具增加includeSystemScope功能 Springboot导入本地jar后 打包依赖无法加入 1.本地jar包 2.pom文件添加自定义jar包导入 <!--自定义jar--> <dependency> <groupId

  • SpringBoot为何可以使用Jar包启动详解

    目录 引言 Spring Boot 打包插件 SpringBoot FatJar 的组织结构 MAINFEST.MF 元信息 启动原理 源码分析 JarLauncher Launcher PropertiesLauncher MainMethodRunner 总结 引言 很多初学者会比较困惑,Spring Boot 是如何做到将应用代码和所有的依赖打包成一个独立的 Jar 包,因为传统的 Java 项目打包成 Jar 包之后,需要通过 -classpath 属性来指定依赖,才能够运行.我们今天就

  • SpringBoot分离打Jar包的两种配置方式

    目录 方式一:基于maven-jar-plugin 方式二:基于spring-boot-maven-plugin 附录:参考链接 SpringBoot分离打Jar包的两种方式 方式一:基于maven-jar-plugin 此方式基于这个小伙伴的配置改的:https://www.jb51.net/article/188606.htm 注意 这种方式打包出来的Jar基于插件提供的类加载器启动:org.springframework.boot.loader.PropertiesLauncher 所有依

  • maven项目引用外部jar包的方法

    问题描述: 有一个java maven web项目,需要引入一个第三方包gdal.jar,但是这个包是自己打包的,在maven中央库里面找不到该包,因此我采用传统的方式,将这个包拷贝到:项目名称\src\main\webapp\WEB-INF\lib的目录下,然后通过config build path将该gdal.jar包引入到项目工程中.对于传统java web项目,这么做当然没有问题,但是对于maven项目,项目打包(mvn install)时就会报错,在项目调试时(debug on ser

  • 详解springboot中的jar包部署步骤

    eclipse中: 1.单击整个项目 run as - maven clean - maven install 2.找到项目所在的路径 找到所有的jar包 3.把jar包放到linux对应的文件夹 linux中部署项目: 1.查看jar是否在运行中 ps -ef | grep SpliderWeb-0.0.1-SNAPSHOT.jar 2.有运行的jar包 杀死对应的进程 kill 进程号 3.无运行的jar包 部署项目 java -jar SpliderWeb-0.0.1-SNAPSHOT.j

随机推荐