Intellij IDEA Module 的Language Level的问题

最近从github上fork了张开涛的Shiro代码,IDE是Intellij IDEA。发现无论是Project还是Module,默认的Language Level都是JDK 1.5,而且每次修改都仅限于当前有效,稍后又会变回JDK 1.5

 

搜索了一番,解决办法都是这pom.xml中指定compiler的版本,如下:

复制代码
<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>
复制代码

还有简洁版本:

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

 

至于原因,则是maven compiler plugin自身问题:

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,需要手动设置。

  当前(Version: 3.5.1),默认使用JDK 1.5解析和编译源码与运行Maven的JDK版本无关!!!如果想修改,见链接。

ps,链接中的内容如下:

复制代码
    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
        [...]
      </build>
      [...]
    </project>
复制代码

 

参考:

  http://stackoverflow.com/questions/27037657/stop-intellij-idea-to-switch-java-language-level-everytime-the-pom-is-reloaded

  http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

  http://docs.oracle.com/javase/8/docs/api/javax/tools/JavaCompiler.html

 

posted on   LarryZeal  阅读(20110)  评论(4编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示