简体   繁体   English

spring-boot:run如何与Maven配合使用?

[英]How does spring-boot:run work with maven?

I'm learning the maven build process, and there's on point of contention that stands out as not making a whole lot of sense. 我正在学习Maven的构建过程,有一点争论很突出,那就是没有任何意义。

The way I understand maven, there's the default lifecycles (default, site, clean), each of which has multiple phases. 据我了解Maven的方式,有默认的生命周期(默认,站点,干净),每个生命周期都有多个阶段。 There are some built-in plugins associated with a few phases, and by adding additional plugins, you are able to add extra functionality to the pre-existing phases. 有一些与某些阶段关联的内置插件,并且通过添加其他插件,您可以为先前存在的阶段添加其他功能。

When you specify a phase to run directly within maven, it will go through all the phases up to this point within the same lifecycle, and end with executing your specified phase. 当您指定一个阶段直接在maven中运行时,它将在同一生命周期内经历所有阶段直至这一点,并以执行指定的阶段结束。 To the best of my knowledge however, spring-boot:run is a goal, and thus should not invoke other phases when run. 据我所知, spring-boot:run是一个目标,因此在运行时不应调用其他阶段。 That being said, running spring-boot:run via maven does run other phases ( default-cli , pre-unit-test , default-resources , reserve-container-port , and some others). 也就是说,通过maven运行spring-boot:run确实会运行其他阶段( default-clipre-unit-testdefault-resourcesreserve-container-port等)。 It looks to me that spring has created some new phases ( reserve-container-port for instance), but I'm not sure how to find the jar file in which the configuration for these is located. 在我看来,spring创建了一些新阶段(例如, reserve-container-port ),但是我不确定如何找到这些配置的jar文件。

My questions thus are twofold: 因此,我的问题是双重的:

  1. Is spring-boot:run a goal as I understand it to be? 按照我的理解, spring-boot:run一个目标吗? If so, how does running this goal in turn run other phases? 如果是这样,那么实现此目标又将如何运行其他阶段? To the best of my knowledge, only specifying a phase as a target will run the other previous phases in order, not directly specifying a goal. 据我所知,仅将一个阶段指定为目标将按顺序运行其他先前阶段,而不直接指定目标。
  2. Where is the configuration file for all of this located? 所有这些的配置文件在哪里? In which spring jar file can I find the configuration file that lets all of the above work correctly. 我可以在哪个spring jar文件中找到可以使上述所有功能正常工作的配置文件。

Is spring-boot:run a goal as I understand it to be 是spring-boot吗?据我所知是一个目标

In fact, spring-boot is a maven plugin and run is one of its goals. 实际上, spring-boot是一个maven插件, run是其目标之一。

Here is the official documentation of this plugin. 这是此插件的官方文档

In this documentation, I don't find the default maven phase in which this goal is executed. 在本文档中,我找不到执行该目标的默认Maven阶段。 But in the github source code , I find out that this goal is executed in the TEST_COMPILE phase 但是在github源代码中 ,我发现这个目标是在TEST_COMPILE阶段执行的

only specifying a phase as a target will run the other previous phases in order, not directly specifying a goal. 仅将一个阶段指定为目标将按顺序运行其他先前阶段,而不直接指定目标。

In fact, this how maven works. 实际上,这是行家的工作方式。 Here is an introduction to the maven lifecycle 这是Maven生命周期介绍

Where is the configuration file for all of this located? 所有这些的配置文件在哪里?

It is defined in your spring-boot pom parent: 它在您的spring-boot pom父级中定义:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>${spring-boot-starter-parent.version}</version>
</parent>

Hope this help :) 希望这个帮助:)

As the other answer misses a bit of the information I was looking for, I thought I'd add the results of my research in case anyone else is curious. 由于其他答案错过了我一直在寻找的信息,我想我会添加研究结果,以防其他人感到好奇。 Looking in 往里看

C:\\HOME_DIR.m2\\repository\\org\\springframework\\boot\\spring-boot-maven-plugin\\VERSION_NUMBER\\META-INF\\maven\\plugin.xml C:\\ HOME_DIR.m2 \\ repository \\ org \\ springframework \\ boot \\ spring-boot-maven-plugin \\ VERSION_NUMBER \\ META-INF \\ maven \\ plugin.xml

gives the configuration file for most of the related goals for spring with maven. 给出了带有maven的spring的大多数相关目标的配置文件。 Specifically, the snippet below describes how the spring-boot:run goal works. 具体来说,以下代码段描述了spring-boot:run目标的工作方式。

<mojo>
      <goal>run</goal>
      <description>Run an executable archive application.</description>
      <requiresDependencyResolution>test</requiresDependencyResolution>
      <requiresDirectInvocation>false</requiresDirectInvocation>
      <requiresProject>true</requiresProject>
      <requiresReports>false</requiresReports>
      <aggregator>false</aggregator>
      <requiresOnline>false</requiresOnline>
      <inheritedByDefault>true</inheritedByDefault>
      <phase>validate</phase>
      <executePhase>test-compile</executePhase>
      <implementation>org.springframework.boot.maven.RunMojo</implementation>
      <language>java</language>
      <instantiationStrategy>per-lookup</instantiationStrategy>
      <executionStrategy>once-per-session</executionStrategy>
      <since>1.0.0</since>
      <threadSafe>false</threadSafe>
      ...
</mojo>

Specifically, the <executePhase> tag (detailed partially at https://maven.apache.org/developers/mojo-api-specification.html ), which (I believe) lets this goal execute a different phase as it's run. 具体来说, <executePhase>标记(部分详细说明, 网址https://maven.apache.org/developers/mojo-api-specification.html ),(我相信)该标记可让该目标在运行时执行不同的阶段。

I missed this detail before (it doesn't seem to be documented anywhere very well either). 我以前错过了这个细节(似乎在任何地方都没有很好的记录)。 Either way, this explanation is satisfactory enough for me. 无论哪种方式,这种解释对我来说都足够令人满意。 If anyone finds better documentation for this tag, I'd appreciate a link :) 如果有人找到此标签的更好文档,请提供链接:)

To directly answer your question from maven's standpoint: 从maven的角度直接回答您的问题:

All the stuff about the lifecycles, phases and associated plugins - you've got it correctly. 有关生命周期,阶段和相关插件的所有信息-您已正确找到它。

However, you miss one point, in maven it is possible to directly run plugin's goal . 但是,您错过了一点,在Maven中可以直接运行插件的目标 Think about these goals as logical unit of code that does something, a class with method "main" if you wish that you can run, give it parameters and so forth. 将这些目标想像成做某事的逻辑代码单元,如果希望运行,给它提供参数等,则使用方法为“ main”的类。

In maven's plugins world, a piece of code in plugin associated with a goal is called a Mojo, a class that maven can execute. 在maven的插件世界中,插件中与目标相关的一段代码称为Mojo,这是maven可以执行的类。

So one plugin contains at least one mojo, its possible that it will contain many if plugin developers decide that it should do many related things. 因此,一个插件至少包含一个mojo,如果插件开发人员决定应该做许多相关的事情,那么它可能会包含许多插件。 Here is an example of a plugin having 4 different goals ( Mojos in code of the plugin itself) as of now. 到目前为止,这是一个具有4个不同目标的插件示例 (插件本身的代码中为Mojos)。

So, the syntax of this direct invocation is: 因此,此直接调用的语法为:

mvn plugin-groupId:plugin-artifactId:plugin-version:goal
mvn plugin-groupId:plugin-artifactId:goal
mvn plugin-prefix:goal

Under some conditions (see official maven documentation ), you can omit some of this syntax and this is exactly what happens in your case. 在某些情况下(请参阅Maven官方文档 ),您可以省略某些语法,这正是您所遇到的情况。

Of course, when you run goals directly like this it completely bypasses the lifecycle execution with phases and everything, so if you, say, want to run directly the goal of tests, but you've never compiled the tests, it will fail. 当然,当您像这样直接运行目标时,它会完全绕开阶段和所有阶段的生命周期执行,因此,如果您想直接运行测试目标,但是您从未编译过测试,那么它将失败。 Example: 例:

mvn test // run all phases of the default lifecycle including compilation, tests 
         // compilation, test execution - will succeed on a "clean" project

mvn surefire:test // run directly a goal called "test" inside plugin called "surefire"

The same thing happens with mvn spring-boot:run mvn spring-boot:run发生同样的事情

You're running spring boot maven plugin, maven resolves the version of the plugin to the version of the build since you don't specify it and then runs a goal called "run" 您正在运行spring boot maven插件,由于未指定该插件,因此maven将插件的版本解析为构建版本,然后运行一个名为“ run”的目标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM