Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get maven to timeout earlier while downloading dependencies?

Tags:

java

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

like image 331
rado Avatar asked Jul 22 '09 21:07

rado


People also ask

Why Maven dependencies are not getting downloaded?

Maven uses HTTP to download its dependencies along with the dependencies of the Maven project (such as Camel). If you run Maven and it fails to download your required dependencies it's likely to be caused by your local firewall & HTTP proxy configurations.

Why does Maven download dependencies every time?

When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build. Maven local repository by default get created by Maven in %USER_HOME% directory.


1 Answers

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> 
like image 77
Rich Seller Avatar answered Sep 22 '22 11:09

Rich Seller