Specific use of @ Mojo and @ Execute of Maven Plugin

  • 2021-11-13 07:18:24
  • OfStack

This article takes spring-boot-maven-plugin 2.5. 4 as an example

@Mojo defaultPhase

Take spring-boot-maven-plugin: start, whose @ Mojo defaultPhase is PRE_INTEGRATION_TEST, which is bound to this stage by default.


@Mojo(name = "start", requiresProject = true, defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST,
      requiresDependencyResolution = ResolutionScope.TEST)
public class StartMojo extends AbstractRunMojo {
}

In pom, we only need to specify goal and it will be executed in the PRE_INTEGRATION_TEST phase


<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
    <execution>
        <id>start</id>
        <goals>
            <goal>start</goal>
        </goals>
        <!-- If additional specification phase=verify, Will ignore defaultPhase, And in verify Stage execution -->
        <phase>verify</phase>
    </execution>
</executions>

@Execute phase

Take spring-boot-maven-plugin: run as an example. His @ Execute phase=TEST_COMPILE makes maven run a parallel life cycle until the specified stage TEST_COMPLIE before running the target. The plug-in target is not executed until phase is executed
So when you execute run, you always run to the TEST_COMPLIE phase


@Mojo(name = "run", requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE,
      requiresDependencyResolution = ResolutionScope.TEST)
@Execute(phase = LifecyclePhase.TEST_COMPILE)
public class RunMojo extends AbstractRunMojo {

References

maven official
Blog


Related articles: