pom.xml是否需要指定Maven Surefire Plugin?

Is it required to specify Maven Surefire Plugin in pom.xml?

为什么这个 pom.xml 可以 运行 mvn test 没有指定的 maven surefire 插件?

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lester</groupId>
    <artifactId>junit-basics</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>junit-basics</name>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.jupiter.version>5.4.0</junit.jupiter.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

插件放在pom.xml和不放有什么区别?

如果您没有在 pom 中指定 maven-surefire-plugin,您的构建将采用 Maven 中指定的版本,请参阅 https://maven.apache.org/ref/3.6.3/maven-core/default-bindings.html 这些版本有一段时间没有更新,以确保 Maven 的升级不会因为不同的插件版本而影响您的构建。 这些版本很可能会随着 Maven 的下一个主要版本进行更新。所以这一切都取决于你的项目的生命周期,以及它是否是一个私人项目。如果它有很长的生命周期,最好将这些插件锁定到特定版本,这样你就不会依赖任何人用来构建项目的 Maven 版本。