Maven离线 - mvn-plugins的问题

raf*_*ira 5 java maven-2 offline-mode

我在我的项目中使用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>
Run Code Online (Sandbox Code Playgroud)

我用我的maven跑了params:

mvn -npu -bla.3rdParty.home="$(THE_CORRECT_PATH)" package
Run Code Online (Sandbox Code Playgroud)

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

-npu,--no-plugin-updates      Suppress upToDate check for any relevant
Run Code Online (Sandbox Code Playgroud)

应该适用于此更新.

那等待一些帮助!

提前致谢!


更新(1):
我正在看的是,我可以使用设置:

<usePluginRegistry>true</usePluginRegistry>
Run Code Online (Sandbox Code Playgroud)

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

但它不起作用!:(

hoh*_*uli 7

脱机之前,请运行以下命令:

mvn dependency:go-offline
Run Code Online (Sandbox Code Playgroud)

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

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

mvn install -o
Run Code Online (Sandbox Code Playgroud)

  • 它不会下载mvn 3.5.0中的插件依赖项(插件本身已下载但缺少依赖的jar).每当我尝试使用mvn install -o进行编译时,它都会抛出错误,即尚未下载依赖项. (2认同)

ita*_*kel 6

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>
Run Code Online (Sandbox Code Playgroud)

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


Mar*_*les 5

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>
Run Code Online (Sandbox Code Playgroud)

创建离线缓存

  • 这将确保下载所有依赖项
  • 下载所有依赖包超过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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用 --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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用 --offline 调用测试

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

在此输入图像描述

使用 --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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

FROM JRE
COPY --from package /usr/src/app/target /bin
...
...
Run Code Online (Sandbox Code Playgroud)


Ric*_*ler 0

我认为发生这种情况是因为 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>
Run Code Online (Sandbox Code Playgroud)

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

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

<offline>true</offline>
Run Code Online (Sandbox Code Playgroud)