java Maven 离线 - mvn-plugins 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1245076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:47:47  来源:igfitidea点击:

Maven offline - problem with mvn-plugins

javamaven-2offline-mode

提问by rafa.ferreira

I'm using maven in my project and I need to run the build in a non-internet-accessmachine.

我在我的项目中使用 maven,我需要在非互联网访问机器上运行构建。

When I test my project build everything is working, but when I run the build in a future moment, the maven try to update the mvn-pluginsand this sht* is broking my build.

当我测试我的项目构建时一切正常,但是当我在未来运行构建,maven 尝试更新 mvn-plugins并且这个sht* 破坏了我的构建。

My config file: settings.xmlfrom mvn.

我的配置文件:来自 mvn 的settings.xml

    <profile>
      <id>blaProfile</id>
      <repositories>
        <repository>
          <id>blaRepo</id>
          <url>file://${bla.3rdParty.home}/maven/.m2/repository</url>
          <layout>default</layout>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>blaRepo</id>
          <url>file://${bla.3rdParty.home}/maven/.m2/repository</url>
          <layout>default</layout>
        </pluginRepository>
      </pluginRepositories>
    </profile>

  <activeProfiles>
    <activeProfile>blaProfile</activeProfile>
  </activeProfiles>

And I ran my maven is with the params:

我用参数运行了我的 Maven :

mvn -npu -bla.3rdParty.home="$(THE_CORRECT_PATH)" package

I saw that maven try to update some mvn-plugins for some time, but the option:

我看到 maven 尝试更新一些 mvn-plugins 一段时间,但选项:

-npu,--no-plugin-updates      Suppress upToDate check for any relevant

Should work for this updates.

应该适用于此更新。

Well waiting some help on that!

那么等待一些帮助!

Thanks in advance!

提前致谢!



UPDATE(1):更新(1):


我在看的是我可以使用设置:

<usePluginRegistry>true</usePluginRegistry>

Inside my settings.xmland with this, I'll have a plugin-registry.xmlinside ${user.home}/.m2 that I can config and force the maven plugins versions.

But it's not working! :(

在我的settings.xml 中,我将在 ${user.home}/.m2 中有一个plugin-registry.xml,我可以配置和强制 maven 插件版本。

