代码之家  ›  专栏  ›  技术社区  ›  StormeHawke

IntelliJ Idea Maven插件在配置为1.8(Mac)时尝试在Java 1.7中编译

  •  2
  • StormeHawke  · 技术社区  · 12 年前

    我正在尝试对一个示例项目运行一个maven目标,同时启动一个新项目。我对Mac和IntelliJ都是新手(在评估时,我正在与Eclipse Luna一起运行社区版)。这一切都在Eclipse和终端中工作。

    我遇到的问题是让Maven在Java8中运行它。我已将JAVA_HOME配置为1.8,将项目JDK设置为1.8,语言级别设置为1.8等。代码编译(使用lambdas),因此IntelliJ使用的是1.8。但Maven插件似乎不想遵守这一设置。

    以下是maven目标配置(使用Deploy目标): 工作目录:/Users/brian.trezise/IideaProjects/exampleservice 命令行:clean install tomcat7:run

    当我从Maven项目弹出窗口执行目标时,它执行的是:

    /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/bin/java -Dmaven.home=/usr/local/Cellar/maven/3.2.2/libexec -Dclassworlds.conf=/usr/local/Cellar/maven/3.2.2/libexec/bin/m2.conf -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 13 CE.app/bin" -Dfile.encoding=UTF-8 -classpath "/usr/local/Cellar/maven/3.2.2/libexec/boot/plexus-classworlds-2.5.1.jar:/Applications/IntelliJ IDEA 13 CE.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=13.1.4 clean install tomcat7:run
    

    在Maven尝试编译(下载所有依赖项等)之前,它似乎运行良好,然后我得到以下错误:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project inin-example-app: Compilation failure
    [ERROR] /Users/brian.trezise/IdeaProjects/exampleservice/src/main/java/com/inin/example/controller/PingController.java:[53,49] lambda expressions are not supported in -source 1.7
    [ERROR] (use -source 8 or higher to enable lambda expressions)
    

    我不确定如何将其配置为使用“-source 8”(如果我将其添加到命令行中,则会失败)。

    有人愿意同情IntelliJ新手吗?

    2 回复  |  直到 12 年前
        1
  •  4
  •   Ross Taylor-Turner Rakesh Vanol    12 年前

    看起来您可能缺少Maven构建中的编译器插件,因此默认为1.7,请尝试将以下内容添加到pom.xml中 build 部分

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
    
        2
  •  0
  •   Community Mohan Dere    9 年前

    我尝试对Intellij IDEA进行如下更改:

    1.

    File >> Settings >> Build, Execution, Deployment >> Compiler >> Java Compiler >> project bytecode version: 1.8 >> Per-module bytecode version: 1.8
    

    2.

    File >> Project Structure >> Project Settings >> Project >> SDK : 1.8, Project Language : 8 - Lambdas
    File >> Project Structure >> Project Settings >> Modules >> abc : Language level: 8 - Lambdas
    

    但没有任何效果,我一保存它,它就将版本还原为java1.5。

    然而,在根(项目级)pom.xml中添加以下行使我解决了上述问题:(这两个选项都适用于我)

    选项1:

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

    选项2:

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

    最初发布于: IntelliJ IDEA 13 uses Java 1.5 despite setting to 1.7