首页 > 解决方案 > Apache Maven:-source 1.5 中不支持 Diamond 运算符

问题描述

我使用以下命令构建并运行我的 Java Apache Camel 代码

mvn clean compile camel:run

这个命令一直对我有用,但是当我有使用菱形运算符的代码时,我得到一个错误。

我的钻石运营商:

private List<RestPropertyDefinition> restProperties = new ArrayList<>();

我的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project camel-rest-example: Compilation failure
[ERROR] /home/sindre/IdeaProjects/training/camelrestexample/src/main/java/com/test/EmployeeRoute.java:[21,73] diamond operator is not supported in -source 1.5
[ERROR]   (use -source 7 or higher to enable diamond operator)

我的 POM 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>camel-rest-example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-spring -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>spi-annotations</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-quartz</artifactId>
            <version>2.20.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <!-- Allows the routes to be run via 'mvn camel:run' -->
            <plugin>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-maven-plugin</artifactId>
                <version>2.20.0</version>
            </plugin>
        </plugins>
    </build>
</project>

我在带有 IntelliJ IDEA 的 Ubunut 18.04 上使用 Java 8。看起来 IntelliJ 认为我使用的是 Java 5?但我的机器上只安装了 Java 8

标签: javamavenapache-camel

解决方案


编译器似乎默认假定源代码级别为 1.5。

使用 pom 告诉编译器这样的显式版本:

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

推荐阅读