
在不少企业中,构建过程都发生在企业内网。在内网中通常是无法访问公网的外部依赖的。这时,我们的构建就需要设置代理了。
本文我们总结了一些常用工具的代理配置方法,希望能对大家有用。如果您觉得有用,感觉分享转发。谢谢。
假设我们已经搭建了一个HTTP代理:http://proxy.example.com:8080。用户名和密码是username和password。
Golang语言构建
设置以下环境变量
http_proxy=http://username:password@proxy.example.com:8080
https_proxy=http://username:password@proxy.example.com:8080
no_proxy="192.168.*.*,*.local,localhost,127.0.0.1,*.exludeproxy.com"
Tips:注意,有些工具是从环境变量中读取http的代理配置,至于它是读取大写的HTTP_PROXY,还是小写的http_proxy。有时,我们无法得知,你可以尝试两个都加上。或者先做下实验再设置。
Tips: 当你期望排除对 abc.excludeproxy.com 的代理时,有些工具即使设置了 no_proxy 为.excludeproxy.com,也不会生效,这时你可能需要写全域名 abc.excludeproxy.com。因为这些工具不认.excludeproxy.com
Maven构建
方法一:在settings.xml中加入:
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>proxy.example.com</host>
<username>username</username>
<password>password</password>
<port>8080</port>
<nonProxyHosts>192.168.*.*|*.local|*.exludeproxy.com</nonProxyHosts>
</proxy>
<proxy>
<active>true</active>
<protocol>https</protocol>
<username>username</username>
<password>password</password>
<host>proxy.example.com</host>
<port>8080</port>
<nonProxyHosts>192.168.*.*|*.local|*.exludeproxy.com</nonProxyHosts>
</proxy>
</proxies>
方法二:命令行指定
mvn compile package -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.example.com -Dhttps.proxyPort=8080 -DnonPorxyHosts="192.168.*.*|*.local|*.exludeproxy.com"
Gradle构建
方法一:在命令行指定
gradle clean build -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.example.com -Dhttps.proxyPort=8080 -DnonPorxyHosts="192.168.*.*|*.local|*.exludeproxy.com" -Dhttp.proxyUser=username -Dhttp.proxyPassword=password
方法二:在gradle.properties中设置
systemProp.http.proxyHost=proxy.example.com
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=username
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=192.168.*.*|*.local|*.exludeproxy.com
systemProp.https.proxyHost=proxy.example.com
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=username
systemProp.https.proxyPassword=password
NPM构建
方法一:通过npm config命令
npm config set proxy "<http://username:password@proxy.example.com:8080>"
npm config set https-proxy "<http://username:password@proxy.example.com:8080>"
npm config set noproxy "*.exludeproxy.com"
方法二:通过环境变量
export http_proxy="<http://username:password@proxy.example.com:8080>"
export https_proxy="<http://username:password@proxy.example.com:8080>"
export noproxy="192.168.*.*,*.local,localhost,127.0.0.1,*.exludeproxy.com"
方法三:通过.npmrc设置
proxy=http://username:password@proxy.example.com:8080
https-proxy=http://username:password@proxy.example.com:8080
https_proxy=http://username:password@proxy.example.com:8080
noproxy=192.168.*.*,*.local,localhost,127.0.0.1,*.exludeproxy.com
Tips:注意,NPM有些依赖读取的是NO_PROXY而不是noproxy。所以,保险起见,最好把http_proxy和https_proxy的环境变量也设置了。
Git
虽然它不是构建工具,但是有些构建工具会使用它。
方法一:通过命令行
git config --global http.proxy <http://proxy.example.com:8080>
git config --global https.proxy <http://proxy.example.com:8080>
方法二:通过~/.gitconfig
[http]
proxy = <http://username:password@proxy.example.com:8080>
[https]
proxy = <http://username:password@proxy.example.com:8080>
Docker构建
在Docker deamon进程启动时设置环境变量
NO_PROXY=192.168.*.*,*.local,localhost,127.0.0.1,*.exludeproxy.com
HTTP_PROXY=http://username:password@proxy.example.com:8080
HTTPS_PROXY=http://username:password@proxy.example.com:8080
Docker deamon设置好了代理也只是Docker deamon使用。但是Dockerfile中的CMD也会使用代理,这时,我们可以在docker build命令行中加入参数设置:
docker build -t curl --build-arg http_proxy=http://username:password@proxy.example.com:8080 --build-arg https_proxy=http://username:password@proxy.example.com:8080 --build-arg no_proxy="192.168.*.*,*.local,localhost,127.0.0.1,*.exludeproxy.com" .
Bazel构建
在环境变量中配置
http_proxy=http://username:password@proxy.example.com:8080
https_proxy=http://username:password@proxy.example.com:8080
no_proxy="192.168.*.*,*.local,localhost,127.0.0.1,*.exludeproxy.com"
小结
很多构建工具对于如何配置代理还是有共识的,即从环境变量中读取http_proxy,https_proxy,no_proxy的值。但是no_proxy中,对于*.xxx.com这样的配置规则是否支持,没有达到一致。有些工具支持,有些不支持,需要读者自己尝试。
还要注意的是对于http_proxy这些变量,到底是使用大写,还是小写,目前还没有统一的标准说法。保险起见,大小写都配置一遍。
网友评论