springboot 如何打 thin 包

1 篇文章 0 订阅
1 篇文章 0 订阅

 springboot 用 maven 打包的时候,经常会用到插件spring-boot-maven-plugin,打的一个Fat 包,动辄上百兆,尤其是在测试阶段需要经常改包,配置上传,会很耗时,如果能打一个thin 包,几百K ,且读取外部配置就更好了,网上找了很多资料,大多都千篇一律,东抄西抄,很多都没有说到点子上,大多数都是说的用插件 maven-assembly-plugin 再通过自定义文件descriptor定义包结构,这样打包是没有问题,但程序根本无法通过 java -jar 启动程序,一般都会报 '中没有主清单属性' 或者 class 找不到的错误,对比了一下 插件 spring-boot-maven-plugin 打出来的包,MAINFEST.MF 结构有很大不同,里面指定了Spring-Boot 的属性,所有不能只用 maven-assembly-plugin,同时还需要用到 spring-boot-maven-plugin ,并且在spring-boot-maven-plugin  中要排除依赖,重要配置如下:

spring-boot-starter-parent 是一定要的

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>

    </parent>

接下来是 spring-boot-maven-plugin 和 maven-assembly-plugin

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <executable>true</executable>
                    <includes>
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                    <mainClass>com.xxx.xxx.xxx</mainClass>
                </configuration>
 <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <appendAssemblyId>true</appendAssemblyId>

                    <descriptors>
                        <!--自定义打包描述文件 -->
                        <descriptor>src/main/assemble/package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-zip</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
 </plugin>

打包完成之后,可以看到jar 包,小了不少,接下来就是启动jar 包:java -jar xxx.jar
一般都会报一个 class not found , 一般人想到的解决方法是 -classpath, 但试过之后发现根本不管用,有说法是 springboot 不能直接获取classpath ,
所以,正解是: -Dloader.path=/var/lib/hadoop-hdfs/xxx/lib,/var/lib/hadoop-hdfs/xxx/conf . lib 下放jar 包,conf 下放配置,一定要放绝对路径,否则并没什么卵用
java -Dloader.path=/var/lib/hadoop-hdfs/xxx/lib,/var/lib/hadoop-hdfs/xxx/conf -jar xxx.jar
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值