解決:Java source1.5不支持diamond運算符,請使用source 7或更高版本以啓用diamond運算符

Maven默認用的是JDK1.5去編譯java

diamond運算符,指的是JDK1.7的一個新特性apache

List<String> list = new ArrayList<String>(); // 老版本寫法List<String> list = new ArrayList<>(); // JDK1.7及之後的寫法maven

因此Maven默認使用JDK1.5去編譯是不認識這個東西的,針對這種問題,在網上找了三種解決方案:ide

Ⅰ :在項目pom.xml中加入下面的配置便可ui

<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target></properties>idea

Ⅱ:直接在模塊pom.xml中配置Maven的編譯插件也是能夠的,像下面這樣:插件

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

  

 

Ⅲ:另外還有一種最終的解決方案,適用於idea下配置Maven的全部項目:code

在配置的maven安裝包的setting.xml中的profiles標籤中加入如下標籤xml

<profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>blog

這樣以後就不會出現每次新建立的maven項目默認JDK版本都是1.5版本的了。

 

備註:

第二種解決方案親測有效。有的人電腦沒有這段代碼後會打包失敗,有的電腦卻不會,暫時不知什麼緣由。