Maven将本地jar包生成pom文件,在项目中引用打包到本地仓库。

本文介绍如何从mvnrepository.com查找并手动安装Spring Context Support JAR包至Maven项目。通过具体步骤和实例演示,包括获取依赖信息及使用命令行进行安装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,到 http://mvnrepository.com/ 这个网,在搜索栏中输入你要搜索的 JAR 包的关键字来搜索,下面直接贴图






以 spring-context-support-3.1.0.RELEASE.jar 为例,上图中已经给出这个 jar 包的 groupId,artifactId,version信息,
手动安装的时候这些信息不要改,否则 Maven 项目移植的话,jar 包下载就会失败。顺便把这信息帖下面,方便对照:

< dependency >
     < groupId >org.springframework </ groupId >
     < artifactId >spring-context-support </ artifactId >
     < version >3.1.0.RELEASE </ version >
</ dependency >

Maven 安装 JAR 包的命令是:

mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar


例如:
我下载的这个 jar 包是放到了 D:\mvn 目录下(D:\mvn\spring-context-support-3.1.0.RELEASE.jar)
那么我在 cmd 中敲入的命令就应该是:

mvn install:install-file -Dfile=D:\mvn\spring-context-support-3.1.0.RELEASE.jar -DgroupId=org.springframework -DartifactId=spring-context-support -Dversion=3.1.0.RELEASE -Dpackaging=jar


回车,显示安装成功:



<think>我们正在处理一个关于Maven的问题:如何将本地JAR打包Maven项目中。根据提供的引用,我们可以总结出几种方法,并给出步骤。 方法一:将本地JAR安装到本地Maven仓库 这是推荐的方法,因为它允许你像管理其他依赖一样管理本地JAR。 步骤: 1. 使用mvn install:install-file命令将本地JAR安装到本地仓库。 命令格式: ``` mvn install:install-file -Dfile=<path-to-jar> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar ``` 例如: ``` mvn install:install-file -Dfile=./lib/example.jar -DgroupId=com.example -DartifactId=example -Dversion=1.0.0 -Dpackaging=jar ``` 2. 在项目pom.xml中添加依赖: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> </dependency> ``` 方法二:使用system作用域(不推荐,因为可能导致可移植性问题) 步骤: 1. 将JAR放在项目目录中,例如lib目录。 2. 在pom.xml中添加依赖,并指定作用域为system: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/example.jar</systemPath> </dependency> ``` 3. 注意:使用system作用域的依赖不会被传递,也不会被打包生成JAR中(除非使用下面的插件)。 方法三:使用Maven Shade插件将依赖打包到最终的JAR中(适用于方法一和方法二,但方法二需要额外配置) 如果你使用方法一,那么常规的打包插件(如maven-assembly-plugin或maven-shade-plugin)就可以将依赖打包进去。 如果你使用方法二,那么需要配置maven-shade-plugin来含system作用域的依赖。 配置maven-shade-plugin的示例(在pom.xml的build/plugins部分): ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!-- 如果需要含system作用域的依赖,则添加以下配置 --> <includeSystemScope>true</includeSystemScope> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 但是,注意引用[4]中提到,即使配置了编译时使用本地jar打包时可能还会报错。所以更推荐使用方法一,然后使用shade插件打包。 另外,引用[4]还提到在编译插件中配置extdirs,但这种方法已经过时,而且不推荐。我们应优先使用安装到本地仓库的方式。 总结: 推荐使用第一种方法,将本地JAR安装到本地仓库,然后作为普通依赖引入,再使用shade插件打包(如果需要uber jar)。这样能保证项目的可移植性。 相关问题: 1. 如何将本地JAR安装到Maven本地仓库? 2. 使用system作用域引入本地JAR有什么缺点? 3. 如何配置Maven Shade插件来打包所有依赖?</think>### 如何在 Maven 项目中引入本地 JAR 打包到最终构建 #### 方法一:安装到本地仓库(推荐) 1. **将 JAR 安装到 Maven 本地仓库** 执行命令(参数需替换为实际值): ```shell mvn install:install-file -Dfile=路径/名.jar \ -DgroupId=自定义GroupId \ -DartifactId=自定义ArtifactId \ -Dversion=版本号 \ -Dpackaging=jar ``` 示例: ```shell mvn install:install-file -Dfile=./lib/algo-utils.jar \ -DgroupId=com.example \ -DartifactId=algo-utils \ -Dversion=1.0.0 ``` 2. **在 `pom.xml` 中添加依赖** ```xml <dependency> <groupId>com.example</groupId> <artifactId>algo-utils</artifactId> <version>1.0.0</version> </dependency> ``` 此时 JAR 会像公共仓库依赖一样参与编译和打包[^2]。 --- #### 方法二:使用 `system` 作用域(需配合打包插件) 1. **将 JAR 放入项目目录**(如 `lib/`) 2. **在 `pom.xml` 声明依赖** ```xml <dependency> <groupId>com.example</groupId> <artifactId>local-lib</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/local-lib.jar</systemPath> </dependency> ``` 3. **配置 Maven Shade 插件打包** ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals><goal>shade</goal></goals> <configuration> <includeSystemScope>true</includeSystemScope> <!-- 关键配置 --> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 执行 `mvn package` 后,本地 JAR 会被含在最终 JAR 中[^2][^3]。 --- #### 方法三:直接编译含(不推荐) ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgs> <arg>-extdirs</arg> <arg>${project.basedir}/lib</arg> <!-- 指定JAR目录 --> </compilerArgs> </configuration> </plugin> </plugins> </build> ``` > ⚠️ 注意:此方式仅解决编译问题,打包仍需额外配置 Shade 插件[^4]。 --- ### 最佳实践建议 1. **优先选择方法一** 将本地 JAR 安装到仓库可保持项目结构清晰,避免路径问题[^2]。 2. **避免直接引用文件路径** `system` 作用域会导致项目可移植性降低(如 CI/CD 环境可能缺失文件)[^3]。 3. **关键插件配置** Shade 插件的 `<includeSystemScope>true</includeSystemScope>` 是打包 `system` 依赖的核心[^2]。 最终项目结构示例: ``` project-root ├── lib/ │ └── local-lib.jar # 本地JAR存放位置 ├── src/ └── pom.xml # 配置依赖和插件 ``` 执行 `mvn clean package` 后,在 `target/` 目录生成JAR 文件含所有本地依赖。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值