如何在下载依赖项时使Maven提前超时? [英] How to get maven to timeout earlier while downloading dependencies?

查看:104
本文介绍了如何在下载依赖项时使Maven提前超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Maven构建我的项目,并配置了一个自定义存储库,但是当它访问该存储库时,它只会在很长一段时间内挂起

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

下载: 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

下载: http://repo1.maven.org/maven2/org/springframework/spring/2.5.6/spring-2.5.6.pom 下载了12K(spring-2.5.6.pom)

Downloading: http://repo1.maven.org/maven2/org/springframework/spring/2.5.6/spring-2.5.6.pom 12K downloaded (spring-2.5.6.pom)

我希望超时比那快得多.所有较新版本的Maven都会发生这种情况. 2.0.6或更早版本没有此问题,它将更快地超时.

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.

推荐答案

在2.1之前的Maven版本中,没有办法将客户端配置为超时,但是如果您设置了超时,则可以配置它以较少的频率检查更新.更新政策.这部分解决了这个问题.

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.

例如:

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

有效值为:

  • 始终-始终检查何时启动Maven以获取较新版本的快照
  • 从不-从不检查较新的远程版本.一旦关闭,便可以执行手动更新.
  • 每天(默认)-检查一天的第一轮(当地时间)
  • 时间间隔:XXX-每XXX分钟检查一次

另一个要考虑的因素是用于托管内部存储库的软件.使用存储库管理器,例如 Nexus ,您可以通过管理器管理所有外部远程存储库连接并配置超时对于那些远程连接.然后,您的客户将仅查询存储库管理器,该存储库管理器应尽快响应 在超时允许的范围内.

Another consideration is the software you are using to host your internal repository. With a repository manager such as Nexus you 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.

更新:

如果您知道某个特定存储库将不再提供依赖项,则可以将其分为一个配置文件,因此在该版本中不会对其进行引用.

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>

使用上述配置,运行 mvn软件包-Premote 不会连接到内部存储库,因此超时不会成为问题.

With the above config, running mvn package -Premote will not connect to the internal repository, so the timeout won't be a factor.

您可以通过在设置中添加一些额外的配置来避免在每次构建时都指定配置文件:

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>

对于Maven 2.1,您可以通过在Maven设置(默认为~/.m2/settings.xml)中在服务器上添加配置来设置超时,例如:

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:

<server>
  <id>myrepo</id>
  <configuration>
    <timeout>5000</timeout> <!-- 5 seconds -->
  </configuration>
</server>

这篇关于如何在下载依赖项时使Maven提前超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