文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

maven springboot怎么将jar包打包到指定目录

2023-06-22 03:50

关注

这篇文章将为大家详细讲解有关maven springboot怎么将jar包打包到指定目录,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

1.目的

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

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

如图所示:

原来的打包方式

maven springboot怎么将jar包打包到指定目录

改后的方式:

maven springboot怎么将jar包打包到指定目录

maven springboot怎么将jar包打包到指定目录

2.修改pom

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

如图所示。

maven springboot怎么将jar包打包到指定目录

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.JarLauncherStart-Class: com.chinaunicom.gateway.GatewayApplication

配置后:

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

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

maven springboot怎么将jar包打包到指定目录

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

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.

关于“maven springboot怎么将jar包打包到指定目录”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