【问题标题】:Maven offline - problem with mvn-pluginsMaven 离线 - mvn-plugins 的问题
【发布时间】:2009-08-07 14:34:14
【问题描述】:

我在我的项目中使用 maven,我需要在 non-internet-access 机器上运行构建。

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

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

我用参数运行了我的 maven:

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

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

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

应该适用于此更新。

好吧,等待一些帮助!

提前致谢!


更新(1):
我正在看的是我可以使用以下设置:
<usePluginRegistry>true</usePluginRegistry>

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

但它不工作! :(

【问题讨论】:

标签: java maven-2 offline-mode


【解决方案1】:

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

mvn dependency:go-offline

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

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

mvn install -o

【讨论】:

  • 这是一个好点,但我的想法是创建一种带有创建war文件的程序的CD/DVD,在这个例子中是MVN。通过它,我创建了一个“文件”存储库,其中包含我需要的所有依赖项。根据您的想法,我将不得不强制用户将所有依赖项放入他的 .m2 目录中。这是一个选项,但我正朝着其他方向发展。
  • 它不会在 mvn 3.5.0 中下载插件依赖项(插件本身已下载但依赖 jar 缺失)。每当我尝试使用 mvn install -o 进行编译时,它都会抛出依赖项尚未下载的错误。
  • 您在离线之前是否尝试过运行构建mvn install?我相信应该获取插件和插件依赖项。
【解决方案2】:

为了将插件缓存到 .m2/repository 文件夹中,您需要使用 mvn &lt;maven-plugin-name&gt;:help 明确指定所有插件

您还需要在 pom.xml 的 &lt;plugins&gt; 或 &lt;pluginsManagement&gt; 部分中为每个插件指定显式版本

<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:help compiler:help enforcer:help exec:help failsafe:help install:help jar:help resources:help surefire:help mvn dependency:go-offline mvn compile --offline

【讨论】:

    【解决方案3】:

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

    我的答案是针对本地构建或 Dockerized 环境,这与构建 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解决依赖关系
    • 使用开关--off-line 调用任何mvn 命令

    设置插件的最新版本

    @@ -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 调用编译

    • 我们可以重复使用相同的图像进行编译
    • 只需要7s,因为没有下载任何东西
    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
    ...
    ...
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        经过一些调试,我发现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 了解更多信息。

        <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

        【讨论】:

          【解决方案6】:

          我认为这是因为 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 使用内部存储库而不是 Central。请参阅this answer for more details 了解如何使用存储库管理器和镜像。

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

          <offline>true</offline>
          

          【讨论】:

          • @Rich,当您在指定我的插件版本时提到您是在说 MAVEN 插件,对吗?执行此规范的最佳方法是什么?请在我的帖子中查看我的更新,看看我是否做得对。谢谢!
          猜你喜欢
          • 2021-10-04
          • 1970-01-01
          • 2011-10-12
          • 2016-11-27
          • 1970-01-01
          • 2020-07-26
          • 1970-01-01
          • 2023-03-10
          • 2013-12-10
          相关资源
          最近更新 更多