Maven:错误:-source 1.3不支持泛型,我使用的是1.6

Son*_*ana 30 maven

我已将现有的Maven项目导入Eclipse IDE.我修改了一些代码,它编译成功,我使用Java 1.6作为编译器,当我尝试运行maven clean install -X

它给出了以下错误

无法解析错误消息:(使用-source 5或更高版本启用泛型)D:\ bayer\util\src\main\java\com\tata\bayer\util\BrokerageCalendar.java:179:错误:不支持泛型in -source 1.3

   private static Hashtable<String, Boolean> nyseHolidays = new Hashtable<String, Boolean>();
                           ^

could not parse error message:   (use -source 5 or higher to enable generics)
D:\bayer\util\src\main\java\com\tata\bayer\util\APIHttpXmlClient.java:27: error: generics are not supported in -source 1.3
                        Class<? extends APIResponse> responseClass) {
                         ^
Run Code Online (Sandbox Code Playgroud)

请提出任何想法,如何解决这个问题?

Shi*_*gon 42

您是否声明要在项目pom.xml中使用java 1.6?

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerArgument></compilerArgument>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
Run Code Online (Sandbox Code Playgroud)


the*_*bar 26

配置Maven编译器插件将解决问题.事实证明问题是由Ubuntu存储库中的Maven3包引起的.另一种解决方法是从Apache网站下载Maven 3,它使用更新的Compiler插件.

我想知道为什么当文档声明默认Java源是1.5时发生这种情况.要查看mvn用于编译器插件的内容,请使用:

mvn help:effective-pom
Run Code Online (Sandbox Code Playgroud)

我的Maven编译器插件是2.0.2,即使我使用的是Ubuntu软件包中的Maven 3.0.4.当我使用Apache的Maven 3.0.4运行相同的命令时,我有一个插件版本2.3.2,默认为Java 1.5,如预期的那样.


khm*_*ise 5

您必须配置Maven编译器插件.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.3</version>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

  • (顺便说一句,在示例版本中有一个拼写错误,但你不能在stackoverflow上进行1个字符的编辑:) (2认同)