这篇文章讲述了用pom.xml向项目添加maven编译器插件依赖。
Maven是一个开源的apache工具,用于自动完成基于Java的项目的构建过程。
maven-compiler-plugin是一个基本的插件,每个开发者都用它来编译maven项目的源代码。默认情况下,该插件没有配置。compile 目标将作为maven生命周期过程的一部分被调用,无需在pom.xml中定义。
如果你想做一些配置,请在pom.xml中添加该插件。
JDK提供javac工具来编译源代码。这个插件内部调用jdk javac tool来编译代码。
如何改变pom.xml中的编译器选项
在pom.xml ,在plugin 标签下,有一个configuration 标签,它可以用来配置不同的配置选项。
<project>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<!-- put your configurations here -->
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
插件帮助
下面的maven命令提供了关于编译器插件的有用信息
mvn compiler:help -Ddetail=true
编译器目标的使用
有两个目标可用。
compile 目标:执行compiler:compile选项,在构建过程中作为编译阶段的一部分编译java文件。
mvn compile
test-compile目标:在测试过程中,执行compiler:testCompile选项,作为测试编译阶段的一部分,编译测试java文件。
mvn test-compile
插件配置选项
有很多的配置,我们可以改变/覆盖默认值。我们可以在标签中添加不同的选项。
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>// add Configuration options here</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
插件配置使用实例
我们将看到不同的配置例子
如何为插件配置不同的java路径和编译器版本?
根据你的java设置,改变executable 和compilerVersion 标签中的configuration 选项。
请看下面的例子以了解更多信息。
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_HOME}/bin/javac</executable>
<compilerVersion>1.8</compilerVersion>
</configuration>
在插件中配置java编译器版本
JDKjavac 工具有source 和target 选项,可以在编译过程中设置JDK版本。
这个插件还提供了源和目标选项的配置。利用这些选项,我们可以用pom.xml 中的新版本改变默认的编译器版本。
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
向maven编译器传递参数
有时,我们需要向编译阶段传递与内存有关的配置和垃圾配置。该插件提供了CompilerArgs 标签来实现这一功能。
<compilerArgs>
<arg>-verbose</arg>
<arg>-Xlint:all,-options,-path</arg>
</compilerArgs>
在编译器中启用源文件的编码。
在编译过程中,如果没有设置编码,我们会得到警告。
我们可以在编译器级别或项目级别进行。
Compiler encoding configuration :
<configuration>
<encoding>UTF-8</encoding>
</configuration>
maven project encoding: 这是根级别的配置,影响到项目的所有模块
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
禁用这个编译器插件。
默认情况下,Compiler 插件运行时不需要在pom.xml中配置它。
请在pom.xml中修改configuration 来禁用它。
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
如何改变编译用的源文件夹/目标文件夹的自定义目录
默认情况下,src/main/java 是 java 文件的位置。输入的文件可以用下面的选项进行覆盖
<configuration>
<include>src/javafolder</include>
</configuration>
默认情况下,编译的源文件,即类文件将被写入target/test/classes 。
要用target/myclasses 覆盖目标文件夹,我们可以使用以下配置。
<build>
<outputDirectory>${project.build.directory}/myclasses</outputDirectory>
</build>
打印maven编译器插件的依赖信息
我们可以通过两种方式实现。
使用树状命令:
mvn dependency:tree -Dverbose
使用配置中的verbose选项 :默认情况下,verbose 是false ,将其改为true 以获得该插件的详细信息。
<configuration>
<verbose>true</verbose>
</configuration>
错误
让我们回顾一下在使用maven编译器插件时遇到的一些错误。
发生错误/警告时继续编译
failOnError 和 failOnWarning
选项允许编译器继续编译。
一旦编译构建过程完成,就会显示错误:
javac Task: source release 8 requires target release 1.8
这是在应用程序构建过程中运行maven项目的常见错误。
这基本上是不匹配的java版本不兼容。
如果没有定义maven-compiler-plugin ,请在pom.xml 中添加插件,并通过以下代码更改插件属性。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
这包含源码和目标码是有效的java版本。
您也可以通过项目属性标签更改
- maven.compiler.source
- maven.compiler.source
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
将编译器插件的编码改为UTF-8
在插件配置中,encoding 属性被设置为true。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
总结
您学会了如何在java应用程序中添加maven编译器插件 如何在编译器选项中改变java版本,改变编码,禁用插件,编译和测试-编译目标的使用实例