运维系列之Maven引用本地Jar包

十点数据 1年前 ⋅ 1462 阅读

maven项目中引用本地第三方jar包的方法,大致分为两种。第一种是把本地jar通过命令行的方式,加入到maven库中,然后项目pom.xml文件中直接引用。这种方法的确定是其他人无法使用该jar包;第二种就是直接把jar添加到项目目录下,如lib目录下,然后在pom.xml文件中引用。

第一种方法:加入到maven库中

执行命令格式为:
	mvn install:install-file -Dfile=jar包的位置 -DgroupId=pom.xml中的groupId -DartifactId=pom.xml中的artifactId -Dversion=pom.xml中的version -Dpackaging=jar
如:mvn install:install-file -Dfile=D:maven-jarppspdto-ppsp.jar -DgroupId=dto.ppsp -DartifactId=dto-ppsp -Dversion=3.1.0.RELEASE -Dpackagin=jar
pom.xml中引用为:
<dependency>    
	<groupId>dto.ppsp</groupId>    
	<artifactId>dto-ppsp</artifactId> 
	<version>3.1.0.RELEASE</version>
</dependency>

第二种方法:

1、首先我在项目根目录中创建一个lib文件夹,将jar包拷贝到lib文件夹下

QQ截图20191215183557.png

2、然后我们在maven的pom.xml中配置
<dependency>
	<groupId>jacob</groupId>
	<artifactId>jacob</artifactId>
	<version>1.0</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/lib/jacob/jacob.jar</systemPath>
</dependency>
这里的groupId和artifactId以及version都是可以随便填写的 ,scope必须填写为system,而systemPath我们现在我们jar包的地址就可以了

3、最后我们必须在maven打包的过程中加入我们这个jar包。因为项目运行的时候需要这个Jar,并且我们得拷贝在WEB-INF/lib目录下
<build>
	<resources>
		<resource>
			<directory>lib</directory>
			<targetPath>./WEB-INF/lib/</targetPath>
			<includes>
				<include>**/*.jar</include>
			</includes>
		</resource>
	</resources>
	<plugins>
		<plugin>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.7</source>
				<target>1.7</target>
				<encoding>UTF-8</encoding>
				<compilerArguments>
					<extdirs>lib</extdirs>
				</compilerArguments>
			</configuration>
		</plugin>

全部评论: 0

    我有话说: