本文最后更新于 145 天前 ,文中信息可能已经过时。如有问题请在评论区留言。

开发环境

我们以本地 hutool 5.8.26 为例。

Maven

新建 Spring Boot 工程,使用 Maven 构建。

存放 Jar

新建 libs 目录,将下载好的 hutool-all-5.8.26.jar 放在其中:

image-altf.png

引入依赖

修改 pom.xml 文件,添加 hutool 依赖:

xml
1
2
3
4
5
6
7
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.26</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/hutool-all-5.8.26.jar</systemPath>
</dependency>

测试

编写测试方法:

java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package cn.tofuwine.spring.boot3.maven.example;

import cn.hutool.core.util.StrUtil;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class Test implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String helloWorld = StrUtil.builder()
                .append("hello")
                .append(" world")
                .toString();
        System.out.println(helloWorld);
    }
}

启动工程,控制台打印 hello world

打包

修改 pom.xml,添加如下:

xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 添加如下配置,包含本地 Jar -->
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>

项目打包后,以压缩文件形式打开生成的 jar,查看是否包含 hutool-all:(目录:BOOT-INF/lib

image-lhmb.png

运行 Jar,查看是否能打印出 hello world

bash
1
java -jar .\spring-boot-3-maven-example-0.0.1-SNAPSHOT.jar

image-cbfu.png

验证结束,无问题。

Gradle

新建 Spring Boot 工程,使用 Gradle - Kotlin DSL 构建。

image-xlpy.png

存放 Jar

新建 libs 目录,将下载好的 hutool-all-5.8.26.jar 放在其中:

image-aqjh.png

引入依赖

方式一

单独引入一个 Jar 文件(Jar:hutool-all-5.8.26.jar):

kotlin
1
2
3
4
5
dependencies {
    // ... other dependencies
    implementation(files("libs/hutool-all-5.8.26.jar"))
    // ... other dependencies
}

方式二

引入一个文件夹中的所有 Jar(文件夹: libs):

kotlin
1
2
3
4
5
dependencies {
    // ... other dependencies
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    // ... other dependencies
}

测试

编写测试方法:

java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package cn.tofuwine.spring.boot3.gradle.kotlin;

import cn.hutool.core.util.StrUtil;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class Test implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String helloWorld = StrUtil.builder()
                .append("hello")
                .append(" world")
                .toString();
        System.out.println(helloWorld);
    }
}

启动工程,控制台打印 hello world

打包

使用 bootJar 进行打包:

image-iosk.png

生成的 Jar 目录为 build/libs。以压缩文件形式打开生成的 jar,查看是否包含 hutool-all:(目录:BOOT-INF/lib

image-cpmq.png

运行 Jar,查看是否能打印出 hello world

bash
1
java -jar .\spring-boot-3-gradle-kotlin-0.0.1-SNAPSHOT.jar

image-gtam.png

验证结束,无问题。