同福

SpringBoot项目打包成jar库包给其他项目引用的方法【20211209】

介绍

介绍

福哥建立了一个tfspring项目,把一些常用的功能封装了一下,基本可以达到一行命令搞定的程度!这些常用的功能福哥希望在更多的项目里面使用它们,比较笨的办法就是复制全部java源程序到新的项目里面,不过,这样会使得项目变大,编译变慢。

其实还有一个办法,就是把tfspring打包成一个jar库包,拿给其他项目使用,就像我们添加的各种依赖库包一样。

库包信息

包括groupId、artifactId、version都要设置一些,在其他程序引入我们制作的库包的时候需要填写这些信息。

<groupId>net.tongfu</groupId>
<artifactId>tfspring</artifactId>
<version>1.0.0-RELEASE</version>

JDK版本

JDK版本要统一,这里涉及到四个点需要确认!

pom.xml

home/topic/2021/1206/00/8a1bece432c5f7668d9551a5171ab6bb.png

打包插件

编译插件不能使用默认的,需要换成maven-compiler插件。

Maven Compiler

因为福哥是要打包项目成jar库包给其他项目引用,如果还使用默认的spring-boot-maven插件就会有问题,所以这里需要将spring-boot-maven插件删除掉,添加maven-compiler插件。

删除spring-boot-maven插件。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

添加maven-compliler插件。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>13</source>
        <target>13</target>
        <encoding>UTF-8</encoding>
        <compilerArgument>-Xlint:unchecked</compilerArgument>
    </configuration>
</plugin>

SpringBoot依赖库包

因为我们是基于SpringBoot框架开发的jar库包,所以我们的项目里面一定会有很多groupId为org.springframework.boot的依赖库包,这些库包一定不要打包到jar库包里面。

scope

这里可以使用scope来告诉编译器不要将这个依赖打包进来。福哥给出了几个常用的SpringBoot依赖,大家可以参考来排除自己项目里的SpringBoot依赖。

<!-- jdbc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
    <version>2.1.7.RELEASE</version>
    <scope>provided</scope>
</dependency>

<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.1.7.RELEASE</version>
    <scope>provided</scope>
</dependency>

<!-- freemarker -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    <scope>provided</scope>
</dependency>

构建

普通构建

通过下面的命令就可以完成打包成jar库包的构建。

mvn clean package -D maven.test.skip=true

Assembly构建

使用assembly-plugin插件可以将tfspring依赖的库包也一并打包到jar库包里面,这样引用tfspring库包的项目就可以不用重复导入这些库包了。

添加插件

添加assembly-plugin插件。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

构建

在普通构建的基础之上增加assembly:single步骤。

mvn clean package assembly:single -D maven.test.skip=true

引用

Maven

简单的方法可以通过maven安装到本地环境当中,命令如下:

mvn install:install-file -D groupId=net.tongfu -D artifactId=tfspring -D version=1.0.0-RELEASE -D packaging=jar -D file=target\tfspring-1.0.0-RELEASE-jar-with-dependencies.jar

home/topic/2021/1210/16/5b144625b2a4a2730fa22ca15c32983b.png

POM

还可以在POM里面直接引用,需要将jar放到项目目录下面,并且将路径配置到dependency里面。

<!-- tfspring -->
<dependency>
    <groupId>net.tongfu</groupId>
    <artifactId>tfspring</artifactId>
    <version>1.0.0-RELEASE</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/tfspring-1.0.0-RELEASE.jar</systemPath>
</dependency>

总结

今天福哥带着大家学习了如何将SpringBoot框架打包成jar库包提供给其他下面引用的技巧,这个技巧可以帮助我们积累自己的经验形成自己的jar库包。

随着大家编写代码的量的不断增加,大家的编程水平也会逐步提高起来,慢慢地大家会有一些用得习惯顺手的类会在不同的项目里面使用它们,这个时候如果可以把这些类打包成jar库包用起来就爽了~~