旧的非Maven项目转化为Maven项目,编译打包时会有较多Warning,大部分Warning并不影响
编译打包,可以忽略,但有一些代码会报“不兼容的类型:Object无法转换为T”,导致无法编译通过。
原因
报“不兼容的类型:Object无法转换为T”错误的原因是:Maven默认采用javac编译器进行代码编译,
而javac编译器对代码检查较严格。
解决办法
网上流行的解决方案是,
1、开发环境,用Eclipse的JDT编译,即Project –> Clean就会触发Eclipse编译,正常运行。
2、Maven编译打包,修改pom文件,指定maven-compiler-plugin的compilerId为eclipse,并使用plexus-compiler-eclipse包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<compilerArgument>-nowarn</compilerArgument>
<encoding>UTF-8</encoding>
<compilerId>eclipse</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</plugin>
经本人测试发现:mvn compile可正确编译,但其实所有报Warning“Type mismatch”(类型不匹配)的类都没有编译打包,项目启动时会报缺少类,这个办法不可行。
最终方案
花了一天时间查找解决办法,最终采用最简单直接的方法:修改所有报“Object 无法转化为T”的代码,编译打包通过。
心得
通过解决这个问题过程,最深的感觉是:有些时候,最简单的方法可能最奏效。