springBoot打包瘦身

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

springBoot打包的时候代码和jar包打包在同一个jar包里面,会导致jar包非常庞大,在不能连接内网的时候调试代码,每次只改动了java代码就需要把所有的jar包一起上传,导致传输文件浪费了很多时间,所以如果打包的时候只把写成的代码打包,已经上传服务器的jar包不用修改,这样每次上传文件将会大大节省时间,接下来描述一下单独打jar包的过程。

更改springBoot打jar包的插件即可改为一下格式:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
        <includes>
            <include>
                <groupId>non-exists</groupId>
                <artifactId>non-exists</artifactId>
            </include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>classes</classifier>
                <attach>false</attach>
            </configuration>
        </execution>
    </executions>
</plugin>

这个样子打的jar包就会非常之小。

那么导入的jar包又在哪里呢?没关系,我们还有另一个插件。如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>target/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive>
                <stripVersion>false</stripVersion>
                <includeScope>runtime</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>

这样,就可以把项目中使用到的所有jar包提取出来。

然后项目在大jar包的时候也会把配置文件和页面一起打包,导致每次都要替换或者更改配置文件,导致非常繁琐。

我们可以在打包插件中指定不用打包的内容,如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <!--这里是打包的时候排除的文件例如配置文件-->
        <excludes>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.xml</exclude>
            <exclude>**/*.yml</exclude>
            <exclude>static/**</exclude>
            <exclude>templates/**</exclude>
            <exclude>**/*.xlsx</exclude>
        </excludes>
    </configuration>
</plugin>

这样就部署的,准备工作就做好了。

部署的时候需要把配置文件和页面等文件放在和jar包同一目录,在启动项目的的时候,指定项目jar包和导入jar包的位置即可启动。

java -jar -Dloader.path=.,3rd-lib HMServicePlatform-0.0.1-SNAPSHOT-classes.jar

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值