IntelliJ IDEA 中 maven 项目的不同源和 testSource JDK
Different source and testSource JDKs for a maven project in IntelliJ IDEA
我在 IDEA 中导入了一个 maven 项目。有一个子模块对常规源和测试源使用不同版本的 java - 它对常规源使用 java 1.4,对测试源使用 java 7,如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
<testSource>1.7</testSource>
<testTarget>1.7</testTarget>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
项目在maven中编译正常,在IDEA中编译失败。当我打开 Project Structure -> module settings 时,我看到导入的模块的语言级别为 1.4。因此,测试 类 的编译失败,因为它们使用了 1.4 中不可用的功能。
作为解决方法,我手动更改 pom.xml 中的 java 版本。有没有不用修改maven配置的方法?
编辑:我按照评论里的建议,在IDEA里面的Project structure里面手动修改了设置,但是在编译工程的时候出现如下错误:
Information:3/20/2015 5:29 PM - Compilation completed with 1 error and 0 warnings in 5 sec
Error:java: javacTask: source release 1.7 requires target release 1.7
遗憾的是,Idea 尚不支持源代码和测试的不同语言级别。 2012 年在 JetBrains 问题跟踪器中发现了相应的错误:https://youtrack.jetbrains.com/issue/IDEA-85478
作为解决方法,我每次将我的 Maven 项目导入 Idea 时(即在我对 pom.xml 进行任何更改之后)执行以下操作:
- 转到
Project Structure
(Ctrl+Alt+Shift+S),打开模块并将语言级别设置为 1.7
- 打开
Idea settings
(Ctrl+Alt+S),搜索Java compiler
并将项目的目标字节码版本更改为1.7
缺点是 Idea 不知道您的非测试源应该针对 1.4 编译,并且会建议您仅在 Java 1.5+
中进行更改
另一种解决方案是将测试提取到单独的 Maven 模块中(它将作为单独的 Idea 模块导入到 Idea 中)并为其设置不同的语言级别,即仅针对包含的模块将 testSource lvel 指定为 1.7你的测试
我在 IDEA 中导入了一个 maven 项目。有一个子模块对常规源和测试源使用不同版本的 java - 它对常规源使用 java 1.4,对测试源使用 java 7,如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
<testSource>1.7</testSource>
<testTarget>1.7</testTarget>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
项目在maven中编译正常,在IDEA中编译失败。当我打开 Project Structure -> module settings 时,我看到导入的模块的语言级别为 1.4。因此,测试 类 的编译失败,因为它们使用了 1.4 中不可用的功能。
作为解决方法,我手动更改 pom.xml 中的 java 版本。有没有不用修改maven配置的方法?
编辑:我按照评论里的建议,在IDEA里面的Project structure里面手动修改了设置,但是在编译工程的时候出现如下错误:
Information:3/20/2015 5:29 PM - Compilation completed with 1 error and 0 warnings in 5 sec
Error:java: javacTask: source release 1.7 requires target release 1.7
遗憾的是,Idea 尚不支持源代码和测试的不同语言级别。 2012 年在 JetBrains 问题跟踪器中发现了相应的错误:https://youtrack.jetbrains.com/issue/IDEA-85478
作为解决方法,我每次将我的 Maven 项目导入 Idea 时(即在我对 pom.xml 进行任何更改之后)执行以下操作:
- 转到
Project Structure
(Ctrl+Alt+Shift+S),打开模块并将语言级别设置为 1.7 - 打开
Idea settings
(Ctrl+Alt+S),搜索Java compiler
并将项目的目标字节码版本更改为1.7
缺点是 Idea 不知道您的非测试源应该针对 1.4 编译,并且会建议您仅在 Java 1.5+
中进行更改另一种解决方案是将测试提取到单独的 Maven 模块中(它将作为单独的 Idea 模块导入到 Idea 中)并为其设置不同的语言级别,即仅针对包含的模块将 testSource lvel 指定为 1.7你的测试