Accessing local repository in offline mode

2019-07-19 05:26发布

问题:

I need to make Maven access local repository (file://) when it is ran in offline mode (basically, I am trying to setup repository hierarchy so it does not put artifacts where I don't need them).

This does not work out-of-the-box, though I always assumed this scenario is supported. Is there some flag to enable particular repository in offline mode?

回答1:

For the dev environment only, if you use IntelliJ IDEA, you can configure Maven to work offline as follow:

Preferences... > Search for Maven > Work offline (checked)


回答2:

It is possible to set up profiles for repositories utilizing the local file system rather than a network address.

<repository>
   <id>mymaven</id>
   <url>file://D:\mylocalrepo</url>
</repository>

According to documentation, it is also possible to reference offline mode in a property value.

${settings.offline}

You would then leverage these together to activate a given settings profile according to the examples here. (If Maven doesn't detect the property, try evaluating it directly using the above syntax.)

  <profiles>
    <profile>
      <id>myNeededProfile</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        ...
        <property>
          <name>offline</name>
          <value>true</value>
        </property>
        ...
      </activation>
      ...
    </profile>
  </profiles>

I believe that the Maven Help Plugin can guide this development by computing which profile will be active under certain conditions.

I also think that this could be accomplished more simply by explicitly invoking a profile from the command line each time offline mode is requested.

mvn groupId:artifactId:goal -o -P profile-1,profile-2

Or, even more straightforwardly, by having two separate settings files and subbing them out specifically for the offline/online operations. You could write a command-line wrapper in whatever OS environment you're using to detect the offline request, then move and rename the files before executing the Maven commands, then move them back upon completion.



回答3:

Maven always tried to connect to central repository. You can define own central (it is only property, which you can redefine)

<repository>
        <id>central</id>
        <name>My Central</name>
        <layout>default</layout>
        <url>${my_url}</url>
    </repository>

Make the same for snapshot, plugin repository etc. Configuration you can use as profile - it will be optional. See ingyhere answer.



标签: maven maven-3