134

Does maven require a connection to the internet at some point to be able to use it? Meaning specifically getting the internal maven plugins for compiling, cleaning, packaging, etc?

2
  • Do you have any machine with internet access? Maybe a server?
    – Marcelo
    Commented Aug 29, 2011 at 17:14
  • 2
    We do, but to get things from there to the development environment is well, tedious.
    – user130532
    Commented Aug 29, 2011 at 17:18

15 Answers 15

189

You can run Maven in "offline" mode using the -o or --offline option (e.g. mvn -o install). Of course any artifacts not available in your local repository will fail. Maven is not predicated on distributed repositories, but they certainly make things more seamless. It's for this reason that many shops use internal mirrors that are incrementally synced with the central repos.

In addition, the mvn dependency:go-offline can be used to ensure you have all of your dependencies installed locally before you begin to work offline.

6
  • Inconjuntion with this answer, I discovered that mvn comes with its "core" plugins (i.e. compiler). So this with mvn install I can get what I need.
    – user130532
    Commented Aug 29, 2011 at 20:48
  • 9
    Also consider you can use "mvn dependency:go-offline" to ensure all the dependencies referred to in your pom.xml are present in your local repository. Commented May 3, 2014 at 16:01
  • 27
    Unfortunately mvn dependency:go-offline simply does not catch everything. Inside a fresh virtual machine, for example, my project still must download things even after running this goal. See also this issue.
    – phs
    Commented Aug 12, 2014 at 21:01
  • 4
    My experience shows that the -o option doesn't work properly and that the go-offline goal is far from sufficient to allow a full offline build: See the solution I found here: stackoverflow.com/q/43661755/1767316 Commented Apr 28, 2017 at 7:19
  • If you add the flag -llr additionally to -o it should work.
    – Udo
    Commented Mar 15, 2018 at 12:46
11

If you have a PC with internet access in your LAN, you should install a local Maven repository.

I recommend Artifactory Open Source. This is what we use in our organization, it is really easy to setup.

Artifactory acts as a proxy between your build tool (Maven, Ant, Ivy, Gradle etc.) and the outside world.

It caches remote artifacts so that you don’t have to download them over and over again.

It blocks unwanted (and sometimes security-sensitive) external requests for internal artifacts and controls how and where artifacts are deployed, and by whom.

After setting up Artifactory you just need to change Maven's settings.xml in the development machines:

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <mirrors>
    <mirror>
      <mirrorOf>*</mirrorOf>
      <name>repo</name>
      <url>http://maven.yourorganization.com:8081/artifactory/repo</url>
      <id>repo</id>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>libs-release</name>
          <url>http://maven.yourorganization.com:8081/artifactory/libs-release</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>libs-snapshot</name>
          <url>http://maven.yourorganization.com:8081/artifactory/libs-snapshot</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>plugins-release</name>
          <url>http://maven.yourorganization.com:8081/artifactory/plugins-release</url>
        </pluginRepository>
        <pluginRepository>
          <snapshots />
          <id>snapshots</id>
          <name>plugins-snapshot</name>
          <url>http://maven.yourorganization.com:8081/artifactory/plugins-snapshot</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

We used this solution because we had problems with internet access in our development machines and some artifacts downloaded corrupted files or didn't download at all. We haven't had problems since.

2
  • There is not a machine with access that is connected to the LAN.
    – user130532
    Commented Aug 29, 2011 at 17:46
  • 2
    With artifactory you can backup your whole repository and then restore it in one of the PCs that is in the LAN.
    – Marcelo
    Commented Aug 29, 2011 at 18:11
8

You have two options for this:

1.) make changes in the settings.xml add this in first tag

<localRepository>C:/Users/admin/.m2/repository</localRepository>

2.) use the -o tag for offline command.

mvn -o clean install -DskipTests=true
mvn -o jetty:run
6

Maven needs the dependencies in your local repository. The easiest way to get them is with internet access (or harder using other solutions provided here).

So assumed that you can get temporarily internet access you can prepare to go offline using the maven-dependency-plugin with its dependency:go-offline goal. This will download all your project dependencies to your local repository (of course changes in the dependencies / plugins will require new internet / central repository access).

5

Sadly dependency:go-offline hasn't worked for me as it didn't cached everything, ie. POMs files and other implicitly mention dependencies.

The workaround has been to specify a local repository location, either within settings.xml file with <localRepository>...</localRepository> or by running mvn with -Dmaven.repo.local=... parameter. After initial project build, all necessary artifacts should be cached, and then you can reference repository location the same ways, while running Maven build in offline mode (mvn -o ...).

1
  • 2
    the point is to get it done without actually building project, so we can phase it
    – JJ Roman
    Commented Mar 2, 2021 at 21:40
5

If you're using IntelliJ, you can simply go to Preferences -> Build, Execution, Deployment -> Build Tools -> Maven and check/uncheck Work offline.

