如何配置 Maven 进行离线开发?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您可以使用
-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.如果您的 LAN 中有一台可以访问互联网的 PC,则应该安装本地 Maven 存储库。
我推荐Artifactory 开源。这就是我们在我们组织中使用的,设置非常简单。
设置 Artifactory 后,您只需要在开发机器中更改 Maven 的
settings.xml
:我们使用此解决方案是因为我们的开发机器中存在互联网访问问题,并且某些工件下载了损坏的文件或没有下载完全下载。从那以后我们就没有遇到过问题。
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.
After setting up Artifactory you just need to change Maven's
settings.xml
in the development machines: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.
为此,您有两种选择:
1.) 在 settings.xml 中进行更改,将其添加到第一个标签中
2.) 使用 -o 标签进行离线命令。
You have two options for this:
1.) make changes in the settings.xml add this in first tag
2.) use the -o tag for offline command.
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).
解决方法是在
settings.xml
文件中使用...
本地存储库位置 > 或通过使用-Dmaven.repo.local=...
参数运行mvn
。初始项目构建后,应缓存所有必要的工件,然后您可以以相同的方式引用存储库位置,同时以离线模式运行 Maven 构建 (
mvn -o ...
)。The workaround has been to specify a local repository location, either within
settings.xml
file with<localRepository>...</localRepository>
or by runningmvn
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 ...
).如果您使用 IntelliJ,您只需转到首选项 -> 构建、执行、部署 -> 构建工具 -> Maven 并选中/取消选中脱机工作。
If you're using IntelliJ, you can simply go to Preferences -> Build, Execution, Deployment -> Build Tools -> Maven and check/uncheck Work offline.
在离线之前,您必须确保所有内容都在本地存储库中,这是离线工作时所必需的。对您打算处理的项目/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.
一个新插件似乎修复了
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).这对你有用吗?
http://jojovedder.blogspot.com/2009/04 /running-maven-offline-using-local.html
不要忘记将其添加到您的插件存储库并将 URL 指向您的存储库所在的位置。
如果没有,您可能需要在您的计算机上运行本地服务器,例如 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.
If not, you may need to run a local server, e.g. apache, on your machines.
我的经验表明 -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 andthe 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 themaven-metadata.xml
form expected by maven.See the solution I found here.
(来源: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.
直接回答你的问题:它不需要互联网连接,但需要访问局域网或本地磁盘上的存储库(使用此处发布的其他人的提示)。
如果您的项目尚未处于成熟阶段,这意味着当 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.
这是一个清晰、直接的方法来缓存离线开发的 Maven 依赖项(基于 @luka5z 和其他人的评论):
继续离线开发只要需要就可以。
Here's a clear, straightforward way to cache Maven dependencies for offline development (based on @luka5z and others' comments):
Continue developing offline as long as needed.
对于任何正在寻找能够在本地完全下载依赖项的多模块项目无需构建项目的解决方案的人:
maven-dependency-plugin
在根pom.xml
文件中使用 3.8+:For anyone looking for a solution that works with multi-module projects without building the project that fully downloads dependencies locally:
maven-dependency-plugin
is using 3.8+ in the rootpom.xml
file:离线工作前的准备工作只需运行
mvn 依赖:离线
In preparation before working offline just run
mvn dependency:go-offline
将
离线标签从 false 更改为 true 。
将从在线存储库下载
to
Change the offline tag from false to true .
will download from repo online