分享

【Maven冷知识】Compiler插件

 算法与编程之美 2020-08-08

很多同学在pom的配置中都喜欢加上这样一段配置信息:

从配置信息上看,这是maven对Java源代码进行的编译配置,采用了Java 7 进行编译,但是为什么要加上这段配置呢?不加有没有什么影响?很多同学并不能准确回答这些问题,其本质在于一知半解没有深入的理解这些配置信息的作用。

以下信息 来自于Apache Maven的官方信息:

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

Also note that at present the default source setting is 1.6 and the default target setting is 1.6, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.

Other compilers than javac can be used and work has already started on AspectJ, .NET, and C#.

主要讲述的有以下三点:

(1)Compiler Plugin插件是用来编译项目源代码的,自从Maven 3.0版本之后,默认的编译Java源代码的编译器是javax.tools.JavaCompiler(JDK默认的Java编译器是javac),如果你想使用默认的javac编译器,可以在插件中进行配置:

这个配置一般情况下没有必要修改,除非项目有特殊要求。

(2)目前使用的默认的source和target都是1.6版本,而最新的JDK版本为11,因此强烈建议开发者在实际开发中修改,可以通过下面两种可选方式进行配置修改:

第一种方式是:

第二种方式是:

这两种方式是二选一的,并不是全部都要加上,而很多同学在开发中,都会全部加上。

(3)除了使用javac编译器以外,还可以使用AspectJ、.Net、C#等,这意味者今后maven不仅仅可以编译java工程项目,还可以编译微软的C#之类的项目,功能将会变得更加强大,应用范围更广。

4. 结语


参考文献 

[1] Welcome to Apache Maven.[EB/OL]. http://maven./

[2]Java SE Downloads [EB/OL].https://www.oracle.com/technetwork/java/javase/downloads/index.html

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约