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

原文:3 ways to add local jar to maven project

1. 手动安装jar包至本地Maven仓库

第一种方式即手动添加jar包至本地Maven仓库,需要执行的Maven任务是:install:install-file,命令如下:

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

请注意,我们没有在install时指定jar包的groupId、artifactId、version和packaging。这是因为自Maven-install-plugin的version 2.5之后,这些信息可以从指定的pom文件中选择性获取。

这些信息同样可以使用命令行指定:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>
  • <path-to-file>: Path to the JAR to install
  • <group-id>: Group id of the JAR to install
  • <artifact-id>: Artifact id of the JAR to install
  • <version>: Version of the JAR

示例:

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

然后,你就可以在你的项目的pom.xml文件中用下面的配置添加该依赖:

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

然而,这种方式非常繁琐。因为一旦你对jar的源文件做了修改,你就不得不重新安装该jar至本地仓库。并且,所有使用了该jar的开发者都必须在他自己的电脑上执行这一安装步骤。这个jar的便携性需要重新考虑。

一个改进的安装方式配置pom.xml中的maven-install-plugin,让其在Maven项目的初始化阶段自动安装该jar。要这样做,你必须指定jar的文件路径,最好是将这个jar包放在项目跟路径下。

假定这个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} represents the directory containing pom.xml.

上面的配置可能会导致一些错误,当这些错误发生时,添加下面的配置来允许Maven项目的生命周期映射(lifecycle mapping):

<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 scope的方式添加依赖

这是一个比较“脏”的方式,需要将依赖作为system scope来添加,使用时需要引用其绝对路径。

假定jar包位于<PROJECT_ROOT_FOLDER>/lib,则pom.xml中添加依赖的配置如下:

<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} represents the directory containing pom.xml.

3. 创建一个独立的本地Maven仓库

这种方式与第一种类似,不同之处在于jar包安装的仓库位置不同。

假定这个新的Maven仓库名为maven-repository,且位于项目跟路径下。首先,你需要将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

通常,deploy:deploy-file会将项目安装在远程仓库,但此处是安装在本地。

安装完成之后,你需要在你的项目中添加仓库:

<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>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值