Welcome to my website, have a nice day!
Dream it, Do it, Make it!

Intellij IDEA Modules 的Language Level的Java代码编译问题三种解决方式

从git远程仓库clone下代码,用Intellij IDEA打开之后修改File->Project Settings->Modules->Language LevelFile->Setting->Build,Execution,Deployment->Compiler->Java Compiler->Target bytecode version的版本为1.8,但是关闭idea之后或者重新编译之后,版本又会重新变成1.5。

idea-java-compiler.jpg

这种情况启动项目的时候还会报错:

Error:(234, 61) java: -source 1.5 中不支持 diamond 运算符
  (请使用 -source 7 或更高版本以启用 diamond 运算符)

具体原因是maven默认的编译版本的问题,可以通过修改maven编译的jdk版本来解决。

解决方式1:

在pom.xml文件中增加如下配置:

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

解决方式2:

在pom.xml文件的build配置中增加如下内容:

<build>
    <plugins>
        <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>
    </plugins>
</build>

解决方式3:

修改${MAVEN_HOME}/conf/setting.xml配置,或者你有指定用户自定义的setting.xml文件也可以,具体参见「Maven如何配置自己的私有远程仓库」,这里我修改的是我自己的用户配置C:\Users\michael\.m2\setting.xml,具体修改内容:

1)增加一个profile,如果已经增加了,也可以在原来的profile里添加如下内容:

<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <jdk>1.7</jdk>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <maven.compiler.source>1.7</maven.compiler.source>
      <maven.compiler.target>1.7</maven.compiler.target>
      <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
    </properties>
  </profile>
</profiles>

2)激活该profile

 <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>

这样工程就没有问题了。

赞(0)
未经允许禁止转载:Ddmit » Intellij IDEA Modules 的Language Level的Java代码编译问题三种解决方式

评论 抢沙发

登录

找回密码

注册