我可以在命令行中添加Maven仓库吗?

我知道我可以在〜/ .m2 / settings.xml中添加Maven仓库来获取依赖关系。 但有没有可能使用命令行添加一个存储库,如下所示:

mvn install -Dmaven.repository=http://example.com/maven2 

我想这样做的原因是因为我正在使用持续集成工具,我可以完全控制它用来调用maven的命令行选项,但是为运行集成工具的用户pipe理settings.xml只是一点点的麻烦。

你可以做到这一点,但是你可能比其他人说的更好。

在命令行中,您可以为本地存储库指定一个属性,为远程存储库指定另一个存储库。 远程仓库将有所有的默认设置

以下示例指定了两个远程存储库和一个自定义本地存储库。

 mvn package -Dmaven.repo.remote=http://www.ibiblio.org/maven/,http://myrepo -Dmaven.repo.local="c:\test\repo" 

Maven项目对象模型(POM)的目标之一是捕获所有可靠地再现工件所需的信息,因此传递影响工件创build的设置是非常令人沮丧的。

为了实现你的目标,你可以在每个项目中检查你的用户级settings.xml文件,并使用-s(或–settings)选项将它传递给构build。

我不确定你是否可以使用命令行来完成。 另一方面,您可以在pom.xml中添加存储库,如以下示例所示。 使用这种方法你不需要改变〜/ .m2 / settings.xml文件。

  <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> ... <repositories> <repository> <id>MavenCentral</id> <name>Maven repository</name> <url>http://repo1.maven.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> ... <repository> <id>Codehaus Snapshots</id> <url>http://snapshots.repository.codehaus.org/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>false</enabled> </releases> </repository> </repositories> ... <pluginRepositories> <pluginRepository> <id>apache.snapshots</id> <name>Apache Snapshot Repository</name> <url> http://people.apache.org/repo/m2-snapshot-repository </url> <releases> <enabled>false</enabled> </releases> </pluginRepository> <pluginRepository> <id>Codehaus Snapshots</id> <url>http://snapshots.repository.codehaus.org/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>false</enabled> </releases> </pluginRepository> </pluginRepositories> ... </project> 

正如@Jorge Ferreira所说的那样,将你的存储库定义放在pom.xml中。 使用configuration文件通过命令行select要使用的存储库:

 mvn deploy -P MyRepo2 mvn deploy -P MyRepo1 

我在这里假设你问这个是因为你偶尔想要为你的构build添加一个新的第三方仓库。 我当然可能错了… 🙂

在这种情况下,最好的select是使用一个托pipe的代理,如artifactory或nexus。 然后在settings.xml中进行一次性更改,将其设置为世界的镜像。

您需要添加的任何第三方回购可以通过代理进行处理。

我以前没有真正使用过maven 2,因为maven 2的一些问题,我们的系统仍然在maven 1.x上工作。

但是,看看Maven 2的文档似乎没有任何特定的系统属性。 但是,你可以使用系统属性build立一个到你的poms /设置。 请参阅http://maven.apache.org/settings.html的系统属性部分;

所以你的设置文件中有$ {maven.repository},然后像上面那样使用-Dmaven.repository。

我不确定这是否可行,但有一些调整,我相信你可以拿出一些东西。

创build一个具有所需存储库设置的POM,然后使用项目POM中的父元素来inheritance其他存储库。 当一组项目属于一个团队时,使用“组织”POM还有其他好处。