Maven 项目添加本地jar包的三种方式

本文介绍在Maven项目中添加本地jar包的四种方法:手动添加到本地仓库、添加为systemscope依赖、创建独立的本地仓库及使用Nexus仓库管理器。

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

Maven 项目添加本地jar包的三种方式

翻译自3 ways to add local jar to maven project

[TOC]

简介

在构建 Maven 项目时,有时候需要导入本地的jar包,本文介绍了三种添加本地jar包的方法。

  1. 手动添加jar包到本地仓库
  2. 添加为 system scope 依赖
  3. 创建一个不同的本地仓库
  4. 使用 Nexus 仓库管理器

1. 手动添加jar包到本地仓库

第一种解决方法是通过 Maven goal install:install-file 命令将jar包安装到本地仓库。该插件的使用方法很简单,如下所示:

mvn install:install-file -Dfile=<path-to-file>

注意:我们没有定义依赖包的 groupId、articleId、version、packaging 这些属性。其实,从 Maven-install-plugin 插件2.5版本开始,这些信息可以从特定的pom文件中获取。

这些属性信息同时也可以添加到命令行中:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

其中:

<path-to-file> 为 jar 包的文件路径

<group-id> 为 jar 包的 GroupId

<artifact-id> 为 jar 包的 ArtifactId

<version> 为 jar 包的版本号

示例:

mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0

同过添加 pom 依赖包的方式,可以将上面的 jar 包引入到 maven 项目中:

<dependency>
	<groupId>com.roufid.tutorials</groupId>
	<artifactId>example-app</artifactId>
	<version>1.0</version>
</dependency>

这种添加本地依赖包的方式代价非常高。因为它将依赖文件直接添加到了本地 maven 仓库中,如果有一天你切换了本地仓库,那么你必须重新安装一遍 jar 包。同时,也不利于项目的移植和协同开发。

另一种方案是在 pom 文件中使用 maven-install-plugin 插件的方式,这种方式会在 maven initialize 阶段添加相应的 jar 包。为了实现上述效果,你必须定义好需要安装 jar 包的路径。最好的方式是将所需要安装的 jar 包放置在项目根路径的某个文件夹内(和 pom.xml 同级目录中)。

我们假设 jar 包存放位置为 <PROJECT_ROOT_FOLDER>/lib/app.jar。以下是 maven-install-plugin 的配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.roufid.tutorials</groupId>
                <artifactId>example-app</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/app.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

${basedir} 指的是 pom.xml 所在的根路径。

在添加上述代码的时候,你可能会遇到一个错误(没遇到>_<),那么添加下面的配置来允许 maven 生命周期的映射关系:

<pluginManagement>
	<plugins>
		<!--This plugin's configuration is used to store Eclipse m2e settings only. 
			It has no influence on the Maven build itself. -->
		<plugin>
			<groupId>org.eclipse.m2e</groupId>
			<artifactId>lifecycle-mapping</artifactId>
			<version>1.0.0</version>
			<configuration>
				<lifecycleMappingMetadata>
					<pluginExecutions>
						<pluginExecution>
							<pluginExecutionFilter>
								<groupId>org.codehaus.mojo</groupId>
								<artifactId>aspectj-maven-plugin</artifactId>
								<versionRange>[1.0,)</versionRange>
								<goals>
									<goal>test-compile</goal>
									<goal>compile</goal>
								</goals>
							</pluginExecutionFilter>
							<action>
								<execute />
							</action>
						</pluginExecution>
						<pluginExecution>
							<pluginExecutionFilter>
								<groupId>
									org.apache.maven.plugins
								</groupId>
								<artifactId>
									maven-install-plugin
								</artifactId>
								<versionRange>
									[2.5,)
								</versionRange>
								<goals>
									<goal>install-file</goal>
								</goals>
							</pluginExecutionFilter>
							<action>
								<execute>
									<runOnIncremental>false</runOnIncremental>
								</execute>
							</action>
						</pluginExecution>
					</pluginExecutions>
				</lifecycleMappingMetadata>
			</configuration>
		</plugin>
	</plugins>
</pluginManagement>

2. 直接将依赖包添加为 system 范围

这种方案(不是特别好方案)通过添加 system scope 并指向 jar 包的全路径。假如 jar 包的路径为 <PROJECT_ROOT_FOLDER>/lib , 那么它的配置如下所示:

<dependency>
	<groupId>com.roufid.tutorials</groupId>
	<artifactId>example-app</artifactId>
	<version>1.0</version>
	<scope>system</scope>
	<systemPath>${basedir}/lib/yourJar.jar</systemPath>
</dependency>

${basedir} 指的是 pom.xml 所在的根路径。

3. 创建一个不同的本地仓库

第三种方案和第一种方案相似,不同在于 jar 包安装的仓库位置。假如我们有一个本地仓库,名称为 maven-repository,位置在 ${basedir}(pom.xml所在的根路径)。第一步要做的就是将 jar 包部署到本地仓库,如下所示:

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true

通常 Maven 命令 deploy:deploy-file 会将 artificial 安装在远端仓库中,但是在本例子中仓库地址在本地。 添加完 jar 包后需要在对应的 pom 文件添加对应的仓库:

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>

然后添加对应的依赖:

<dependency>
	<groupId>com.roufid.tutorials</groupId>
	<artifactId>example-app</artifactId>
	<version>1.0</version>
</dependency>

4. 使用Nexus仓库管理工具

最好的方式当然是建立自己的私有云仓库了,怎么安装?戳这里

转载于:https://my.oschina.net/u/3655450/blog/1833699

### 在 IntelliJ IDEA 中为 Maven 项目添加本地 JAR 文件作为依赖 对于希望在 IntelliJ IDEA 的 Maven 项目中加入本地 JAR 文件的情况,有两种主要方法可以实现这一目标。 #### 方法一:通过命令行安装本地 JAR本地仓库 可以直接利用 Maven 命令将本地JAR 安装至本地仓库。具体操作是在 jar 所在的目录下执行如下指令: ```bash mvn install:install-file -Dfile={artifactId}-{version}.jar -DgroupId={groupId} -DartifactId={artifactId} -Dversion={version} -Dpackaging=jar ``` 这条命令会把指定路径下的 JAR 文件按照给定的信息注册到本地 Maven 仓库中[^1]。 之后,在项目的 `pom.xml` 文件里声明相应的依赖即可正常使用该库。 #### 方法二:直接配置 pom.xml 使用 system 范围 另一种方式是不经过本地仓库而直接引用本地磁盘上的 JAR 文件。这需要修改 `pom.xml` 来定义一个新的依赖项,并设置其作用域为 `system` 同时提供具体的文件位置: ```xml <dependency> <!-- 替换下面的内容为你实际使用的 groupId 和 artifactId --> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${basedir}/path/to/your/local.jar</systemPath> </dependency> ``` 这里 `${basedir}` 表达的是当前项目的根目录路径;`path/to/your/local.jar` 应替换成为相对于项目根目录的实际 JAR 文件的位置[^4]。 需要注意的是,这种方法虽然简单快捷,但在团队协作或者跨环境部署时可能会遇到问题,因为其他开发者可能无法找到相同的绝对路径。因此推荐优先考虑第一种方案即通过命令行安装的方式处理。 #### 配置完成后刷新 Maven 项目结构 无论采用哪种方法完成上述步骤后都应当同步更新 IDE 对于 Maven 项目的理解。可以通过点击右上角的小象图标旁边的绿色箭头按钮来触发重新加载过程,确保所有更改生效[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值