1
  • 3
    Similarly - Eclipse is Window -> Preferences -> Maven and check "Offline" Commented Sep 12, 2018 at 17:07
4

Before going offline you have to make sure that everything is in your local repo, which is required while working offline. Running "mvn dependency:go-offline" for the project(s)/pom(s), you intend to work on, will reduce the efforts to achieve this.

But it´s usually not the whole story, because dependency:go-offline will only download the "bare build" plugins (go-offline / resolve-plugins does not resolve all plugin dependencies). So you have to find a way to download deploy / test / site plugins (and maybe others) and their dependencies into your repo.

Furthermore dependency:go-offline does not download the pom´s artifact itself, so you have to dependency:copy it if required.

Sometimes - as MaDa wrote - you do not know, what you will need, while being offline, which makes it pretty impossible to have a "sufficient" repo.

Anyway having a properly filled repo you only have to add "<offline>true</offline>" to Maven´s settings.xml to go offline.

Do not change the Maven profile (id) you used to fill your repo, while being offline. Maven recognizes the downloaded artifacts in its metadata with an "identity", which is bound to the profile id.

2

Does this work for you?

http://jojovedder.blogspot.com/2009/04/running-maven-offline-using-local.html

Don't forget to add it to your plugin repository and point the url to wherever your repository is.

<repositories>
    <repository>
        <id>local</id>
        <url>file://D:\mavenrepo</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>local</id>
        <url>file://D:\mavenrepo</url>
    </pluginRepository>
</pluginRepositories>

If not, you may need to run a local server, e.g. apache, on your machines.

2
  • That seems to be more for dependencies and plugins like the jetty, tomcat, etc plugins. I am more concerned about the internal maven plugins like maven-compiler-plugin
    – user130532
    Commented Aug 29, 2011 at 17:49
  • 1
    @predhme: This approach will work for all plugins, since all of these go into the maven repository on your local machine
    – JoseK
    Commented Oct 10, 2011 at 11:26
2

My experience shows that the -o option doesn't work properly and that the go-offline goal is far from sufficient to allow a full offline build:

The solution I could validate includes the use of the --legacy-local-repository maven option rather than the -o (offline) one and the use of the local repository in place of the distribution repository

In addition, I had to copy every maven-metadata-maven2_central.xml files of the local-repo into the maven-metadata.xml form expected by maven.

See the solution I found here.

1
  • It worked for me. Thank you so much! Commented Sep 27, 2023 at 14:55
2


(source: jfrog.com)

or

Just use Maven repository servers like Sonatype Nexus http://www.sonatype.org/nexus/ or JFrog Artifactory https://www.jfrog.com/artifactory/.

After one developer builds a project, build by next developers or Jenkins CI will not require Internet access.

Maven repository server also can have proxies configured to access Maven Central (or more needed public repositories), and they can have cynch'ed list of artifacts in remote repositories.

2

A new plugin has appeared to fix shortcomings of mvn dependency:go-offline:

https://github.com/qaware/go-offline-maven-plugin

Add it in your pom, then run mvn -T1C de.qaware.maven:go-offline-maven-plugin:resolve-dependencies. Once you've setup all dynamic dependencies, maven won't try to download anything again (until you update versions).

0

Answering your question directly: it does not require an internet connection, but access to a repository, on LAN or local disk (use hints from other people who posted here).

If your project is not in a mature phase, that means when POMs are changed quite often, offline mode will be very impractical, as you'll have to update your repository quite often, too. Unless you can get a copy of a repository that has everything you need, but how would you know? Usually you start a repository from scratch and it gets cloned gradually during development (on a computer connected to another repository). A copy of the repo1.maven.org public repository weighs hundreds of gigabytes, so I wouldn't recommend brute force, either.

2
  • It is, but not as a maven project (ant). So I have all the libs I need but was wondering specifically about maven internal libs. Such as maven-plugin-compiler.
    – user130532
    Commented Aug 29, 2011 at 20:49
  • If you have all the dependencies handled by Ant, you probably lack Maven metadata anyway, so Maven won't pick them up. As for "Maven internal libs", the only way to download them is to build your pom structure and run it on an internet-connected computer. By "run it" I mean executing every target that you think you'll be using, not necessarily only "mvn clean install".
    – MaDa
    Commented Aug 30, 2011 at 7:15
0

Here's a clear, straightforward way to cache Maven dependencies for offline development (based on @luka5z and others' comments):

  1. While you have internet access, cache dependencies locally:
mvn -Dmaven.repo.local=dependencies install
  1. Disconnect from the internet, verify that offline mode compilation succeeds:
mvn clean
mvn -o -Dmaven.repo.local=dependencies package

Continue developing offline as long as needed.

-1

In preparation before working offline just run mvn dependency:go-offline

-2
<offline> false </offline>

<localRepository>${user.home}/.m2/repository</localRepository>

to

<offline> true <offline>

<localRepository>${user.home}/.m2/repository</localRepository>

Change the offline tag from false to true .

will download from repo online

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.