【问题标题】:How does Maven resolve plugin versions?Maven 如何解析插件版本?
【发布时间】:2014-01-15 03:04:14
【问题描述】:

我正在阅读the docs,但仍然对 Maven 如何决定下载哪些版本的插件感到困惑。

例如,考虑这个简单的场景:

  1. 一个空的本地存储库
  2. 默认设置.xml
  3. 运行一个简单的 maven 命令。例如,mvn archetype:generate 表示 maven-archetype-quickstart,如 Maven in 5 Minutes 文档中所述。

运行命令后,Maven 做的第一件事就是下载一堆插件。

Maven 正在下载的一些插件包括:

  • maven-clean-plugin-2.4.1
  • maven-install-plugin-2.3.1
  • maven-deploy-plugin-2.5

为什么是这些版本?

这些插件的最新版本是:

  • maven-clean-plugin-2.5
  • maven-install-plugin-2.5.1
  • maven-deploy-plugin-2.8.1

我查看了 maven-clean-plugin 的 LATEST version metadata,它是 2.5

我并不是一定要强制 Maven 使用这些插件的不同版本,我只是想了解为什么它会解析这些版本。

我正在使用 Apache Maven 3.0.3

【问题讨论】:

    标签: maven maven-3 maven-plugin


    【解决方案1】:

    Maven 在 META-INF/plexus/components.xml 中定义了 3 个生命周期:

    1.默认生命周期

    默认生命周期是在没有任何关联插件的情况下定义的。这些生命周期的插件绑定在 META-INF/plexus/default-bindings.xml

    中为每个打包单独定义

    例如jar 打包的插件绑定

    <phases>
      <process-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:resources
      </process-resources>
      <compile>
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile
      </compile>
      <process-test-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
      </process-test-resources>
      <test-compile>
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1:testCompile
      </test-compile>
      <test>
        org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
      </test>
      <package>
        org.apache.maven.plugins:maven-jar-plugin:2.4:jar
      </package>
      <install>
        org.apache.maven.plugins:maven-install-plugin:2.4:install
      </install>
      <deploy>
        org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
      </deploy>
    </phases>
    

    2。清洁生命周期 干净的生命周期是直接用它的插件绑定定义的。

    <phases>
      <phase>pre-clean</phase>
      <phase>clean</phase>
      <phase>post-clean</phase>
    </phases>
    <default-phases>
      <clean>
        org.apache.maven.plugins:maven-clean-plugin:2.5:clean
      </clean>
    </default-phases>
    

    3.网站生命周期 站点生命周期直接通过其插件绑定定义。

    <phases>
      <phase>pre-site</phase>
      <phase>site</phase>
      <phase>post-site</phase>
      <phase>site-deploy</phase>
    </phases>
    <default-phases>
      <site>
        org.apache.maven.plugins:maven-site-plugin:3.3:site
      </site>
      <site-deploy>
        org.apache.maven.plugins:maven-site-plugin:3.3:deploy
      </site-deploy>
    </default-phases>
    

    如果你想覆盖这些默认插件版本,你可以在命令提示符下进行如下操作

    mvn org.apache.maven.plugins:maven-clean-plugin:2.0:clean
    

    而不是

    mvn clean:clean
    

    【讨论】:

    • 当我查看 components.xml 的 v3.0.3 时,我确实看到了为 maven-clean-plugin 指定的版本,但没有看到为安装和部署插件指定的版本。这些版本(和其他插件)如何得到解决? svn.apache.org/repos/asf/maven/maven-3/tags/maven-3.0.3/…
    • 有一个超级 pom(所有 pom 的父级)包含在 maven 核心 jar 中,其中包含通用/核心插件的版本。
    • 安装或部署版本取决于打包类型,并在 default-bindings.xml 中定义。我已经相应地更新了我的答案。有关安装和部署 svn.apache.org/repos/asf/maven/maven-3/tags/maven-3.0.3/… 的版本,请参阅此 URL。希望这能回答您的问题。
    • default-bindings.xml 现在已从 moved 变为 LifecycleMapping providers
    【解决方案2】:

    每个版本的 Maven 二进制文件都有某些版本的插件版本硬编码。这是为了在用户不提供自己的版本信息的情况下进行一些可重现的构建。我们鼓励您这样做,一旦您使用您选择的插件版本填充 &lt;pluginManagement&gt; 部分,构建将开始使用它。

    【讨论】:

    • 这也是我的经历,让我困惑了一段时间,使用的版本来自哪里(事实上,直到我在这里找到你的答案。:)。自this answer states that Maven takes the latest version that exists in its local repo 以来更多。你对你描述的行为有参考吗?
    • 现在不能用谷歌搜索任何相关的东西,但从记忆中,最新版本是在一些旧版本的 maven 中获取的,但最终一些核心插件在 maven 发行版中编码了默认版本,因此未来版本的插件不会不要破坏旧的 Maven 发行版。
    • 但是为什么最新版本有一个更旧的硬编码插件版本? Maven 3.5.4 使用版本 2 中的 maven-clean-plugin。(它使用 2.5,而 3.1 已推出)。
    • @Tigerware 我知道这是旧的,但我认为这是为了避免构建依赖于您使用的 Maven 版本。
    【解决方案3】:

    如果您在构建项目时使用 -X 选项,就会变得非常清楚。请在另一个线程中检查我的答案: https://stackoverflow.com/a/48874533/4470135

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      相关资源
      最近更新 更多