如何配置 Maven 进行离线开发?

发布于 2024-12-02 11:16:19 字数 65 浏览 1 评论 0原文

maven 是否需要在某些时候连接到互联网才能使用它?是指专门获取用于编译、清理、打包等的内部 Maven 插件吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

阪姬 2024-12-09 11:16:19

您可以使用-o--offline 选项(例如mvn -o install)以“离线”模式运行Maven。当然,本地存储库中不可用的任何工件都会失败。 Maven 并不基于分布式存储库,但它们确实使事情变得更加无缝。正是由于这个原因,许多商店使用与中央存储库增量同步的内部镜像。

此外,mvn dependency:go-offline 可用于确保您在开始脱机工作之前已在本地安装了所有依赖项。

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.

悸初 2024-12-09 11:16:19

如果您的 LAN 中有一台可以访问互联网的 PC,则应该安装本地 Maven 存储库。

我推荐Artifactory 开源。这就是我们在我们组织中使用的,设置非常简单。

Artifactory 充当构建工具(Maven、Ant、Ivy、Gradle 等)和外部世界之间的代理。

它会缓存远程工件,这样您就不必一遍又一遍地下载它们。

它阻止对内部工件的不需要的(有时是安全敏感的)外部请求,并控制工件的部署方式和位置以及由谁部署。

设置 Artifactory 后,您只需要在开发机器中更改 Maven 的 settings.xml

<?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>

我们使用此解决方案是因为我们的开发机器中存在互联网访问问题,并且某些工件下载了损坏的文件或没有下载完全下载。从那以后我们就没有遇到过问题。

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.

秋叶绚丽 2024-12-09 11:16:19

为此,您有两种选择:

1.) 在 settings.xml 中进行更改,将其添加到第一个标签中

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

2.) 使用 -o 标签进行离线命令。

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

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
妄想挽回 2024-12-09 11:16:19

Maven 需要本地存储库中的依赖项。获取它们的最简单方法是通过互联网访问(或者使用此处提供的其他解决方案更困难)。

因此,假设您可以暂时访问互联网,您可以准备使用 离线maven-dependency-plugin 及其 依赖:离线 目标。这会将所有项目依赖项下载到本地存储库(当然,依赖项/插件的更改将需要新的互联网/中央存储库访问权限)。

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).

哆啦不做梦 2024-12-09 11:16:19

遗憾的是dependency:go-offline对我不起作用,因为它没有缓存
一切,即。 POM 文件和其他隐式提及的依赖项。

解决方法是在 settings.xml 文件中使用 ...本地存储库位置 > 或通过使用 -Dmaven.repo.local=... 参数运行 mvn
初始项目构建后,应缓存所有必要的工件,然后您可以以相同的方式引用存储库位置,同时以离线模式运行 Maven 构建 (mvn -o ...)。

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 ...).

情绪操控生活 2024-12-09 11:16:19

如果您使用 IntelliJ,您只需转到首选项 -> 构建、执行、部署 -> 构建工具 -> Maven 并选中/取消选中脱机工作

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

寄居人 2024-12-09 11:16:19

在离线之前,您必须确保所有内容都在本地存储库中,这是离线工作时所必需的。对您打算处理的项目/pom 运行“mvn dependency:go-offline”将减少实现此目标的工作量。

但这通常不是全部,因为 dependency:go-offline 只会下载“裸构建”插件(go-offline/resolve-plugins 不解析所有插件依赖关系)。因此,您必须找到一种方法将部署/测试/站点插件(也许还有其他插件)及其依赖项下载到您的存储库中。

此外, dependency:go-offline 不会下载 pom 的工件本身,因此您必须 dependency:copy 它(如果需要)。

有时 - 正如 MaDa 所写 - 当你处于离线状态时,你不知道你需要什么,这使得你几乎不可能拥有一个“足够的”存储库。

无论如何,拥有一个正确填充的存储库,您只需添加“true”到 Maven 的 settings.xml 即可离线。

离线时请勿更改用于填充存储库的 Maven 配置文件 (id)。 Maven 通过“身份”识别其元数据中下载的工件,该“身份”与配置文件 ID 绑定。

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.

坏尐絯℡ 2024-12-09 11:16:19

一个新插件似乎修复了 mvn dependency:go-offline 的缺点:

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

将其添加到您的 pom 中,然后运行 ​​mvn -T1C de.qaware.maven:go-offline-maven-plugin:解决依赖关系。一旦您设置了所有动态依赖项,maven 将不会尝试再次下载任何内容(直到您更新版本)。

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).

苏璃陌 2024-12-09 11:16:19

这对你有用吗?

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

不要忘记将其添加到您的插件存储库并将 URL 指向您的存储库所在的位置。

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

如果没有,您可能需要在您的计算机上运行本地服务器,例如 apache。

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.

貪欢 2024-12-09 11:16:19

我的经验表明 -o 选项无法正常工作,并且脱机目标远远不足以允许完全脱机构建:

我可以验证的解决方案包括使用 --legacy-local- repository maven 选项而不是 -o (离线)选项
使用本地存储库代替分发存储库

此外,我必须复制每个maven-metadata-maven2_central.xml文件将 local-repo 转换为 maven 期望的 maven-metadata.xml 形式。

请参阅我在此处找到的解决方案。

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.

泪痕残 2024-12-09 11:16:19


(来源:jfrog.com)

