首页 文章

如何在下载依赖项时让maven更早超时?

提问于
浏览
28

我正在使用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或更早版本没有这个问题,它会更快地超时 .

2 回答

  • 0

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

    有效值为:

    始终

      • 始终检查Maven是否为较新版本的快照启动
    • 从不 - 永远不会检查更新的远程版本 . 一旦关闭,可以执行手动更新 .

    • 每日(默认) - 检查当天的第一次运行(当地时间)

    • interval: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>
    
  • 21

    一个快速而又脏的黑客是添加自定义主机文件条目以将对无效存储库的网络请求重定向到有效存储库 .

相关问题