使 Maven 代理/服务器设置可根据位置进行配置?
所以我不确定实现这一点的最佳方法是什么,但基本上我有一台笔记本电脑,用于 Maven 项目的工作。当我在工作时,它工作得很好,但是当我走出他们的公司代理和 Maven 服务器的大门时,我经常不得不对 settings.xml
进行大量手动修改当我在家时如果我没有通过 VPN 连接的话,我可以使用以下文件:
- 我们有一个公司安装的 Maven 存储库代理服务器来存储我们自己的一些工件并处理成为我们常用工件的中间人。
- 我们有一个 http 代理,用于连接到外部世界。
这两种配置均由我的 settings.xml
文件处理,用于设置 单个 Nexus 组 和 maven 代理。如果我不在办公室时未连接到 VPN,则每次不在办公室时我都必须修改 settings.xml,然后在使用时将其切换回来。
其他人找到了什么解决方案来处理这个问题?我一直在尝试使用配置文件来管理代理,但我似乎无法让它正常工作,而且它开始看起来相当难看。当我在工作中不在代理后面并且不使用公司代理服务器或 Maven 服务器时,是否有一些设置配置可以检测?
So I'm not sure what the best way to accomplish this is, but basically I have a laptop that I use at work for Maven projects. It works fine when I'm at work, but as soon as I walk out of the door of their corporate proxy and maven server, I often have to do alot of hand-fudging of the settings.xml
file when I'm at home if I'm not VPN'ed in:
- We have a corporate-installed Maven Repository proxy server to store some of our own artifacts and handle being the middle-man for our commonly used artifacts.
- We have an http proxy that we use for connecting to the outside world.
Both configurations have been handled by my settings.xml
file for setting a single Nexus group and maven proxies. If I'm not connected to the VPN while away from the office, I have to muck around with the settings.xml each time I'm not on it, then switch it back when I am on it.
What solutions have anyone else found to handle this? I've been trying profiles to manage the proxy, but I can't seem to get it to work correctly, and it's starting to look pretty ugly. Are there some settings configurations that can detect when I'm not behind the proxy at work and not use the corporate proxy server or Maven server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然我可以想到一些基于配置文件的解决方案来处理代理(基本上,从配置文件中定义的属性读取
值),但这不会完全自动化(配置文件激活不支持基于网络的东西)除非你能找到文件是否存在取决于您的位置(在这种情况下,您可以使用现有/丢失的文件触发器,但这有点hacky)。无论如何,这只能解决问题的一部分,因为无法在配置文件中声明镜像(请参阅因此,我的建议是使用两个
settings.xml
并使用settings-home.xml
文件传递您的settings-home.xml
文件,而不是尝试使用配置文件来控制它>-s 当您在家时的命令行选项。另一种选择是使用脚本自动更改
settings.xml
(正如有人在 MNG-3525)。While I can think of some profile based solution to handle the proxy (basically, reading the
<active>
value from a property defined in a profile), this wouldn't be fully automated (the profile activation do not support network based stuff) unless you can find a file that is present or not depending on your location (in which case, you could use an existing/missing file trigger but this is kinda hacky). Anyway, this would solve only one part of the problem because mirrors can't be declared in profiles (see MNG-3525).So, instead of trying to control this with a profile, my suggestion would be to use two
settings.xml
and to pass yoursettings-home.xml
file with the-s
command line option when you're at home.Another option would be to automate the changes in your
settings.xml
with a script (Groovy would be a good choice as someone reported in MNG-3525).我发现使用环境变量将
nonProxyHosts
与proxy
和noproxy
shell 别名一起设置是在具有代理和代理的网络之间切换时最方便的解决方案没有它。配置代理
在
settings.xml
中,使用then in~/.profile
set要在漫游时进行切换,您只需从 shell 执行:
或者
显然,别名
proxy
和noproxy
可以包含比仅设置MAVEN_NOPROXY 更多的更改
和all_proxy
。I found a use environment variables to set
nonProxyHosts
together withproxy
andnoproxy
shell aliases to be the most convenient solution when switching between networks with proxy and without it.In
settings.xml
, configure proxy withThen in
~/.profile
setTo do the switch when roaming, you would just execute from a shell:
or
Obviously, both aliases
proxy
andnoproxy
can include much more changes than just setup ofMAVEN_NOPROXY
andall_proxy
.我对同样的问题感到沮丧:在网络之间漫游时必须手动编辑 settings.xml。事实上,我编写了一个 Maven 插件,可以自动发现代理设置。当前的实现使用 Bernd Rosstauscher 编写的 proxy-vole 库来根据操作系统配置、浏览器和环境设置来检测代理设置。
我刚刚在 Github 上发布了该插件的源代码,采用 Apache 2.0 许可证: https:/ /github.com/volkertb/autoproxy-maven-plugin
欢迎您尝试一下,看看它是否满足您的需求。欢迎任何反馈或贡献!
(注意:您不必将该插件添加到项目的 POM 中。安装后也可以从命令行调用它。有关更多详细信息,请参阅网站上的自述文件。)
I was frustrated by the same problem: having to manually edit settings.xml when roaming between networks. So much in fact, that I wrote a Maven plugin that enables automatic discovery of proxy settings. The current implementation uses the proxy-vole library written by Bernd Rosstauscher to detect proxy settings based on OS configuration, browser, and environment settings.
I've just released the source code of the plugin on Github, under an Apache 2.0 license: https://github.com/volkertb/autoproxy-maven-plugin
You're welcome to give it a try and to see if it meets your needs. Any feedback or contributions are welcome!
(Note: you don't necessarily have to add the plugin to your project's POM. You can invoke it from the command line as well, after you've installed it. See the README on the site for more details.)
当您需要激活代理时,可以设置
MAVEN_OPTS
:You can set
MAVEN_OPTS
when you need to activate a proxy: