1. 配置maven本地资源库
Maven的本地资源库是用来存储所有项目的依赖关系(插件jar和其他文件,这些文件被Maven下载)到本地文件夹。很简单,当你建立一个Maven项目,所有相关文件将被存储在你的Maven本地仓库。
-
打开
C:\Program Files\apache-maven-3.5.2\conf\settings.xml文件,找到以下内容:<!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> -
在这段话的下面添加自己的本地资源库路径,我的路径设置如下:
<localRepository>C:\Program Files\apache-maven-3.5.2\repository</localRepository> -
如果想要检查本地仓库是否正确配置,可以先执行如下命令:
mvn archetype:generate -DgroupId=com.yiibai -DartifactId=NumberGenerator -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -
配置过程截图:

-
配置完成后,发现
C:\Program Files\apache-maven-3.5.2\repository目录下新增了一些文件夹。由于使用的是maven的中央存储库,下载非常的慢,所以截图只是部分内容。

参考链接:Maven本地资源库
2. 配置maven中央存储库
-
当你建立一个 Maven 的项目,Maven 会检查你的
pom.xml文件,以确定哪些依赖下载。首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源。如果没有找到,然后把它会从默认的 Maven 中央存储库 – https://repo.maven.apache.org/maven2/ 查找下载。 -
从 https://repo.maven.apache.org/maven2/ 中查找下载相应的依赖是非常慢的,因此需要将maven中央存储库,改为
阿里云的maven中央存储库。 -
打开
C:\Program Files\apache-maven-3.5.2\conf\settings.xml文件,找到以下内容:<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> </mirrors> -
在
</mirrors>之前添加以下内容:<mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>https://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> -
重新使用以下命令,发现所以得依赖都是从阿里云下载:
mvn archetype:generate -DgroupId=com.yiibai -DartifactId=NumberGenerator -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
3. 使用mvn命令添加jar到本地仓库
-
当遇到以下情况时,需要手动发出Maven命令包括一个 jar 到 Maven 的本地资源库。
① 要使用的 jar 不存在于 Maven 的中心储存库中。
② 您创建了一个自定义的 jar ,而另一个 Maven 项目需要使用 -
例如,
kaptcha,它是一个流行的第三方Java库,它被用来生成 “验证码” 的图片,以阻止垃圾邮件,但它不在 Maven 的中央仓库中。 -
mvn安装命令详解:
安装指定文件到本地仓库命令:mvn install:install-file -DgroupId=<groupId> : 设置项目代码的包名(一般用组织名) -DartifactId=<artifactId> : 设置项目名或模块名 -Dversion=2.3 : 版本号 -Dpackaging=jar : 什么类型的文件(jar包) -Dfile=C:\Program Files\apache-maven-3.5.2\kaptcha-2.3.jar : 指定jar文件路径与文件名(同目录只需文件名) 安装命令实例: mvn install:install-file "-Dfile=C:\Program Files\apache-maven-3.5.2\kaptcha-2.3.jar" "-DgroupId=com.google.code" "-DartifactId=kaptcha" "-Dversion=2.3" "-Dpackaging=jar" -
安装成功的截图:

-
PS: 最开始使用的命令如下,但一直报错。
mvn install:install-file -Dfile=C:\Program Files\apache-maven-3.5.2\kaptcha-2.3.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar -
报错信息如下:
C:\Users\lucy>mvn install:install-file -Dfile=C:\Program Files\apache-maven-3.5.2\kaptcha-2.3.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.063 s [INFO] Finished at: 2019-03-03T15:33:16+08:00 [INFO] Final Memory: 6M/121M [INFO] ------------------------------------------------------------------------ [ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\Users\lucy). Please verify you invoked Maven from the correct directory. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException -
一定要注意:
C:\Program Files\apache-maven-3.5.2\kaptcha-2.3.jar需要提前下载,并放在对应的目录,不然会报错!!![ERROR] The specified file 'C:\Program Files\apache-maven-3.5.2\kaptcha-2.3.jar' not exists [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.841 s [INFO] Finished at: 2019-03-03T15:35:39+08:00 [INFO] Final Memory: 10M/134M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install-file (default-cli) on project standalone-pom: The specified file 'C:\Program Files\apache-maven-3.5.2\kaptcha-2.3.jar' not exists -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException -
安装完毕后,就在 pom.xml 中声明 kaptcha 的坐标。
<dependency> <groupId>com.google.code</groupId> <artifactId>kaptcha</artifactId> <version>2.3</version> </dependency>
参考链接:
定制库到Maven本地资源库
添加jar包到本地Maven仓库
kaptcha-2.3下载地址
no POM in this directory 的解决办法——Stack Overflow大法好!!!
本文详细介绍了如何配置Maven的本地和中央仓库,包括更改中央仓库为阿里云加速下载,以及如何手动添加自定义Jar包到本地仓库。通过具体步骤和命令示例,帮助读者掌握Maven资源管理技巧。


11万+

被折叠的 条评论
为什么被折叠?



