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

时间:2022-09-13 23:56:24

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有点麻烦。

7 个解决方案

#1


59  

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"

#2


5  

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't Project Object Model(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)选项将其传递给构建。

#3


3  

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

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

#4


3  

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

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

mvn deploy -P MyRepo2

mvn deploy -P MyRepo1

#5


2  

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... :)

我在这里假设你问这个是因为你偶尔想要为你的构建添加一个新的第三方存储库。我当然可能错了...... :)

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.

您需要从该点添加的任何第三方存储库都可以通过代理处理。

#6


1  

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 / settings中构建一个。请参阅此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.

我不确定这是否有用,但经过一些调整我相信你能想出一些东西。

#7


1  

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还有其他一些好处。

#1


59  

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"

#2


5  

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't Project Object Model(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)选项将其传递给构建。

#3


3  

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

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

#4


3  

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

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

mvn deploy -P MyRepo2

mvn deploy -P MyRepo1

#5


2  

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... :)

我在这里假设你问这个是因为你偶尔想要为你的构建添加一个新的第三方存储库。我当然可能错了...... :)

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.

您需要从该点添加的任何第三方存储库都可以通过代理处理。

#6


1  

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 / settings中构建一个。请参阅此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.

我不确定这是否有用,但经过一些调整我相信你能想出一些东西。

#7


1  

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还有其他一些好处。