且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在下载依赖项时使Maven提前超时?

更新时间:2022-12-12 16:21:42

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