Java – How to get maven to timeout earlier while downloading dependencies

javamaven-2

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

Downloading: 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.pom
12K downloaded (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.

Best Answer

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.

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

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.


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

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>