Is it possible to use proxy only when a specific profile is active in Maven?

∥☆過路亽.° 提交于 2021-02-06 15:15:49

问题


I would like to use proxy only when a specific profile is active. To accomplish this, my guess is to parameterize the <active> property of <proxy> element. However, I am not exactly sure how to accomplish this.

Question: How can I use proxy only when a specific profile is active?


回答1:


You could try the following approach:

<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>

So by default the proxy-off profile would be active, which would set activate.proxy to false and as such active of proxy to false.

Then executing with:

mvn clean install -Pproxy-on

Would activate the proxy-on profile and the whole chain should result to true for active.




回答2:


This does not answer the original question, which asks about control-by-profile, but one workaround is to ignore settings.xml proxies and set MAVEN_OPTS when you need to activate a proxy:

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"


来源:https://stackoverflow.com/questions/39348487/is-it-possible-to-use-proxy-only-when-a-specific-profile-is-active-in-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!