记一次mvn compile error问题

背景:
我们的项目通过maven来管理,构建。在一次项目构建中出现如下的错误:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.192 s
[INFO] Finished at: 2015-12-31T16:53:26+08:00
[INFO] Final Memory: 16M/48M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xxx-web: Compilation failure
[ERROR] /c:/Users/a574539/Perforce/clu_HZ47P4498186_3323/xxx-web/2.4.0/src/main/java/com/xx/xx/xx/service/xxClientService.java:[1015,79]
 strings in switch are not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable strings in switch)
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException


分析:
很显然是说的这个问题:
strings in switch are not supported in -source 1.5

代码中出现了jdk1.5 支持不了的语法。导致编译不了,出错。

查看了代码有这么一段:
switch(String.valueOf(product.get(selectedColumn.get(i)))){
	case "EQ":
	primaryAssetClass = "Equity";
	break;
	case "FX":
	primaryAssetClass = "ForeignExchange";
	break;
	case "IR":
	primaryAssetClass = "InterestRate";
	break;
	case "COM":
	primaryAssetClass = "Commodity";
	break;
	default:
	primaryAssetClass = "";
	break;
}



那么为什么mvn compile 默认是用的jdk1.5的版本呢。答案在这里: http://maven.apache.org/plugins/maven-compiler-plugin

引用
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


默认在没有给pom配compiler-plugin的情况下,maven就用的是1.5版本的jdk,如果要改就要加maven-compiler-plugin,并且指定jdk为1.7 或更高,不管怎么样就要是支持String在switch里的语法就可以了。
加了如下代码,mvn compile就可以成功了。
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.1</version>
	<configuration>
	<source>1.7</source>
	<target>1.7</target>
	</configuration>
</plugin>


PS:
1。   如果你不知道mvn 在compile的时候,用了什么版本的jdk, 那么mvn提供了一个参数用以支持debug 模式。
命令如下:
mvn clean compile -X

可以看到debug 信息如下:
-g -nowarn -target 1.5 -source 1.5 -encoding UTF-8


你可以看到:
-g -nowarn -target 1.5 -source 1.5 -encoding UTF-8

而如果你指定了jdk1.7 之后,就变成下面这样子了。
 -g -nowarn -target 1.7 -source 1.7 -encoding UTF-8


2。mvn命令行的一些参数可以参考: http://jan.baresovi.cz/dr/en/maven-command-line

--EOF--

猜你喜欢

转载自xfxlch.iteye.com/blog/2267922