Working in offline mode

There might be situations where a Maven project needs to be built without access to the Internet. Let us see how Maven supports this, as well as the caveats.

How to do it...

  1. Open a project that you want to build offline.
  2. Run the following command:
    mvn dependency:go-offline 
    
  3. Observe the output:
    How to do it...
  4. Run the following command:
    mvn –o clean package
    

Observe that the build is completed successfully without any network connection.

How it works...

The go-offline goal of the Maven Dependency plugin downloads all the required dependencies and plugins for the project, based on the pom file. The –o option tells Maven to work offline and not check the Internet for anything.

However, it is not without its issues. On a brand new local repository, the offline option will not work with the following error:

How it works...

This is a known problem or limitation with the Maven Dependency plugin. The required project has to be built online once to download anything that is missed out by the plugin. Subsequently, the project can be built offline. This is typically not required, as most organizations use a repository manager, such as Nexus or Artifactory, if they do not allow direct Internet access.

Tip

If your project uses SNAPSHOT dependencies, then Maven will need the Internet to look for updates to the dependencies. To avoid this, you can set the updatePolicy to never, but this would be counterproductive as you will not get the latest version of the dependency.

There's more...

Another way to run Maven in offline mode is to specify the offline parameter as true in the settings file. Once this is done, no attempt is made by Maven to connect to the Internet.

  1. Add the following in the settings file:
    <offline>true</offline>
  2. Run a Maven build:
    mvn clean package
    

Observe that the build is completed successfully without connecting to the Internet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.213.44