Intellij IDEA Module 的Language Level的問題

最近從github上fork了張開濤的Shiro代碼,IDE是Intellij IDEA。發現不管是Project仍是Module,默認的Language Level都是JDK 1.5,並且每次修改都僅限於當前有效,稍後又會變回JDK 1.5html

 

搜索了一番,解決辦法都是這pom.xml中指定compiler的版本,以下:java

<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>

還有簡潔版本:git

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

 

至於緣由,則是maven compiler plugin自身問題:github

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.

上面這一段的內容是:apache

  該插件從3.0版本開始,默認編譯器是javax.tools.JavaCompiler (前提是JDK 1.6之後);若是想使用javac,須要手動設置。api

  當前(Version: 3.5.1),默認使用JDK 1.5解析和編譯源碼與運行Maven的JDK版本無關!!!若是想修改,見連接。intellij-idea

ps,連接中的內容以下:oracle

    <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>

 

參考:maven

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

  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

相關文章
相關標籤/搜索