如何在 Spring 应用程序中使用 Maven 使用最新的 Java 版本?

How to use latest Java version using Maven in Spring application?

我正在开发基于 Spring MVC mavenized 的应用程序。该应用程序使用的是 Java 1.6,尽管我在我的机器上只安装了 Java 8。我们如何使 Maven 配置使用最新的 java 版本?

pom.xml中定义了这个maven.compiler.source属性来告诉Maven使用Java 8编译项目。

<properties>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
</properties>

另一种方法-2

Compiler Plugin
Alternative, configure the plugin directly.

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

这会有所帮助,在我的情况下它起作用了。除了添加 <java.version> 属性 我还 Eclipse->Properties->Project Facets->Convert to faceted form(如果未转换)然后在下拉列表中将 java 版本更改为 1.8。在 Java 构建路径设置中将 Workspace 默认 JRE 更改为 Jdk 1.8。

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <start-class>com.Whosebug.MyClass</start-class>
    </properties> 

同时在 Eclipse Window->Preferences->Java->Compiler 中设置编译器设置以使用 JDK 1.8.