Mastering The Maven Command Line – Managing Dependencies

This post is the first post of a Maven-related series. It focuses on the Maven command line that can do a lot more than just mvn clean install. This first post explores the options to configure the dependency resolution like using another local repository, forcing updates, and working offline.

Offline Mode

Maven often takes a lot of time to check the potential updates of dependencies and plugins. But sometimes, you want to skip this process, either because you don’t have an Internet connection or because you know that you can safely ignore them.

Before being able to run maven in offline mode, be sure you have compiled everything at least one time online. Maven retrieves all the dependencies in your local repository. Then, the offline mode just reuses the latest retrieved artifacts.

Enabling the offline mode is quite simple:

mvn –-offline clean install


mvn –o clean install

So, to work offline:

  • run the build in online mode
  • use the --offline option

Using different settings

If you are working on different projects or for different customers, you may have several settings files. By default, maven uses a settings file located in ~/.m2/settings.xml. You can override this location in the command line with the --settings option:

mvn --settings ~/.m2/settings-customer1.xml clean install


mvn –s ~/.m2/settings-customer1.xml clean install

This second settings file can define different repositories, credentials, proxies, profiles…

Less common but still, you can override the global settings with the --global-settings parameter (or simply -gs).

Forcing updates

Working offline is sometimes interesting, but forcing updates can be useful to retrieve the latest version of a snapshot artifacts developed by one of your teammates. The --update-snapshots options (or -U) forces maven to check all snapshot dependencies and retrieve latest versions:

mvn --update-snapshots clean install


mvn -U clean install

Thanks to this option, you’re sure to build the project against the latest available version of all your snapshot dependencies. If not used, the dependencies are checked once a day (default behavior).

Building from scratch: specifying a local repository

The last option of this blog post is not a real option but a pretty useful system variable. Before committing your changes, you want to be sure that you didn’t introduce any unavailable dependency. However, such dependencies may be in your local repository, and so you want to be sure they can still be retrieved and used.

Deleting your local maven repository completely may do the job, but … it’s not necessarily what you want.

The maven.repo.local variable allows configuring the local repository location of Maven:

mvn clean install –Dmaven.repo.local=~/tmp/tmp-repo

This creates a complete new local repository at the specified location. In other words, you will compile your projects from scratch checking that you’re not using non-available artifacts from your (main) local repository. For sure it takes more time, but that’s the price to pay. Coupled with the --settings option (pointing to a generic settings file) you can be sure that the projects you are working on are building without any specific configuration.

3 thoughts on “Mastering The Maven Command Line – Managing Dependencies

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s