Java 下载依赖项时如何让maven提前超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1168468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get maven to timeout earlier while downloading dependencies?
提问by rado
I am building my project with Apache Maven and have a custom repository configured but when it hits the repository it just hangs for a very long time with
我正在使用 Apache Maven 构建我的项目并配置了一个自定义存储库,但是当它到达存储库时,它会挂起很长时间
Downloading: http://maven.mycompany.com/m2/org/springframework/spring/2.5.6/spring-2.5.6.pom
下载:http: //maven.mycompany.com/m2/org/springframework/spring/2.5.6/spring-2.5.6.pom
after a few minutes it goes and downloads it from the central repo
几分钟后,它会从中央仓库下载
Downloading: http://repo1.maven.org/maven2/org/springframework/spring/2.5.6/spring-2.5.6.pom12K downloaded (spring-2.5.6.pom)
下载:http: //repo1.maven.org/maven2/org/springframework/spring/2.5.6/spring-2.5.6.pom 12K下载(spring-2.5.6.pom)
I want the timeout to be much quicker than that. This happens with all the newer versions of maven. Version 2.0.6 or earlier didn't have this problem, it would timeout much quicker.
我希望超时比这快得多。所有较新版本的 maven 都会发生这种情况。2.0.6 或更早的版本没有这个问题,它会更快地超时。
采纳答案by Rich Seller
In versions of Maven before 2.1, there is no means to configure the client to timeout, but you can configure it to check for updates less often if you set the update policy. This partially addresses the problem.
在 Maven 2.1 之前的版本中,没有办法将客户端配置为超时,但是如果您设置更新策略,您可以将其配置为较少检查更新。这部分解决了这个问题。
For example:
例如:
<repository>
<id>myrepo</id>
<url>http://maven.mycompany.com/m2</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
Valid values are:
有效值为:
- always - always check when Maven is started for newer versions of snapshots
- never - never check for newer remote versions. Once off manual updates can be performed.
- daily (default) - check on the first run of the day (local time)
- interval:XXX - check every XXX minutes
- 总是 - 总是检查 Maven 何时启动以获得更新版本的快照
- 从不 - 从不检查较新的远程版本。一旦关闭,就可以执行手动更新。
- 每天(默认) - 检查当天的第一次运行(当地时间)
- 间隔:XXX - 每 XXX 分钟检查一次
Another consideration is the software you are using to host your internal repository. With a repository manager such as Nexusyou can manage all your external remote repository connections through the manager and configure the timeout for those remote connections. Your client will then only query the repository manager, which should respond as quickly as the timeouts allow.
另一个考虑因素是您用来托管内部存储库的软件。使用Nexus等存储库管理器,您可以通过管理器管理所有外部远程存储库连接,并为这些远程连接配置超时。然后您的客户端将只查询存储库管理器,它应该在超时允许的情况下尽快响应。
Update:
更新:
If you know the dependencies aren't going to be served by a particular repository, you can separate it into a profile, so it is not referenced in that build.
如果您知道依赖项不会由特定存储库提供服务,您可以将其分成一个配置文件,这样它就不会在该构建中被引用。
<profiles>
<profile>
<id>remote</id>
<repositories>
<repository>
<id>central</id>
<url>http://repo1.maven.org</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
...
</repositories>
</profile>
<profile>
<id>internal</id>
<repositories>
<repository>
<id>myrepo</id>
<url>http://maven.mycompany.com/m2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
...
</repositories>
</profile>
</profiles>
With the above config, running mvn package -Premotewill not connect to the internal repository, so the timeout won't be a factor.
使用上述配置,运行mvn package -Premote将不会连接到内部存储库,因此超时不会成为一个因素。
You can avoid having to specify the profiles on each build by adding some extra config to your settings:
您可以通过在设置中添加一些额外的配置来避免在每个构建中指定配置文件:
<settings>
...
<activeProfiles>
<activeProfile>internal</activeProfile>
<activeProfile>remote</activeProfile>
</activeProfiles>
...
</settings>
For Maven 2.1 you can set the timeout by adding a configuration on a server in the Maven settings (~/.m2/settings.xml
by default), for example:
对于 Maven 2.1,您可以通过在 Maven 设置(~/.m2/settings.xml
默认情况下)中添加服务器上的配置来设置超时,例如:
<server>
<id>myrepo</id>
<configuration>
<timeout>5000</timeout> <!-- 5 seconds -->
</configuration>
</server>
回答by An???drew
One quick and dirty hack is adding a custom hosts file entry to redirect network requests to the invalid repository to a valid one.
一个快速而肮脏的黑客是添加一个自定义主机文件条目,以将网络请求重定向到无效存储库到有效存储库。