【问题标题】:Maven settings.xml related issueMaven settings.xml 相关问题
【发布时间】:2016-01-08 05:22:07
【问题描述】:

我遇到了settings.xml 的一些问题。实际上问题出在工作场所,我需要指向我们 VPN 上的不同存储库,而在尝试新示例的情况下,我想忽略在 VPN 上配置存储库服务器的settings.xml。有什么办法可以做到吗?目前我正在做的只是简单地将settings.xml 的名称更改为settings.xml1 并且它可以工作。但我厌倦了做这样的改变。

我的同事正在做的一件事是将 settings.xml 保存在项目本身中。我不喜欢它,因为它是 maven 存储库配置,不应该保留在项目本身中。对于同样的事情,我也从快速入门指南中获得了类似的参考。来自official documentation on settings

settings.xml 文件中的 settings 元素包含用于定义以各种方式配置 Maven 执行的值的元素,例如 pom.xml,但不应捆绑到任何特定项目或分发给受众。

如果您对此有任何解决方案,请随时回答。

【问题讨论】:

  • 是否可以将您的存储库服务器设置为公共存储库的代理?这样您就不需要 2 个 settings.xml 文件(Maven 的默认文件和工作场所特定的文件)。
  • @Ralf 不,这是不可能的 :(

标签: java maven maven-plugin


【解决方案1】:

如果存储库是在您的settings.xml 文件中定义的,则它们会在配置文件中定义,我猜默认情况下它是活动的,应用official documentation 建议的标准方法。假设此配置文件名为 vpn-rep(通过其 id 元素)。

然后您可以添加第二个配置文件,该配置文件声明您想要的任何存储库并且默认情况下不处于活动状态。所以,没有影响。你可以叫它my-rep

然后,由于它们是配置文件,您可以通过命令行轻松地switch 从一个到另一个,如下所示:

  • 在 VPN 存储库上工作,您的 maven 命令没有更改
  • 在没有 VPN 存储库的情况下工作,运行 mvn clean install -P!vpn-rep,my-rep

-P!vpn-rep,my-rep 部分实际上将关闭 vpn-rep 配置文件(因此 maven 不会使用在其上声明的存储库,即不会使用任何 VPN 存储库)并将打开 my-rep 一个.

如果您实际上没有配置任何额外或特殊的存储库,并且您只是不想使用 VPN,您可以跳过创建第二个配置文件并仅关闭第一个配置文件 (-P!vpn-rep),Maven然后将使用默认存储库。

这样的settings.xml 文件的示例如下:

<settings>
    <profiles>
        <profile>
            <id>vpn-rep</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://your-vpn-company-repository/libs-releases</url>
                </repository>
            </repositories>

            <pluginRepositories>
                <pluginRepository>
                   ...
                </pluginRepository>
            </pluginRepositories>
        </profile>
        <profile>
            <id>my-rep</id>
            <repositories>
                <!-- any specific repository configuration here -->
            </repositories>
            <pluginRepositories>
                ...
            </pluginRepositories>           
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>vpn-rep</activeProfile>
    </activeProfiles>
</settings>

【讨论】:

  • 感谢您提供详细的答案和其中的链接.. :)
【解决方案2】:

我假设您有一个私有(内部工件)和公共存储库。如果是这种情况,只需将两者都添加到您的设置文件中,它将按照给定的顺序在每个存储库中查找

https://maven.apache.org/guides/mini/guide-multiple-repositories.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2012-09-06
    • 2012-04-19
    • 1970-01-01
    相关资源
    最近更新 更多