• idea的language level含义和module的language level自动跳到5


    一、idea中项目的language level的含义

    language level指的是编译项目代码所用的jdk版本。
    那么,从这个定义出发会有两个小问题。

    第一,如果project sdk是jdk8,那么language level应该是多少呢?可以选择的值是8、7、6、。。。1,取哪个值表示编译代码的时候使用哪个版本的java编译器,虽然project sdk是8,但是sdk8是可以编译出jdk7兼容的字节码的。就是说高版本的sdk可以编译出低版本的jvm的字节码。

    第二、这里的language level可千万不能设置的高于8,比如设置11、17等,那都是不行的,因为sdk的版本限制了它的最高编译版本。

    二、module的language level自动跳动5

    原因:
    这个问题是由于maven-compiler-plugin这个maven插件导致的。

    Apache Maven Compiler Plugin
    
    The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.
    
    Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.
    

    该插件从3.0版本开始,默认编译器是javax.tools.JavaCompiler (前提是JDK 1.6以后);如果想使用javac,需要手动设置。
    当前的maven版本(Version: 3.5.1),默认使用JDK 1.5解析和编译源码,与运行Maven的JDK版本无关!!!如果想修改,见链接(setting the -source and -target of the Java Compiler

    (
    javac和javax.tools.JavaCompiler的区别是什么呢?
    javac (作为java编译器)是一个可执行文件,理论上甚至可以是依赖于平台的可执行文件或脚本。这被称为将 .java 编译为 .class 。

    在Windows上它的名字是 javac.exe ,它通常在 C以下的地方:\程序文件* \ jdk * \ \ _ \ n 。

    此编译器也是用java开发的。这意味着,如果我们启动此.exe,则需要启动一个新的Java虚拟机来运行它。这很慢。

    但是,因为它是用Java编写的,所以有一个更快的替代方案:从我们已经运行的jvm,我们只需 import 它的主类(fe javax.tools.JavaCompiler 等)并调用它。这不需要启动不需要的jvm。这就是maven的作用。仅仅10年就足以让他们正确地做到这一点。 : - )

    当然它也有一些后退。最可能的原因是,在内部编译器的情况下,它需要从与maven核心相同的jvm和相同的命名空间中运行。同样指定备用jvm是不可能的,并且可能还存在一些由命名空间冲突导致的副作用。但它们非常不可能,因为它们都是精心设计的软件。
    )

    查看全文

    解决办法:
    1、项目的pom文件中增加以下plugin

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

    2、项目pom中增加以下properties

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

    上面的两个办法,在每个项目中都要手动设置一下,比较麻烦。下面的第三个办法是一个一劳永逸的办法。

    3、一劳永逸的办法
    File->setting->Editor->File and Code Templates->Other->Maven-> Maven Project.xml
    在${END}下面(这个自己的情况而定)添加java编译器版本等内容,然后点击Apply,点击OK,结束。

    这里添加后,再新建项目后,项目的pom文件中自动加上了:

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

    其实根本上和上面两种是一样的,只不过这第三种方法会自动加上properties,前面两种方法要研发人员每次新建一个项目后手动添加,不过研发不易,能省点力气就省点力气吧,毕竟研发不是人生的唯一事项,我们还要省出精力生活呢,是吧?

  • 相关阅读:
    SE78添加图片,比如smartforms用的…
    Smartforms
    将文件上传到FTP服务器
    【转自ITPUB】SYNONYM关于underlying table权限的小小发现
    alter session set current_schema=Schema
    exportDISPLAY=:0.0的解释
    如何通过使用Xmanager的图形化界面修改系统
    Xms Xmx PermSize MaxPermSize 区别
    oracle中schema指的是什么?
    oracle表空间查询
  • 原文地址:https://www.cnblogs.com/zhangzl419/p/16220504.html
Copyright © 2020-2023  润新知