设置JAVA编译程序级别,Maven编译插件(翻译)--(2)

本文介绍如何使用Maven编译插件配置Java源代码和目标版本,并提供了将项目编译为Java 1.6的具体配置示例。同时,还介绍了如何设置源码的编码方式及其他编译选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过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>
 
这个可以和ANT比较,还是不如ANT清晰,还是ANT适合编译
 
	<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>
<compilerArgument>-Xlint:unchecked</compilerArgument>
 

常见遇到的maven问题

当使用maven dependency:tree命令时:出现错误,搜索时,有如下答案(颁布不兼容性):

tree: org/sonatype/aether/version/VersionConstraint
https://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound
https://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound 写道

org.apache.maven.plugins

maven-shade-plugin

shade

Maven Shade Plugin

2.1

MSHADE-143

 

 

org.apache.maven.plugins

maven-dependency-plugin

tree

Maven Dependency Plugin

2.8

MDEP-407

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值