1. Overview
Sometimes we may need to ask Maven explicitly to not download anything from its repositories for a variety of reasons.
In this short tutorial, we’re going to see how to enable the offline mode in Maven.
2. Preparing
Before going for the offline mode, it’s essential to download the necessary artifacts. Otherwise, we may fail to use this mode effectively.
In order to prepare for offline mode, we can use the go-offline goal from the maven-dependency-plugin:
mvn dependency:go-offline
This goal resolves all project dependencies — including plugins and reports and their dependencies. After running this goal, we can safely work in offline mode.
3. Offline Mode
To execute Maven goals and phases in offline mode, we just have to use the -o or –offline option. For instance, in order to run integration tests in offline mode:
mvn -o verify
This command will successfully execute all tests if we already downloaded all the required artifacts. Otherwise, it will fail.
It’s also possible to configure the offline mode globally by setting the offline property in the ~/.m2/settings.xml file:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<offline>true</offline>
</settings>
This setting will be applied to all Maven projects. The offline property is by default set to false. So, when we’re using the -o option, it will override that default setting temporarily for the duration of that command.
4. Conclusion
In this quick tutorial, we saw how to prepare for Maven offline mode using maven-dependency-plugin. Also, we got familiar with both a command-line approach and a settings-based approach to enable the offline mode.