30

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.

1

2 Answers 2

21

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>
9
  • 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
    Jul 22, 2009 at 22:08
  • Fair enough, I've updated my answer with a workaround that may help in your current situation. Jul 22, 2009 at 22:19
  • 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
    Jul 22, 2009 at 22:27
  • Also, it looks like the timeout answer is here brettporter.wordpress.com/2009/06/16/… but I can't get it to work.
    – rado
    Jul 22, 2009 at 22:28
  • From that link, timeout is only supported in Maven 2.1+, what version are you on? Jul 22, 2009 at 22:35
0

One quick and dirty hack is adding a custom hosts file entry to redirect network requests to the invalid repository to a valid one.

1
  • Sounds like a good idea, how would that look like? I assume you mean /etc/hosts on Linux. Aug 23, 2017 at 18:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.