maven 插件maven-compiler-plugin

http://maven.apache.org/components/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html
插件在编译阶段使用,有如下作用

1.指定编译版本

具体文件用具体版本,如module-info.java用java9编译,其他用1.5

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <configuration>
              <!-- compile everything to ensure module-info contains right entries -->
              <!-- required when JAVA_HOME is JDK 8 or below -->
              <jdkToolchain>
                <version>9</version>
              </jdkToolchain>
              <release>9</release>
            </configuration>
          </execution>
          <execution>
            <id>base-compile</id>
            <goals>
              <goal>compile</goal>
            </goals>
            <!-- recompile everything for target VM except the module-info.java-->
            <configuration>
              <excludes>
                <exclude>module-info.java</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
        <!-- defaults for compile and testCompile -->
        <configuration>
          <!-- jdkToolchain required when JAVA_HOME is JDK 9 or above -->
          <jdkToolchain>
            <version>[1.5,9)</version>
          </jdkToolchain>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

2。使用编译的和源文件的jdk版本输入输出一样

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

3.编译的时候还可以强制指定参数

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <fork>true</fork>
          <meminitial>128m</meminitial>
          <maxmem>512m</maxmem>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>
4.还可以传递参数给javac
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <compilerArgs>
            <arg>-verbose</arg>
            <arg>-Xlint:all,-options,-path</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>
    原文作者:lmem
    原文地址: https://www.jianshu.com/p/b6b81e64670e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