tags:

views:

1435

answers:

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.

+3  A: 

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&lt;/url&gt;
  <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&lt;/url&gt;
        <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&lt;/url&gt;
        <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, for example:

<server>
  <id>myrepo</id>
  <configuration>
    <timeout>5000</timeout> <!-- 5 seconds -->
  </configuration>
</server>
Rich Seller
I was not aware of these update policies, but I just upgraded some dependencies and so it needs to go out and download them. Agreed on the repository manager, but as I am consulting for this company I can only do the work they ask of me. Maybe I can get to use a repository manager in the future, though that will be one more thing they have to maintain.
rado
Fair enough, I've updated my answer with a workaround that may help in your current situation.
Rich Seller
To work it around it for the moment what I did was to delete the <repository> from the pom.xml, run mvn and let it download from the repo1, then put my custom repository back.
rado
Also, it looks like the timeout answer is here http://brettporter.wordpress.com/2009/06/16/configuring-maven-http-connections/ but I can't get it to work.
rado
From that link, timeout is only supported in Maven 2.1+, what version are you on?
Rich Seller
I was on 2.0.10, but I upgraded to 2.1.0 and then to 2.2 to get it to work, but haven't been able to get it work yet.
rado
Something like Nexus or Artifactory can also be an internal repository, and is really not harder to maintain (in my experience). It wouldn't be "one more thing", it would just be a different thing.
Zac Thompson
@radorant added the Maven 2.1 timeout to my install (and the answer) and it worked for me, did you set the timeout in seconds or milliseconds (it expects milliseconds).
Rich Seller
@Zac I already referenced Nexus, in my answer, did you have anything particular to add?
Rich Seller
+1  A: 

This did not work for me using Maven 2.2.1:

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

Also, there really should be a command-line option to change the global default, particularly since the built-in default appears to be something ridiculous like 15 minutes. I generally do not want to wait more than 30 seconds to connect to a repo URL, and changing it for all my repos is a pain.

Ian Springer