Maven:为什么我 运行 mvn checkstyle:checkstyle 没有 pom.xml 配置?

Maven: Why can I run mvn checkstyle:checkstyle without pom.xml config?

我正在使用一个基本的 Maven 项目,其中仅在 pom.xml 中定义了以下内容:

为什么我可以在命令window中运行mvn checkstyle:checkstyle?我不应该定义 pom.xml 的 checkstyle 插件吗?我错过了什么?

编辑:安装了 eclipse 插件 "Eclipse Checkstyle Plug-In"。是这个原因吗?如果是,maven是如何与之通信的?

如您所见,可以直接从命令行执行插件,而无需在项目的 POM 文件中配置插件。

要知道你想执行哪个插件maven使用插件前缀解析。

使用您的命令行,mvn checkstyle:checkstyle,作为示例,这大致是 maven 所做的:

  • 从执行中获取前缀,即冒号之前的位。在你的情况下 checkstyle.
  • 将其解析为插件的可能名称(artifactId)。为此,它使用两种可能的格式:
    • maven-${prefix}-plugin(官方 Maven 插件)
    • ${prefix}-maven-plugin(对于其他来源的插件)
  • 所以现在它有 maven-checkstyle-plugin 作为插件的 artifactId
  • 使用此 artifactId 和配置的 groupId 之一搜索插件(默认情况下,maven 使用 groupId org.apache.maven.plugins 搜索)
  • 它找到 maven-checkstyle-plugin
  • 执行目标(命令中冒号后的位),因此在这种情况下 checkstyle goal

所有这些都比我在官方 maven 文档的 plugin prefix mapping 页面上解释得更详细、更好。

原因是插件可能不需要在 pom 中显式配置,也不需要绑定到阶段 运行。因此,它总是可以通过直接调用 运行(在调用 Maven 时指定目标而不是阶段):

mvn [options] [<goal(s)>] [<phase(s)>]

查看 lifecycle 文档:

A plugin goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.

绝对与Eclipse插件无关