Spring boot添加本地Jar包


Spring boot添加本地Jar包

之前文章《IDEA Java 项目添加本地 Jar 包》写过如何在 IDEA 中添加本地 Jar 包依赖。那些方式同样适用于 IDEA 下的 Spring boot 项目,但是有一点问题:

那些不通过 Maven 方式添加的 Jar 依赖只能在 IDEA 开发阶段适用,如果要打包成 Jar 包或 War 包放到外部部署,该 Jar 不会被打包进去。

那么如何解决上述问题呢?下面讨论基于 Maven 的解决方法

方式一、通过 mvn install 命令将 Jar 添加到本地仓库

这种方式在那篇《IDEA Java 项目添加本地 Jar 包》 最后有介绍。这里就不再赘述。

方式二、POM 文件安装并添加依赖

首先在 resources 下创建 lib 目录(这个目录可以随意指定),并将要依赖的 Jar 拷贝到该目录,如下:

Spring boot添加本地Jar包-打不死的小强

接着在 pom.xml 文件加入 maven-install-plugin 插件,代码如下:

<!-- 在执行 clean 时将自定义 jar 包添加到本地 maven 仓库,用于上面添加 dependency 依赖-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>install-encryption-model</id>
            <phase>clean</phase>
            <configuration>
                <file>${basedir}/src/main/resources/lib/encryption.jar</file>
                <repositoryLayout>default</repositoryLayout>
                <groupId>com.huatec.auth</groupId>
                <artifactId>encryption</artifactId>
                <version>1.0-SNAPSHOT</version>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
                    </goals>
        </execution>
    </executions>
</plugin>

接着添加 dependency,代码如下:

<!-- 引入自定义 jar 包-->
<dependency>
    <groupId>com.huatec.auth</groupId>
    <artifactId>encryption</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

这样,先执行mvn clean , 再执行mvn install 打出来的 Jar 包里就包含刚添加的本地 Jar 包了。

后语

网上有另一种方式,pom.xml 中配置如下:

<dependency>
    <groupId>com.huatec.auth</groupId>
    <artifactId>encryption</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>system</scope>
    <systemPath>
        ${project.basedir}/src/main/resources/lib/encryption.jar
    </systemPath>
</dependency>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

这种方式我试了,打出来的 Jar 中也包含要依赖的本地 Jar,但是在执行mvn cleanmvn install的时候会有警告,如下:

[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.huatec.auth.license:license:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.huatec.auth:encryption:jar should not point at files within the project directory, ${project.basedir}/src/main/resources/lib/encryption.jar will be unresolvable by dependent projects @ line 89, column 14
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.

可以看出,maven 官方不推荐使用 <dependency> 下<systemPath> 这种方式。所以还是老老实实使用最开始那种方式吧。

当然推荐使用的还是方式一:直接使用mvn install命令将本地 Jar 安装到 maven 本地仓库。然后直接 dependency 引用。

end



发表评论

邮箱地址不会被公开。 必填项已用*标注