29

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

下载:http ://maven.mycompany.com/m2/org/springframework/spring/2.5.6/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)

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

4

2 回答 2

21

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

例如:

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

有效值为:

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

另一个考虑因素是您用于托管内部存储库的软件。使用Nexus等存储库管理器,您可以通过管理器管理所有外部远程存储库连接,并为这些远程连接配置超时。然后,您的客户端将只查询存储库管理器,它应该在超时允许的情况下尽快响应。


更新:

如果您知道特定存储库不会提供依赖项,则可以将其分离到配置文件中,因此在该构建中不会引用它。

<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 package -Premote将不会连接到内部存储库,因此超时不会是一个因素。

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

<settings>
  ...
  <activeProfiles>
    <activeProfile>internal</activeProfile>
    <activeProfile>remote</activeProfile>
  </activeProfiles>
  ...
</settings>

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

<server>
  <id>myrepo</id>
  <configuration>
    <timeout>5000</timeout> <!-- 5 seconds -->
  </configuration>
</server>
于 2009-07-22T22:04:13.843 回答
0

一种快速而肮脏的技巧是添加自定义主机文件条目,以将网络请求重定向到无效存储库到有效存储库。

于 2013-03-14T17:44:14.530 回答