只需使用 Maven存储库服务器,例如 Sonatype Nexus http://www.sonatype.org/nexus/ 或 JFrog Artifactory < a href="https://www.jfrog.com/artifactory/" rel="nofollow noreferrer">https://www.jfrog.com/artifactory/。

一个开发人员构建项目后,由下一个开发人员或 Jenkins CI 构建将不需要 Internet 访问。

Maven 存储库服务器还可以配置代理来访问 Maven Central(或更需要的公共存储库),并且它们可以在远程存储库中拥有 cynch 的工件列表。


(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.

放赐 2024-12-09 11:16:19

直接回答你的问题:它不需要互联网连接,但需要访问局域网或本地磁盘上的存储库(使用此处发布的其他人的提示)。

如果您的项目尚未处于成熟阶段,这意味着当 POM 经常更改时,离线模式将非常不切实际,因为您也必须经常更新存储库。除非您可以获得包含您需要的一切的存储库的副本,但您怎么知道呢?通常,您从头开始启动一个存储库,并在开发过程中逐渐克隆它(在连接到另一个存储库的计算机上)。 repo1.maven.org 公共存储库的副本重达数百 GB,因此我也不建议使用暴力破解。

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.

So尛奶瓶 2024-12-09 11:16:19

这是一个清晰、直接的方法来缓存离线开发的 Maven 依赖项(基于 @luka5z 和其他人的评论):

  1. 当您可以访问互联网时,在本地缓存依赖项:
mvn -Dmaven.repo.local=dependencies install
  1. 断开互联网连接,验证离线模式编译是否成功:
mvn clean
mvn -o -Dmaven.repo.local=dependencies package

继续离线开发只要需要就可以。

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.

一个人的夜不怕黑 2024-12-09 11:16:19

对于任何正在寻找能够在本地完全下载依赖项的多模块项目无需构建项目的解决方案的人:

  1. 确保maven-dependency-plugin在根 pom.xml 文件中使用 3.8+:
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
      <plugins>
    </pluginManagement>
  1. 创建临时的仅 POM 项目布局(通用步骤):
#!/usr/bin/env bash

set -euo pipefail

project_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"   # --> this is just a reliable way of getting the parent directory of the script
cache_temp_build="$(mktemp -d)"
deps_dir="~/third_party/maven"

trap 'rm -rf ${cache_temp_build}' EXIT

# Copy pom.xml files to `cache_temp_build` directory
find . -name pom.xml -exec bash -c 'i="$1"; mkdir -p '"${cache_temp_build}"'/$(dirname $i); cp "$i" '"${cache_temp_build}"'/"$i"' shell {} \;

cd "${cache_temp_build}"

# Note: at this point in `cache_temp_build` there are only pom.xml files, there isn't the project code to build.

# Add all the modules to Maven reactor with package, this doesn't build the modules (since there is no code to build)
# Save dependencies to `deps_dir`
mvn package dependency:go-offline -Dmaven.repo.local="${deps_dir}"

# Remove remote repositories references
find "${deps_dir}" -path "*_remote.repositories" -exec rm {} \;
  1. 构建项目(项目特定步骤):
#!/usr/bin/env bash

set -euo pipefail

project_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"   # --> this is just a reliable way of getting the parent directory of the script
deps_dir="~/third_party/maven"

# Build the app in offline mode using dependencies in `deps_dir`
mvn -Dmaven.repo.local="${deps_dir}" --offline package -pl=<your-module>  -am -DskipTests

For anyone looking for a solution that works with multi-module projects without building the project that fully downloads dependencies locally:

  1. Ensure maven-dependency-plugin is using 3.8+ in the root pom.xml file:
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
      <plugins>
    </pluginManagement>
  1. Create a temporary POM-only project layout (generic step):
#!/usr/bin/env bash

set -euo pipefail

project_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"   # --> this is just a reliable way of getting the parent directory of the script
cache_temp_build="$(mktemp -d)"
deps_dir="~/third_party/maven"

trap 'rm -rf ${cache_temp_build}' EXIT

# Copy pom.xml files to `cache_temp_build` directory
find . -name pom.xml -exec bash -c 'i="$1"; mkdir -p '"${cache_temp_build}"'/$(dirname $i); cp "$i" '"${cache_temp_build}"'/"$i"' shell {} \;

cd "${cache_temp_build}"

# Note: at this point in `cache_temp_build` there are only pom.xml files, there isn't the project code to build.

# Add all the modules to Maven reactor with package, this doesn't build the modules (since there is no code to build)
# Save dependencies to `deps_dir`
mvn package dependency:go-offline -Dmaven.repo.local="${deps_dir}"

# Remove remote repositories references
find "${deps_dir}" -path "*_remote.repositories" -exec rm {} \;
  1. Build the project (project specific step):
#!/usr/bin/env bash

set -euo pipefail

project_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"   # --> this is just a reliable way of getting the parent directory of the script
deps_dir="~/third_party/maven"

# Build the app in offline mode using dependencies in `deps_dir`
mvn -Dmaven.repo.local="${deps_dir}" --offline package -pl=<your-module>  -am -DskipTests

臻嫒无言 2024-12-09 11:16:19

离线工作前的准备工作只需运行
mvn 依赖:离线

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

能怎样 2024-12-09 11:16:19
<offline> false </offline>

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

<offline> true <offline>

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

离线标签从 false 更改为 true 。

将从在线存储库下载

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文