Java 我可以在命令行中添加 Maven 存储库吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/71030/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:57:06  来源:igfitidea点击:

Can I add maven repositories in the command line?

javamaven-2build-process

提问by Sindri Traustason

I'm aware I can add maven repositories for fetching dependencies in ~/.m2/settings.xml. But is it possible to add a repository using command line, something like:

我知道我可以添加 maven 存储库以在 ~/.m2/settings.xml 中获取依赖项。但是是否可以使用命令行添加存储库,例如:

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

The reason I want to do this is because I'm using a continuous integration tool where I have full control over the command line options it uses to call maven, but managing the settings.xml for the user that runs the integration tool is a bit of a hassle.

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

采纳答案by Rich Seller

You can do this but you're probably better off doing it in the POM as others have said.

您可以这样做,但正如其他人所说,您最好在 POM 中这样做。

On the command line you can specify a property for the local repository, and another repository for the remote repositories. The remote repository will have all default settings though

在命令行上,您可以为本地存储库指定一个属性,并为远程存储库指定另一个存储库。尽管远程存储库将具有所有默认设置

The example below specifies two remote repositories and a custom local repository.

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

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

回答by Jorge Ferreira

I am not sure if you can do it using the command line. You can on the other hand add repositories in the pom.xmlas in the following example. Using this approach you do not need to change the ~/.m2/settings.xmlfile.

我不确定您是否可以使用命令行来完成。另一方面,您可以在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>

回答by Henry B

I haven't really used maven 2 before, our system is still working on maven 1.x because of some issues with maven 2.

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

However, looking at the documentation for maven 2 it seems that there aren't any specific System properties like that. However, you could probably build one into your poms/settings using the System properties. See System properties part of this http://maven.apache.org/settings.html

但是,查看 maven 2 的文档似乎没有任何特定的系统属性。但是,您可能可以使用系统属性在您的 poms/设置中构建一个。请参阅此http://maven.apache.org/settings.html 的系统属性部分

So you'd have ${maven.repository} in your settings file and then use the -Dmaven.repository like you do above.

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

I am unsure as to if this would work, but with some tweaking I am sure you can come up with something.

我不确定这是否可行,但通过一些调整,我相信你可以想出一些办法。

回答by Eduard Wirch

As @Jorge Ferreira already said put your repository definitions in the pom.xml. Use profilesadittionally to select the repository to use via command line:

正如@Jorge Ferreira 已经说过的那样,将您的存储库定义放在 pom.xml 中。使用配置文件附加地通过命令行选择要使用的存储库:

mvn deploy -P MyRepo2

mvn deploy -P MyRepo1

回答by Steve Moyer

Create a POM that has the repository settings that you want and then use a parent element in your project POMs to inherit the additional repositories. The use of an "organization" POM has several other benefits when a group of projects belong to one team.

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

回答by ddimitrov

One of the goals for Maven't Project Object Model (POM) is to capture all information needed to reliably reproduce an artifact, thus passing settings impacting the artifact creation is strongly discouraged.

Maven 的项目对象模型 (POM) 的目标之一是捕获可靠地重现工件所需的所有信息,因此强烈不鼓励传递影响工件创建的设置。

To achieve your goal, you can check in your user-level settings.xml file with each project and use the -s (or --settings) option to pass it to the build.

为了实现您的目标,您可以在每个项目中签入您的用户级 settings.xml 文件,并使用 -s(或 --settings)选项将其传递给构建。

回答by Kevin Wright

I'll assume here that you're asking this because you occasionally want to add a new 3rd-party repository to your builds. I may be wrong of course... :)

我在这里假设您提出这个问题是因为您偶尔想将一个新的 3rd-party 存储库添加到您的构建中。我当然可能是错的... :)

Your best bet in this case is to use a managed proxy such as artifactory or nexus. Then make a one-time change in settings.xml to set this up as a mirror for the world.

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

Any 3rd party repos that you need to add from that point on can be handled via the proxy.

从那时起您需要添加的任何 3rd 方存储库都可以通过代理处理。