Maven 离线 - mvn-plugins 问题

发布于 2024-08-01 16:20:28 字数 1708 浏览 5 评论 0原文

我在项目中使用 Maven,并且需要在非互联网访问计算机中运行构建。

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

我的配置文件:来自 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>

我运行我的maven,参数如下:

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

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

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

应该适用于此更新。

好吧,等待一些帮助!

提前致谢!


UPDATE(1):
What I'm looking at, is that I could use the setting:

<usePluginRegistry>true</usePluginRegistry>

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

但这不起作用! :(

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

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-plugins and this sht* is broking my build.

My config file: settings.xml from mvn.

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

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:

-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):
What I'm looking at, is that I could use the setting:

<usePluginRegistry>true</usePluginRegistry>

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

But it's not working! :(

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

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

发布评论

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

评论(6

心凉 2024-08-08 16:20:28

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

我的答案是针对本地构建或 Docker 化环境,后者根据 docker 镜像构建方式的本质进行隔离。 这使用 Maven 3.6.3-jdk-8

有了这个答案,你就可以准确地知道你的 CI 在下载、编译、测试、打包上花费了多少时间……

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

  • 更新 pom.xml依赖项
    • maven-dependency-plugin
    • surefire-junit-platform

  • 调用go-offline解决依赖关系
  • 调用任何mvn 带开关的命令 --off-line

设置插件的最新版本

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

创建离线缓存

  • 这将确保下载所有依赖项
  • 超过 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

在此处输入图像描述

使用 --offline 调用编译

  • 我们可以重复使用相同的图像进行编译
  • 只需 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

在此处输入图像描述

使用 --offline 调用测试

  • 我们可以重复使用相同的图像进行测试
  • 花费 18 秒来运行测试用例,无需任何下载
FROM compile AS tests
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml test --offline

在此处输入图像描述

使用 --offline 调用包

  • 我们可以在最终的 jar 中重用相同的图像
  • 甚至跳过在之前的 docker 图像中运行的测试
  • 采取比以前更少的方式
FROM tests AS package
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml package -Dmaven.test.skip=true --offline

在此处输入图像描述

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

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

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

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.

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

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

  • Update pom.xml dependencies
    • maven-dependency-plugin
    • surefire-junit-platform
  • Call go-offline resolving dependencies
  • Call any mvn command with the switch --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

  • This will ensure all the dependencies are downloaded
  • More than 3m to download all dependencies
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

  • We can reuse the same image for compilation
  • Only takes 7s because nothing is downloaded
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

  • We can reuse the same image for tests
  • Taking 18s to run the test cases, without any download whatsoever
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

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

FROM JRE
COPY --from package /usr/src/app/target /bin
...
...
腻橙味 2024-08-08 16:20:28

经过一番调试后,我发现 maven-dependency-plugin (撰写本文时的版本 3.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>

之后我发现go-offline-maven-plugin 就可以了! 请参阅 https://github.com/qaware/go-offline-maven-plugin< /a> 了解更多信息。

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

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

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

<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-plugin which just works! Pls see https://github.com/qaware/go-offline-maven-plugin for more info.

<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-plugin and Maven issue here https://issues.apache.org/jira/browse/MDEP-82

晚风撩人 2024-08-08 16:20:28

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

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

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

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

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

-o tells Maven not to try to update its dependencies over the network and with -Dmaven.repo.local you 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 localRepository tag and add an <offline>true</offline>.
You can configure the same in your Maven Eclipse configurations.

女中豪杰 2024-08-08 16:20:28

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

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

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

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

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

<offline>true</offline>

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.

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

<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 details on using repository managers and mirrors.

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:

<offline>true</offline>
々眼睛长脚气 2024-08-08 16:20:28

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

mvn dependency:go-offline

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

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

mvn install -o

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.

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

mvn install -o
抹茶夏天i‖ 2024-08-08 16:20:28

为了将插件缓存到 .m2/repository 文件夹中,您需要使用 mvn:help 显式指定所有插件,

您还需要为每个插件指定显式版本pom.xml 的 部分中的插件

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

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

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

mvn assembly:帮助编译器:帮助执行者:帮助执行者:帮助failsafe:帮助安装:帮助jar:帮助资源:帮助surefire:帮助
mvn 依赖:脱机
mvn 编译 --offline

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

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

<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 -o uses the same plugin version.

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

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

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