`spring-boot-maven-plugin` 和 `maven-compiler-plugin` 有什么区别?

What's the difference between `spring-boot-maven-plugin` and `maven-compiler-plugin`?

我不知道 spring-boot-maven-pluginmaven-compiler-plugin 之间的区别。

   <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

这是否意味着 Spring Boot Maven Plugin 的功能包括 maven-compiler-plugin

我就用Spring Boot Maven Plugin就可以了,不需要加2个插件??

"Spring Boot Maven Plugin 在 Maven 中提供 Spring 引导支持,让您可以打包可执行 jar 或 war 存档和 运行 应用程序“就地”。" =12=]

"Maven Compiler Plugin 用于编译项目的源代码。"

maven-compiler-plugin 有两个目标。两者都已绑定到 Maven 生命周期中的适当阶段,因此会在各自的阶段自动执行。

compiler:compile绑定到编译阶段,用于编译主要源文件。 compiler:testCompile绑定测试编译阶段,用于编译测试源文件

了解更多关于 maven 构建生命周期的信息 - http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

maven-compiler-plugin 用法 -

  • 使用 -source 和 -target javac 选项编译源代码
  • 使用不同的编译源代码JDK
  • 使用内存分配增强编译源代码
  • 传递编译器参数

最常用于定义源版本和目标版本。 有时您可能希望将某个项目编译为与您当前使用的版本不同的版本。 javac 可以使用-source 和-target 接受这样的命令。 maven-compiler-plugin 也可以配置为在编译期间提供这些选项。

例如,如果你想使用Java 8语言特性(-source 1.8)并且还希望编译后的类兼容JVM 1.8(-target 1.8),你可以添加以下两个属性,它们是插件参数的默认 属性 名称:

<project>
  [...]
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  [...]
</project>

或者直接配置插件:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

从技术上讲,如果需要创建可执行 jar 以及确保源代码和目标代码具有特定的版本(通过包含 maven-compiler-plugin 来实现)。

在我的例子中,我没有组合使用,但是当我的 java 项目是一个需要 运行 作为微服务等的 spring 启动应用程序时,我们需要一个可执行 jar 作为构建输出,所以使用 spring boot maven 插件(仅)但我的另一个 java 项目由 spring bean 或组件组成,将用作 spring 在其他外部应用程序中启用了库,但不需要 运行 本身,但必须确保指定源和目标版本,然后正常 "mvn package" 生成的 jar 应该可以工作。对于那个 Maven 编译器插件(仅)应该可以完成这项工作。