如果以下配置在maven中重复

If the following configuration duplicate in maven

指定编译器版本时,我使用了下面两个

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
 </plugin>

我想知道这两个是否重复?

您可以使用属性变体或显式变体。我建议这样做。

<project..>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <build>
    <pluginManagement>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artfiactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
      </plugin>
    </pluginManagement>
  </build>
  ..
</project>

我通常鼓励用户拥有一个包含插件版本定义的公司 pom(在我们的例子中 pluginManagement 部分也包括其他插件)并且在您自己的 pom 中您可以使用属性来定义要为此构建的 source/target 如下所示:

<project..>

  <parent>
     <groupId>com.acme</groupId>
     <artfiactId>corporate-pom</artifactId>
     <version>3.0.2</version>
  </parent>      

  <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  </build>
  ..
</project>

以上情况并非 100% 正确,因为您可以在代码中使用 JDK 8 个部分(如果您使用 JDK 8 进行编译),如果您需要,这将失败运行 它在 JRE 7 上。要正确处理这个必须使用 maven-toolchains and animalsniffer-maven-plugin

如果您使用的是 JDK 9+,您应该像这样增强您的配置:

<project..>

  <properties>
    <maven.compiler.release>9</maven.compiler.release>
  </properties>

  <build>
    <pluginManagement>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artfiactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
      </plugin>
    </pluginManagement>
  </build>
  ..
</project>