Apache Maven: offline execution

In this article, we will learn and find ways to run/execute maven in offline mode

 

With Maven, everything from compiling to build to deploy becomes easy for every application with the configuration of pom.xml for respective projects i.e.;

 

But downloading artifacts from Internet makes compiling & packaging slower to complete all above tasks for a very large enterprise applications

So sometimes it is very important to run/execute maven goal in offline mode

 

There are 3 ways to run/execute maven in offline mode

  1. Run maven build in offline mode using “mvn –o install”
  2. Point to local m2_repo repository in xml
  3. Turn-on offline mode from maven’s xml using the element <offline />

Note: To run/execute maven in offline mode, it is very necessary to have every required artifacts inside local m2_repo repository

 

Command approach:

Way 1: Run maven build in offline mode using “mvn –o install”

By appending “-o” to maven goals dictates that it should run/execute in offline mode. But before that, developer should make sure every required artifacts already downloaded/stored

1st execute, “mvn dependency:go-offline” goal –> which checks every possible dependencies required for project before going offline

mvn dependency:go-offline

2nd execute, your required goal in offline mode. For example,

mvn –o install 
or 
mvn --offline install

Note: 1st execute isn’t required, if developers are sure that every required artifacts present in the local repository

 

pom.xml modification approach

Way 2: Point to local m2_repo repository in pom.xml

Once developer is sure that, every required artifacts present in the local m2_repo repository then just change the pom.xml with the below chunk

<repository>
<id>central</id>
<url>file://D:\Users\.m2\repository</url>
</repository>

Generally, <repository> element inside pom.xml provides URL address from where required artifacts need to be downloaded

 

Settings.xml modification approach

Way 3: Turn-on offline mode from maven’s settings.xml using the element <offline />

Earlier approaches are at the finer level with changes impacting only project in consideration. But this approach is at the highest level as turning-on <offline /> mode to true, applies to every projects in that particular system

<offline>true</offline>

While modifying Settings.xml, developer should consider all factors as it will impact every projects on that particular system

 

References:

http://maven.apache.org/plugins/maven-dependency-plugin/go-offline-mojo.html
https://maven.apache.org/plugins/maven-dependency-plugin/
https://maven.apache.org/settings.html

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Apache Maven: Co-ordinates explained