你知道springboot+maven的打包騷操作嗎?

前言

本人在公司擔任的活兒比較雜,包括部署上線,調試都是自己幹.那麼在項目打包部署遇到以下困擾

  1. 項目打包,代碼本身不多,但是依賴太重,導致打出的jar包很大,上傳到服務器比較慢
  2. 配置文件打包到jar包內部,不便於調試修改

這是一個springboot+maven寫的demo,主要演示springboot項目通過maven插件,將依賴與配置文件打包到jar包外部,實現靈活的項目管理.僅供參考學習
方法總比問題多,覺得我這個辦法比較low的,請不要往下看

  • 適用場景:內網項目
  • 如果是外網項目建議用更優雅的解決方案,比如使用腳本拉取代碼,用配置中心動態修改配置.

一.創建SpringBoot項目

1.用idea新建項目
在這裏插入圖片描述2.填寫項目信息
在這裏插入圖片描述3.這裏勾選spring web 版本選擇低版本
在這裏插入圖片描述

4.選擇 項目名,然後finish
在這裏插入圖片描述
5.新建一個TestController,寫先經典的hello world
在這裏插入圖片描述

package xyz.hashdog.pk;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @RequestMapping("test")
    public String test(){
        return "hello world";
    }
}

6.配置文件改一下端口

server.port=4567

7.啓動項目
在這裏插入圖片描述

8.成功訪問接口
http://localhost:4567/test
在這裏插入圖片描述

二.項目打包

1.執行package打包(默認打包到target目錄)
在這裏插入圖片描述2.修改pom文件
去掉原有的spring-boot-maven-plugin
將pom.xml中的plugin修改爲

 <plugins>
            <!-- 依賴包插件 -->
            <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>${project.build.directory}/app/lib</outputDirectory>
                            <!-- 是否不包含間接依賴 -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- 忽略版本 -->
                            <stripVersion>false</stripVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- maven資源文件複製插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-config</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/app/config</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <exclude>**/*.xml</exclude>
                                        <exclude>**/*.conf</exclude>
                                        <exclude>**/*.properties</exclude>
                                    </includes>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 可執行jar插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <outputDirectory>${project.build.directory}/app/bin</outputDirectory>
                    <!--這些配置將寫入到MANIFEST.MF文件中-->
                    <archive>
                        <!--指定程序入口-->
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>../lib/</classpathPrefix>
                            <mainClass>xyz.hashdog.pk.PkApplication</mainClass>
                        </manifest>
                        <!-- (配置文件外置目錄) -->
                        <manifestEntries>
                            <Class-Path>../config/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <excludes>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.conf</exclude>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*/*.json</exclude>
                    </excludes>
                </configuration>
            </plugin>

        </plugins>

3.執行package,將整個項目打包到app目錄下了
在這裏插入圖片描述

4.將config中properties配置文件端口改爲4568,的啓動jar文件
在這裏插入圖片描述5.訪問成功
localhost:4568/test
在這裏插入圖片描述

三.代碼參考

https://download.csdn.net/download/corleone_4ever/12378074

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章