将jar安装到本地Maven仓库

转自:https://blog.csdn.net/ShuSheng0007/article/details/78547264/

第一种方案

1
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
  • <path-to-file>: 要安装的JAR的本地路径
  • <group-id>:要安装的JAR的Group Id
  • <artifact-id>: 要安装的JAR的 Artificial Id
  • <version>: JAR 版本
  • <packaging>: 打包类型,例如JAR

最好在pom.xml文件所在的目录运行上述命令,个人经验不在根目录运行有时会安装不成功

执行上述命令后,我们就可以在pom.xml文件中引用

1
2
3
4
5
<dependency>
<groupId>com.baidu.app</groupId>
<artifactId>bdpush</artifactId>
<version>3.0.1</version>
</dependency>

总结:这种方法弊端较大,程序的可维护性以及移植性较低。例如当你改变本地Maven仓库时需要重新安装。如果引用此JAR的项目是多人协调工作的项目,则每个人都要将其安装在自己的本地仓库。

解决办法

可以将此JAR文件放在工程的根目录下,让其随着项目走,然后在pom.xml文件中使用maven-install-pluginMaven初始化阶段完成安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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.baidu.app</groupId>
<artifactId>bdpush</artifactId>
<version>3.0.1</version>
<packaging>jar</packaging>
<file>${basedir}/lib/bdpush-3.0.1.ja</file>
</configuration>
</execution>
</executions>
</plugin>

${basedir}表示pom.xml文件所在的目录

第二种方案

第二种方法比较粗暴简单,具体为将依赖设置为系统域,通过完全路径引用。例如要引用的JAR文件在 <PROJECT_ROOT_FOLDER>/lib下,那么使用如下方法添加依赖

1
2
3
4
5
6
7
<dependency>
<groupId>com.baidu.app</groupId>
<artifactId>bdpush</artifactId>
<version>3.0.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/bdpush-3.0.1.ja</systemPath>
</dependency>

${basedir}表示pom.xml文件所在的目录,例如你的JAR文件在D盘下的jarLibs里面,就将${basedir}替换为D:/jarLibs即可。

这种方法我自己在SpringBoot项目中打包成war文件时,没有成功打包到里面

第三种方案

第三种方案与第一种差不多,不同的是JAR文件被安装在一个单独的仓库里。这个本地仓库建在你项目的根目录下,随着项目走。例如

1:我们在${basedir}pom.xml文件所在路径)目录下建立一个叫maven-repository的本地仓库。

2:使用如下命令安装我们要引用的JAR到此仓库中

1
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

3:在pom.xml中如下使用

  • 申明仓库
1
2
3
4
5
6
<repositories>
<repository>
<id>maven-repository</id>
<url>file:///${project.basedir}/maven-repository</url>
</repository>
</repositories>
  • 然后添加引用
1
2
3
4
5
<dependency>
<groupId>com.baidu.app</groupId>
<artifactId>bdpush</artifactId>
<version>3.0.1</version>
</dependency>

Maven常用命令

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
[root@zhang ~]# mvn --help

usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode (disables output color)
-b,--builder <arg> The id of the build strategy to
use
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a system property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml)
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-gt,--global-toolchains <arg> Alternate path for the global
toolchains file
-h,--help Display help information
-l,--log-file <arg> Log file where all build output
will go (disables output color)
-llr,--legacy-local-repository Use Maven 2 Legacy Local
Repository behaviour, ie no use of
_remote.repositories. Can also be
activated by using
-Dmaven.legacyLocalRepo=true
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-ntp,--no-transfer-progress Do not display transfer progress
when downloading or uploading
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-t,--toolchains <arg> Alternate path for the user
toolchains file
-T,--threads <arg> Thread count, for instance 2.0C
where C is core multiplied
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-v,--version Display version information
-V,--show-version Display version information
WITHOUT stopping build
-X,--debug Produce execution debug output
1
2
3
4
mvn package \
-Dmaven.repo.local=C:\Users\lenovo\.m2\Repository4 \
-Dmaven.test.skip=true \
-f pom.xml
  • -Dmaven.repo.local 强制使用本地源
  • -Dmaven.test.skip 跳过单测
  • -f 指定配置文件