但它不起作用!:(

回答by hohonuuli

Before you go offline run the following:

在离线之前运行以下命令:

mvn dependency:go-offline

That will download all your dependencies and plugins that you need to build your project into ~/.m2/repository.

这会将构建项目所需的所有依赖项和插件下载到 ~/.m2/repository 中。

Once you've run that you can now build your project offline using the '-o' flag:

运行后,您现在可以使用“-o”标志离线构建项目:

mvn install -o

回答by itaifrenkel

In order to cache plugins into the .m2/repository folder you would need to specify all plugins explicitly with the mvn <maven-plugin-name>:help

为了将插件缓存到 .m2/repository 文件夹中,您需要使用 mvn <maven-plugin-name>:help

You would also need to specify explicit version for each plugin in the <plugins>or <pluginsManagement>section of your pom.xml

您还需要在pom.xml的<plugins>or<pluginsManagement>部分为每个插件指定显式版本

<plugin> 
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.19</version>
    </dependency>
  </dependencies>
</plugin>

This is needed to make sure that mvn install -ouses the same plugin version.

这是确保mvn install -o使用相同插件版本所必需的。

Ofcourse you would also need to run mvn dependency:go-offlineto take care of your compile and test dependencies.

当然,您还需要运行mvn dependency:go-offline以处理编译和测试依赖项。

mvn assembly:help compiler:help enforcer:help exec:help failsafe:help install:help jar:help resources:help surefire:help mvn dependency:go-offline mvn compile --offline

mvn assembly:help compiler:help enforcer:help exec:help failsafe:help install:help jar:help resources:help surefire:help mvn dependency:go-offline mvn compile --offline

回答by evgeni

It should suffice to run a mvn clean installon your code. The next time, when you run it in an offline mode you can use the following options:

mvn clean install在您的代码上运行 a就足够了。下一次,当您在离线模式下运行它时,您可以使用以下选项:

mvn -Dmaven.repo.local=..\repository –o clean install

-otells Maven notto try to update its dependencies over the network and with -Dmaven.repo.localyou provide the path of the repository which contains all the required dependencies. Alternatively, you can add the path of the repository in your settings.xml in the localRepositorytag and add an <offline>true</offline>. You can configure the same in your Maven Eclipse configurations.

-o告诉 Maven不要尝试通过网络更新其依赖项,并-Dmaven.repo.local提供包含所有必需依赖项的存储库路径。或者,您可以在localRepository标签中的 settings.xml 中添加存储库的路径并添加一个<offline>true</offline>. 您可以在 Maven Eclipse 配置中进行相同的配置。

回答by Rich Seller

I think this happens because Maven hasn't got the metadata available locally to determine if its plugin versions are correct. If you specify exact versions for your plugins (which is a good idea for reproducability anyway), it doesn't need to do the check, so will not try to connect to the remote repositories.

我认为这是因为 Maven 没有获得本地可用的元数据来确定其插件版本是否正确。如果您为插件指定了确切的版本(无论如何这对于可重复性来说是一个好主意),则不需要进行检查,因此不会尝试连接到远程存储库。

By specify exact versions, I mean that in your project's POM you should add the version to the plugin declaration. For example:

通过指定确切的版本,我的意思是在您项目的 POM 中,您应该将版本添加到插件声明中。例如:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <!-- set the version explicitly-->
    <version>2.0</version>
  </plugin>
</plugins>

Note you can also force Maven to use internal repositories instead of Central by setting up repository mirrors. See this answer for more detailson using repository managers and mirrors.

请注意,您还可以通过设置存储库镜像来强制 Maven 使用内部存储库而不是 Central。有关使用存储库管理器和镜像的更多详细信息,请参阅此答案

In the config you included, you're setting your remote repository to point to your local repository, this is not a good idea. To run offline you should either pass -o at the command line or add this to your settings.xml:

在您包含的配置中,您将远程存储库设置为指向本地存储库,这不是一个好主意。要离线运行,您应该在命令行中传递 -o 或将其添加到您的 settings.xml 中:

<offline>true</offline>

回答by bedla.czech

After some debugging I found that maven-dependency-plugin(version 3.1.1at the time of writing) is unable to resolve plugin's dependencies when specified like:

经过一些调试后,我发现maven-dependency-plugin3.1.1撰写本文时的版本)在指定如下时无法解析插件的依赖项:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>3.0.0-M3</version>
 <dependencies>
  <dependency>    <--- this is not going to be resolved by dependency:go-offline command !!!
   <groupId>org.apache.maven.surefire</groupId>
   <artifactId>surefire-junit4</artifactId>
   <version>3.0.0-M3</version>
  </dependency>
 </dependencies>
</plugin>

After that I found go-offline-maven-pluginwhich just works! Pls see https://github.com/qaware/go-offline-maven-pluginfor more info.

之后我发现go-offline-maven-plugin哪个有效!请参阅https://github.com/qaware/go-offline-maven-plugin了解更多信息。

<plugin>
 <groupId>de.qaware.maven</groupId>
 <artifactId>go-offline-maven-plugin</artifactId>
 <version>x.y.z</version>
</plugin>

Current version could be found here https://mvnrepository.com/artifact/de.qaware.maven/go-offline-maven-pluginand Maven issue here https://issues.apache.org/jira/browse/MDEP-82

当前版本可以在这里找到https://mvnrepository.com/artifact/de.qaware.maven/go-offline-maven-plugin和 Maven 问题在这里https://issues.apache.org/jira/browse/MDEP-82

回答by Marcello de Sales

Maven Go-offline + Isolated Docker Multi-stage Image Builds

