Java 如何让 Maven 使用正确的存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2361294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get Maven to use the correct repositories?
提问by user284163
I have just checked out some projects and need to build them, however I installed Maven quite some time ago (6 months maybe?) and really haven't used it since - the pom.xml
for the project I have doesn't have this "http://repo1.maven.org/myurlhere" anywhere in it - it has the absolute url
where the Maven repo is for the project, but Maven is still trying to download from the general Maven repo:
我刚刚检查了一些项目并需要构建它们,但是我很久以前(也许是 6 个月?)安装了 Maven,并且从那以后真的没有使用它 -pom.xml
我的项目没有这个“ http: //repo1.maven.org/myurlhere" 在它的任何地方 - 它具有url
项目的 Maven 存储库的绝对位置,但 Maven 仍在尝试从通用 Maven 存储库下载:
Macintosh:trunk$ mvn clean install
[INFO] Scanning for projects...
Downloading: http://repo1.maven.org/url/project/project/x.x/project-x.x.pom
[INFO] Unable to find resource 'url.project:project:pom:x.x' in repository central (http://repo1.maven.org/)
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
GroupId: url.project
ArtifactId: project
Version: x.x
Reason: Unable to download the artifact from any repository
url.project:project:pom:x.x
from the specified remote repositories:
central (http://repo1.maven.org/)
Can anyone help me with what I'm not doing right?
Basically, I have just checked the projects out from the command line, cd
-ed into the directory and ran mvn clean install
- nothing else.
Any help is greatly appreciated.
任何人都可以帮助我解决我做错的事情吗?
基本上,我刚刚从命令行检查了项目,cd
-ed 进入目录并运行mvn clean install
- 没有别的。
任何帮助是极大的赞赏。
回答by Taylor Leese
Basically, all Maven is telling you is that certain dependencies in your project are not available in the central maven repository. The default is to look in your local .m2 folder (local repository), and then any configured repositories in your POM, and then the central maven repository. Look at the repositories sectionof the Maven reference.
基本上,所有 Maven 告诉您的是,您项目中的某些依赖项在中央 Maven 存储库中不可用。默认是查看您的本地 .m2 文件夹(本地存储库),然后是 POM 中任何已配置的存储库,然后是中央 maven 存储库。查看Maven 参考的存储库部分。
The problem is that the project that was checked in didn't configure the POM in such a way that all the dependencies could be found and the project could be built from scratch.
问题是签入的项目没有以可以找到所有依赖项并且可以从头开始构建项目的方式配置 POM。
回答by Romain Linsolas
By default, Maven will always look in the official Maven repository, which is http://repo1.maven.org.
默认情况下,Maven 将始终在官方 Maven 存储库中查找,即http://repo1.maven.org。
When Maven tries to build a project, it will look in your local repository (by default ~/.m2/repository
but you can configure it by changing the <localRepository>
value in your ~/.m2/settings.xml
) to find any dependency, plugin or report defined in your pom.xml
. If the adequate artifact is not found in your local repository, it will look in all external repositories configured, starting with the default one, http://repo1.maven.org.
当Maven尝试构建一个项目,它将在你的本地仓库(默认情况下~/.m2/repository
,但你可以通过改变配置它<localRepository>
在你的价值~/.m2/settings.xml
)找到任何依赖,插件或在您定义的报告pom.xml
。如果在您的本地存储库中找不到足够的工件,它将在所有配置的外部存储库中查找,从默认的http://repo1.maven.org 开始。
You can configure Maven to avoid this default repository by setting a mirror in your settings.xml
file:
您可以通过在settings.xml
文件中设置镜像来配置 Maven 以避免使用此默认存储库:
<mirrors>
<mirror>
<id>repoMirror</id>
<name>Our mirror for Maven repository</name>
<url>http://the/server/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
This way, instead of contacting http://repo1.maven.org
, Maven will contact your entreprise repository (http://the/server
in this example).
这样http://repo1.maven.org
,Maven 将联系您的企业存储库(http://the/server
在本例中),而不是联系。
If you want to add another repository, you can define a new one in your settings.xml file:
如果要添加另一个存储库,可以在 settings.xml 文件中定义一个新存储库:
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>foo.bar</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>http://new/repository/server</url>
</repository>
</repositories>
You can see the complete settings.xml
model here.
您可以在此处查看完整settings.xml
模型。
Concerning the clean
process, you can ask Maven to run it offline. In this case, Maven will not try to reach any external repositories:
关于这个clean
过程,你可以要求Maven离线运行。在这种情况下,Maven 不会尝试访问任何外部存储库:
mvn -o clean
回答by Pascal Thivent
the pom.xml for the project I have doesn't have this "http://repo1.maven.org/myurlhere" anywhere in it
我的项目的 pom.xml 中的任何地方都没有这个“ http://repo1.maven.org/myurlhere”
Allprojects have http://repo1.maven.org/declared as <repository>
(and <pluginRepository>
) by default. This repository, which is called the centralrepository, is inherited like others default settings from the "Super POM" (all projects inherit from the Super POM). So a POM is actually a combination of the Super POM, any parent POMs and the current POM. This combination is called the "effective POM" and can be printed using the effective-pom
goal of the Maven Help plugin (useful for debugging).
默认情况下,所有项目都将http://repo1.maven.org/声明为<repository>
(和<pluginRepository>
)。这个存储库,称为中央存储库,像其他默认设置一样继承自“超级 POM”(所有项目都继承自超级 POM)。所以一个 POM 实际上是超级 POM、任何父 POM 和当前 POM 的组合。这种组合称为“有效 POM”,可以使用effective-pom
Maven 帮助插件的目标打印(用于调试)。
And indeed, if you run:
事实上,如果你运行:
mvn help:effective-pom
You'll see at least the following:
您至少会看到以下内容:
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
it has the absolute url where the maven repo is for the project but maven is still trying to download from the general maven repo
它具有项目的 maven repo 所在的绝对 url,但 maven 仍在尝试从一般 maven repo 下载
Maven will try to find dependencies in all repositories declared, including in the centralone which is there by default as we saw. But, according to the trace you are showing, you only have onerepository defined (the central repository) or maven would print something like this:
Maven 将尝试在所有声明的存储库中查找依赖项,包括在我们看到的默认情况下存在的中央存储库中。但是,根据您显示的跟踪,您只定义了一个存储库(中央存储库),否则 maven 会打印如下内容:
Reason: Unable to download the artifact from any repository
url.project:project:pom:x.x
from the specified remote repositories:
central (http://repo1.maven.org/),
another-repository (http://another/repository)
So, basically, maven is unable to find the url.project:project:pom:x.x
because it is not available in central.
所以,基本上,maven 无法找到,url.project:project:pom:x.x
因为它在中央不可用。
But without knowing which project you've checked out (it has maybe specific instructions) or which dependency is missing (it can maybe be found in another repository), it's impossible to help you further.
但是如果不知道您签出了哪个项目(它可能有特定的说明)或缺少哪个依赖项(它可能在另一个存储库中找到),就不可能进一步帮助您。
回答by shabby
I think what you have missed here is this:
我认为你在这里错过的是:
https://maven.apache.org/settings.html#Servers
https://maven.apache.org/settings.html#Servers
The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. However, certain settings such as username and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml.
用于下载和部署的存储库由 POM 的存储库和 distributionManagement 元素定义。但是,某些设置(例如用户名和密码)不应与 pom.xml 一起分发。此类信息应存在于构建服务器上的 settings.xml 中。
This is the prefered way of using custom repos. So probably what is happening is that the url of this repo is in settings.xml of the build server.
这是使用自定义存储库的首选方式。所以可能发生的事情是这个 repo 的 url 在构建服务器的 settings.xml 中。
Once you get hold of the url and credentials, you can put them in your machine here: ~/.m2/settings.xml
like this:
一旦你掌握了 url 和凭据,你就可以把它们放在你的机器上:~/.m2/settings.xml
像这样:
<settings ...>
.
.
.
<servers>
<server>
<id>internal-repository-group</id>
<username>YOUR-USERNAME-HERE</username>
<password>YOUR-PASSWORD-HERE</password>
</server>
</servers>
</settings>
回答by bence of outer space
tl;dr
tl;博士
All maven POMs inherit from a base Super POM.
The snippet below is part of the Super POM for Maven 3.5.4.
所有 Maven POM 都继承自基础Super POM。
下面的代码片段是 Maven 3.5.4 的 Super POM 的一部分。
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>