Maven項(xiàng)目打包時,如果遇到需要添加本地jar包依賴的時候,可以選擇兩種方法:
1. 安裝到本地倉庫
第一種方法比較常規(guī),適用于需要添加的jar包也是由maven項(xiàng)目導(dǎo)出,含有pom文件的時候。只需要將jar包安裝到本地maven倉庫下,然后添加依賴即可。
(1)安裝到本地倉庫,執(zhí)行以下命令(其中的-Dfile/-DgroupId/-DartifactId/-Dversion項(xiàng)根據(jù)pom文件內(nèi)容填寫):
1
|
mvn install:install-file -Dfile=xxxxx.jar -DgroupId=xxx.xxx.xxx -DartifactId=xxxxx -Dversion=1.0.0 -Dpackaging=jar |
(2)安裝之后可以在本地倉庫中找到對應(yīng)的jar包。然后將對應(yīng)的依賴信息插入到工程的pom文件即可:
1
2
3
4
5
|
< dependency > < groupId >xxx.xxx.xxx</ groupId > < artifactId >xxxxx</ artifactId > < version >1.0.0</ version > </ dependency > |
2. dependency中指定scope="system"和本地jar包路徑
這種方法適用于其他方式導(dǎo)出的jar包,jar包中不含有pom信息,從而無法安裝進(jìn)本地倉庫的情況。做法是:先配置本地jar包依賴,然后在build時將設(shè)置將jar包導(dǎo)出,同時配置manifest。
(1)配置本地jar包依賴(systemPath指向本地jar包路徑):
1
2
3
4
5
6
7
|
< dependency > < groupId >com.amazonaws</ groupId > < artifactId >aws-java-sdk-s3</ artifactId > < version >1.11.0</ version > < scope >system</ scope > < systemPath >${project.basedir}/lib/xxx.jar</ systemPath > </ dependency > |
(2)在<build>的spring-boot-maven-plugin中設(shè)置將本地jar包導(dǎo)出到項(xiàng)目最終的依賴庫中:
1
2
3
4
5
6
7
|
< plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < includeSystemScope >true</ includeSystemScope > </ configuration > </ plugin > |
(3)如果項(xiàng)目使用maven-jar-plugin插件打包的話,還需要在manifectEntries中添加對應(yīng)的jar包信息;否則雖然jar包導(dǎo)出了,但是項(xiàng)目生成的MANIFEST.MF文件中沒有對應(yīng)的依賴信息,也會導(dǎo)致運(yùn)行時找不到對應(yīng)的class。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-jar-plugin</ artifactId > < configuration > < archive > < manifest > < addClasspath >true</ addClasspath > < classpathPrefix >lib/</ classpathPrefix > < mainClass >XXXX</ mainClass > </ manifest > < manifestEntries > < Class-Path >./ lib/xxxxx.jar</ Class-Path > </ manifestEntries > </ archive > < outputDirectory > ${project.build.directory}/XXXXX </ outputDirectory > </ configuration > </ plugin > |
(4)最后附上一個項(xiàng)目完整的<build>配置(該配置可以將最終生成的jar包和依賴庫、配置文件分開)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
< build > < finalName >XXXXX</ finalName > < sourceDirectory >src/main/java</ sourceDirectory > < resources > < resource > < directory >src/main/resources</ directory > < targetPath >${project.build.directory}/XXXXX</ targetPath > < excludes > < exclude >**/*.java</ exclude > </ excludes > </ resource > </ resources > < testSourceDirectory >src/test/java</ testSourceDirectory > < testResources > < testResource > < directory >src/test/resources</ directory > < filtering >true</ filtering > < excludes > < exclude >**/*.java</ exclude > </ excludes > </ testResource > </ testResources > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < includeSystemScope >true</ includeSystemScope > </ configuration > </ plugin > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-surefire-plugin</ artifactId > < configuration > < skipTests >true</ skipTests > </ configuration > </ plugin > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-dependency-plugin</ artifactId > < executions > < execution > < id >copy-dependencies</ id > < phase >package</ phase > < goals > < goal >copy-dependencies</ goal > </ goals > < configuration > < outputDirectory > ${project.build.directory}XXXXX/lib </ outputDirectory > </ configuration > </ execution > </ executions > </ plugin > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-jar-plugin</ artifactId > < configuration > < archive > < manifest > < addClasspath >true</ addClasspath > < classpathPrefix >lib/</ classpathPrefix > < mainClass >xxx.xxx.XXXXX</ mainClass > </ manifest > < manifestEntries > < Class-Path >./ lib/xxxxx.jar</ Class-Path > </ manifestEntries > </ archive > < outputDirectory > ${project.build.directory}/XXXXX </ outputDirectory > </ configuration > </ plugin > </ plugins > </ build > |
到此這篇關(guān)于Maven pom.xml 添加本地jar包依賴以及打包方法的文章就介紹到這了,更多相關(guān)Maven pom.xml jar包內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/strugglion/p/12513956.html