首页 > 作文

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

更新时间:2023-04-04 03:54:21 阅读: 评论:0

目录
如何将jar包打包到指定目录1.目的2.修改pom3.修改启动脚本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.relea</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>                    <pha>package</pha>                    <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-rvice-basic-2.2.0.jar

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

java dloader.path=../lib  -jar etc-manage-rvice-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世界大学排名揭晓包下所有加载器实现:

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

jarlauncher

launcher for jar bad archives. this launcher assumes that dependency jars are included inside a /boot-inf/lib directory and that application class are included inside a /boot-inf/class directory.

warlauncher

launcher for war bad archives. this launcher for standard war archives. supports dependencies in web-inf/lib as well as web-inf/lib-provided, class are loaded from web-inf/class.

propertieslauncher

launcher for archives with ur-configured classpath and main class via a properties file. this model is often more flexible and more amenable to creating well-behaved os-level rvices than a model v9手机bad on executable jars.
looks in various places for a properties file to extract loader ttings, 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 tting 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 ca the file doesn’t exist):
loader.path: a comma-parated 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/class,boot-inf/lib in the application archive are always ud
loader.main: the main method to delegate execution to once the class loader is t 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帮助

对zip格式说明对jar和war说明

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 03:54:19,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/7f30647773c9f401cd38a89b84b4e0ca.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:maven springboot如何将jar包打包到指定目录.doc

本文 PDF 下载地址:maven springboot如何将jar包打包到指定目录.pdf

标签:目录   脚本   上线   加载
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图