IntelliJ是否不使用Maven的来设置目标字节码级别?

huangapple go评论77阅读模式
英文:

Does IntelliJ not utilize Maven's <release> for target byte code level?

问题

我刚刚发现,当仅配置maven-compiler-plugin的&lt;release&gt;时,IntelliJ的行为非常奇怪:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.0</version>
  <configuration>
    <release>14</release>
  </configuration>
</plugin>

结果在重新加载Maven项目后,在“Java编译器”首选项中,它显示目标字节码级别为8,而不是预期的14。我猜测这是因为模块没有使用任何Java 14特性而被推断出来的。

请参见首选项截图:https://i.stack.imgur.com/gqpKV.jpg

相比之下,当添加&lt;source&gt;&lt;target&gt;时,IDE会正确配置自己的配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.0</version>
  <configuration>
    <source>14</source>
    <target>14</target>
  </configuration>
</plugin>

请参见首选项截图:https://i.stack.imgur.com/gqpKV.jpg

就我理解,javac的--release选项略微更可靠,因为它还将-bootclasspath选项设置为正确的级别(与this SO post中的内容进行了双重检查)。因此,我认为仅依赖这个选项是更好的做法。

是否还需要执行其他配置步骤,以便让IntelliJ正确设置首选项,而不声明较旧的参数?

英文:

I just found out, that IntelliJ behaves very peculiarly when configuring the maven-compiler-plugin with &lt;release&gt; only:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-xml -->

&lt;plugin&gt;
  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  &lt;version&gt;3.8.0&lt;/version&gt;
  &lt;configuration&gt;
    &lt;release&gt;14&lt;/release&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;

<!-- end snippet -->

As a result after reloading the Maven project, in the 'Java Compiler' preferences, it displays target bytecode level 8 instead of the expected 14. I guess it was inferred because the modules didn't use any Java 14 features, yet.

see preferences screenshot: https://i.stack.imgur.com/gqpKV.jpg

In comparison, when adding &lt;source&gt; and &lt;target&gt; the IDE configures its own configuration, correctly.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-xml -->

&lt;plugin&gt;
  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  &lt;version&gt;3.8.0&lt;/version&gt;
  &lt;configuration&gt;
    &lt;source&gt;14&lt;/source&gt;
    &lt;target&gt;14&lt;/target&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;

<!-- end snippet -->

see preferences screenshot: https://i.stack.imgur.com/gqpKV.jpg

As far as I understand it, the javac --release option is slightly more reliable than just defining -source and -target because it additionally sets the -bootclasspath option to the correct level (double-checked with this SO post). So it seems to me that relying on this option exclusively is a better practice.

Is there another configuration step that I would have to execute in order to make IntelliJ set up the preferences, correctly, without declaring the older parameters?

答案1

得分: 1

我对这个问题进行了更详细的调查,并发现在某个父pom中,maven-compiler-plugin被添加到了<pluginManagement>中,但是却从未为配置设置过一个maven属性。

尽管我在子pom中覆盖了插件的配置,但这似乎导致IntelliJ仍按语言级别推断。

我想这是可以接受的行为,因为如果父pom有问题,IntelliJ不应过于努力地自行解决问题,以免造成更多混淆。

英文:

I investigated that issue a little closer and found out that somewhere in one of the parent poms the maven-compiler-plugin was added to the &lt;pluginManagement&gt; with a maven-property that got never set for the configuration.

This seemed to have caused IntelliJ to infer by language level even though I overwrote the plugin configuration in the child-pom.

I guess that's acceptable behaviour because if the parent pom is faulty, IntelliJ shouldn't try too hard to figure out stuff on its own to just cause more confusion.

huangapple
  • 本文由 发表于 2020年9月15日 19:08:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63900571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定