通过Maven compiler Plugin选项可以设置javac的编译选项
Maven Compiler Plugin
The Compiler Plugin is used to compile the sources of your project. The default compiler is javac and is used to compile Java sources. Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set 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#.
This Compiler Plugin corresponds to Maven 1.x's Java Plugin.
Maven 编译插件是用于编译项目源程序。缺省的编译器是javac来编译java文件。注意当前的插件缺省的source和target都是1.5.
- 将程序按java1.6编译
参见链接http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
Setting the -source and -target of the Java Compiler
如下设置,就可以将程序编译为1.6的
注意只要修改maven-compiler-plugin的选项,
如果要指定源码的编码方式在configuration中增加<encoding>UTF-8</encoding>就变为UTF-8编码方式。
同理,如果要设置其他参数,可参见JAVAC的编译选项http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html
<project> [...] <build> [...] <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> [...] </build> [...] </project>
<target name="build" depends="init"> <!--编译 --> <javac srcdir="${src.dir}" destdir="${build.classes}" source="${javac.source}" target="${javac.jvm-target}" debuglevel="${debuglevel}" includeJavaRuntime="yes" includeAntRuntime="no" listfiles="yes" encoding="${encoding}" debug="${javac.debug}" deprecation="${javac.deprecation}" optimize="${javac.optimize}"> <classpath refid="classpath" /> <compilerarg value="-Xlint:unchecked"/> </javac> </target>
常见遇到的maven问题
当使用maven dependency:tree命令时:出现错误,搜索时,有如下答案(颁布不兼容性):
org.apache.maven.plugins | maven-shade-plugin | shade | 2.1 |
|
org.apache.maven.plugins | maven-dependency-plugin | tree | 2.8 |