记一次mvn compile error问题

[b]背景:[/b]
我们的项目通过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


[b]分析:[/b]
很显然是说的这个问题:

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的版本呢。答案在这里:[url]http://maven.apache.org/plugins/maven-compiler-plugin[/url]

[quote]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[/quote]

默认在没有给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命令行的一些参数可以参考:[url]http://jan.baresovi.cz/dr/en/maven-command-line[/url]

--EOF--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值