Maven 下线 + 隔离 Docker 多阶段镜像构建

My answer is for both a local build or a Dockerized environment, which is isolated on the nature of how docker images are built. This uses Maven 3.6.3-jdk-8.

我的答案是针对本地构建或 Dockerized 环境,这与构建 docker 映像的性质有关。这使用 Maven 3.6.3-jdk-8

With this answer, you know exactly how much time your CI spends on downloading, compiling, testing, packaging...

有了这个答案,您就可以确切地知道您的 CI 在下载、编译、测试、打包...

Finally, also answering to the old question on Jira for the go-offline https://issues.apache.org/jira/browse/MDEP-82?focusedCommentId=16997793&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-16997793

最后,还回答了关于 Jira 下线的旧问题https://issues.apache.org/jira/browse/MDEP-82?focusedCommentId=16997793&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment -tabpanel#comment-16997793

  • Update pom.xmldependencies
    • maven-dependency-plugin
    • surefire-junit-platform
  • Call go-offlineresolving dependencies
  • Call any mvncommand with the switch --off-line
  • 更新pom.xml依赖
    • maven-dependency-plugin
    • surefire-junit-platform
  • 调用go-offline解析依赖
  • mvn使用开关调用任何命令--off-line

Set latest versions of the plugins

设置插件的最新版本

@@ -23,6 +23,9 @@
         <junit-jupiter.version>5.5.2</junit-jupiter.version>
         <common-io.version>2.6</common-io.version>
         <jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
+        <!-- https://issues.apache.org/jira/browse/MDEP-82 -->
+        <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
+        <surefire-junit-platform.version>2.22.2</surefire-junit-platform.version>
         <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
         <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
         <maven-surefire-report-plugin.version>2.22.2</maven-surefire-report-plugin.version>
...
...
     <build>
         <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <version>${maven-dependency-plugin.version}</version>
+            </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
@@ -135,6 +143,11 @@
                     <target>${java.version}</target>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.surefire</groupId>
+                <artifactId>surefire-junit-platform</artifactId>
+                <version>${surefire-junit-platform.version}</version>
+            </plugin>

Create the Go-offline cache

创建 Go-offline 缓存

  • This will ensure all the dependencies are downloaded
  • More than 3m to download all dependencies
  • 这将确保下载所有依赖项
  • 3m多下载所有依赖
FROM maven:3.6.3-jdk-8 AS dependencies-downloaded
...
...
COPY pom.xml /usr/src/app/pom.xml
COPY settings.xml /usr/src/app/settings.xml
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml dependency:resolve-plugins dependency:go-offline

enter image description here

在此处输入图片说明

Call compile with --offline

使用 --offline 调用编译

  • We can reuse the same image for compilation
  • Only takes 7s because nothing is downloaded
  • 我们可以重用相同的图像进行编译
  • 只需要 7 秒,因为什么都没有下载
FROM dependencies-downloaded AS compile
COPY app /usr/src/app
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml compile --offline

enter image description here

在此处输入图片说明

Call tests with --offline

使用 --offline 调用测试

  • We can reuse the same image for tests
  • Taking 18s to run the test cases, without any download whatsoever
  • 我们可以重复使用相同的图像进行测试
  • 运行测试用例需要 18 秒,无需任何下载
FROM compile AS tests
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml test --offline

enter image description here

在此处输入图片说明

Call package with --offline

使用 --offline 调用包

  • We can reuse the same image for the final jar
  • Skipping even the tests ran in the previous docker image
  • Taking way less than before
  • 我们可以为最终的 jar 重用相同的图像
  • 甚至跳过在之前的 docker 镜像中运行的测试
  • 走得比以前少
FROM tests AS package
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml package -Dmaven.test.skip=true --offline

enter image description here

在此处输入图片说明

The final runtime image is a Docker image from the package.

最终的运行时映像是来自包的 Docker 映像。

FROM JRE
COPY --from package /usr/src/app/target /bin
...
...