Maven打包时将本地 jar 加入 classpath

在使用 maven 编译项目时,我们有时候会需要引入本地的 jar 包作为依赖(不部署到本地仓库),一般会使用 scope 为 system 的标签来引入,如下所示:

复制代码
<dependency>
  <groupId>com.example</groupId>
  <artifactId>system-dependency</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/system-dependency.jar</systemPath>
</dependency>

此时,pom 中往往还有其他的依赖,是从远端的 maven 仓库下载。如果我们需要同时将本地 jar 和远端下载的 jar 同时注册到 classpath 中,我们可以这么配置插件。

一、配置maven-dependency-plugin

首先利用maven-dependency-plugin插件,将 runtime 和 system 的依赖都拷贝到指定路径,这里是选择的路径是target/lib:

复制代码
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.4.0</version>
                <executions>
                    <execution>
                        <id>copy-runtime-dependency</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeTransitive>false</excludeTransitive>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-system-dependency</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeTransitive>false</excludeTransitive>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <includeScope>system</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

二、配置maven-jar-plugin

然后利用maven-jar-plugin插件将这些所有依赖的 jar 都注册到 classpath中,如下所示:

复制代码
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.xx.YourMain</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>lib/system-dependency.jar</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

这里的lib/system-dependency.jar对应的就是我们通过maven-dependency-plugin插件拷贝之后的新 jar,而不是最初的 systemPath 路径。

三、编译 & 执行

最终通过mvn clean package命令编译之后,就可以在我们自己的 jar的MANIFEST.MF文件中查看,所有的 jar 都被写到classpath,包括本地 jar。然后我们就可以通过 java -jar xxx.jar直接执行我们自己的程序。

相关推荐
麦兜*1 小时前
Spring Boot 集成Reactive Web 性能优化全栈技术方案,包含底层原理、压测方法论、参数调优
java·前端·spring boot·spring·spring cloud·性能优化·maven
胚芽鞘6811 天前
关于java项目中maven的理解
java·数据库·maven
今天又在摸鱼1 天前
Maven
java·maven
老马啸西风1 天前
maven 发布到中央仓库常用脚本-02
java·maven
斐波娜娜1 天前
Maven详解
java·开发语言·maven
胚芽鞘6811 天前
查询依赖冲突工具maven Helper
java·数据库·maven
奔跑吧邓邓子1 天前
从入门到精通:Maven全解析
maven·从入门到精通·全解析
程序员的世界你不懂1 天前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(5)失败用例截图与重试
java·selenium·maven
麦兜*2 天前
Spring Boot秒级冷启动方案:阿里云FC落地实战(含成本对比)
java·spring boot·后端·spring·spring cloud·系统架构·maven
亮1112 天前
Maven 编译过程中发生了 Java Heap Space 内存溢出(OutOfMemoryError)
java·开发语言·maven