【问题标题】:Is it possible to use proxy only when a specific profile is active in Maven?仅当特定配置文件在 Maven 中处于活动状态时才可以使用代理?
【发布时间】:2017-01-13 21:08:12
【问题描述】:

我只想在特定配置文件处于活动状态时使用代理。为此,我的猜测是参数化<proxy> 元素的<active> 属性。但是,我不确定如何实现这一点。

问题:如何仅在特定配置文件处于活动状态时使用代理?

【问题讨论】:

  • settings.xml 文件中,代理不在配置文件元素中,因此这是不可能的(参见此处:maven.apache.org/settings.html#Proxies
  • 什么可能有帮助:您可以指定 maven 使用 mvn -s /path/to/settings.xml 使用的 settings.xml - 因此您的情况可能有两种配置?

标签: maven proxy maven-3 maven-profiles


【解决方案1】:

您可以尝试以下方法:

<settings>

    <proxies>
        <proxy>
            <id>httpproxy</id>
            <active>${activate.proxy}</active>
            <protocol>http</protocol>
            <host>some.host</host>
            <port>8080</port>
            <nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
        </proxy>
    </proxies>

    <profiles>
        <profile>
            <id>proxy-on</id>
            <properties>
                <activate.proxy>true</activate.proxy>
            </properties>
        </profile>

        <profile>
            <id>proxy-off</id>
            <properties>
                <activate.proxy>false</activate.proxy>
            </properties>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>proxy-off</activeProfile>
    </activeProfiles>
</settings>

因此,默认情况下,proxy-off 配置文件将处于活动状态,这会将 activate.proxy 设置为 false,因此将 activeproxy 设置为 false

然后执行:

mvn clean install -Pproxy-on

将激活proxy-on 配置文件,整个链应导致true 用于active

【讨论】:

  • @Utku 你解决了这个问题吗?这个答案有帮助吗?
  • 在 Maven 3.3.9 和 3.5.0 中对我有用吗?
  • @Jan 你用其他方法解决了吗?我会再次检查这种方法,感谢您的反馈
  • 我在stackoverflow.com/a/34177273 中找到了一个很好的建议,我实施了。有总比没有好,但远非完美。 (链接方法不回答 OP 的问题,因为它不是基于配置文件的。)
  • 这在(也许)Maven 的下一个大版本之前永远不会起作用。原因是插值不能在配置文件标签之外工作,因为:Note that properties defined in profiles within the settings.xml cannot be used for interpolation. 第二件事是 maven 不支持字符串以外的值的插值(在设置中)。 (例如 proxy.port 或 proxy.active。)
【解决方案2】:

这没有回答最初的问题,即按配置文件控制,但一种解决方法是在需要激活代理时忽略 settings.xml 代理并设置 MAVEN_OPTS

export MAVEN_OPTS="-Dhttp.proxyHost=my-proxy-server -Dhttp.proxyPort=80 -Dhttp.nonProxyHosts=*.my.org -Dhttps.proxyHost=my-proxy-server -Dhttps.proxyPort=80 -Dhttps.nonProxyHosts=*.my.org"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多