【问题标题】:Removing warning messages on IntelliJ IDEA while building Java project在构建 Java 项目时删除 IntelliJ IDEA 上的警告消息
【发布时间】:2015-08-21 19:21:52
【问题描述】:

我是第一次使用 IntelliJ IDEA Community Edition,使用 Maven 搭建 TDD 环境。下面提供了我正在尝试测试的代码以及我遇到的警告消息以及项目结构。

项目结构:

代码:

package miscellaneous;

import org.junit.Test;

import static org.junit.Assert.*;

public class TestHello {

    // Methods to be tested.....
    private int Add1Plus1(int i, int j) {
        return (i + j);
    }

    @Test
    public void testAdd1Plus1() throws Exception {
        assertEquals(2, Add1Plus1(1, 1));
    }
}

配置详情:

  • Java 编译器: 1.8.0_45
  • Maven 版本: 3.0.5
  • Maven 用户设置文件的路径: /home/sandeep/Desktop/MyDocs/repos/git-repos/public/MavenCodeBase/settings.xml
  • Maven 本地存储库的路径: /home/sandeep/Desktop/MyDocs/repos/maven-repos
  • pom.xml: http://pastebin.com/462Uytad

警告信息:

Warning:java: source value 1.5 is obsolete and will be removed in a future release
Warning:java: target value 1.5 is obsolete and will be removed in a future release
Warning:java: To suppress warnings about obsolete options, use -Xlint:-options.

问题:

是什么导致了这些消息?修复这些警告消息的好方法/推荐方法是什么?

【问题讨论】:

  • 你的 pom 没有显示任何编译插件的配置,尝试在你的 pom.xml 中设置 -source and -target

标签: java intellij-idea


【解决方案1】:

检查你的 pom.xml 中的 java 版本(here 你可以找到如何去做)。 还要检查 Project Structure 中的 java 版本。最后你可以做的 - 检查编译器版本,例如

【讨论】:

  • 好的。所以,如果我理解正确的话,File -> Project Structure --> Project(Setting Project SDK to above 1.5)File -> Settings -> Build, Execution, Deployment, Compiler -> Java Compiler 和将目标字节码版本设置为 1.5 以上是有区别的。
  • 是的,上面列出的所有java sdk设置必须相同!
  • 它们必须始终相同,但不能位于同一个地方,这是我最不喜欢的 IntelliJ 功能之一。
  • 在创建新的 IntelliJ IDEA 项目时,某处是否有配置设置,总是会使其设置为合理的值(即根据所选的 JDK)?
【解决方案2】:

我做了上述所有操作,但仍然有一个警告实例:

Warning:java: source value 1.5 is obsolete and will be removed in a future release

我进入了我的 project_name.iml 文件并替换了以下标签:

<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">

与:

<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">

瞧,没有更多的错误消息。希望这对某人有所帮助。

【讨论】:

  • 谢谢。遇到了同样的问题。这解决了它!
  • 也为我工作。也需要在Answer by Schermann 加上这个答案中进行更改。
  • 我的 .iml 文件没有。它看起来像这样:
  • @emirhosseini 这取决于您如何设置项目。您的 .iml 文件中可能没有该标签。如果没有看到上面的建议。 :) 祝你好运
【解决方案3】:

我在 Java 的 Gradle 项目中遇到过这个问题。

在 build.gradle 文件中,存在未使用分配的警告。我删除了 build.gradle 文件中的sourceCompatibility = 1.5 行,所有警告消息都消失了。

【讨论】:

    【解决方案4】:

    如果项目使用 Maven,请检查 pom.xml 文件的源和目标:

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.6.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>${project.build.sourceEncoding}</encoding>
          </configuration>
        </plugin>
      </plugins>
    </build>
    

    如果是 Gradle - 检查 build.gradle 以获取:

    plugins {
        id 'java'
    }
    sourceCompatibility = 1.8
    

    【讨论】:

      【解决方案5】:

      就我而言,上述解决方案均无效。但是改变项目结构中的语言水平确实如此。

      文件 -> 项目结构 -> 项目设置 -> 模块 -> 在“源”选项卡下将语言级别更改为更高版本。

      【讨论】:

        【解决方案6】:

        如果您有 Maven 项目,请打开 pom.xml 文件并在项目根目录中添加以下代码:

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

        【讨论】:

          【解决方案7】:

          真正的原因是,你的模块目标 jdk 版本与 IDE 不一样。 解决这个问题的一个方法:Preferences->Build,Execution,Deployment->Compiler->Java Compiler->Javac Options: uncheck Use compiler from module target JDK when possible 另外,其他人对 pom 的回答也是正确的。

          <?xml version="1.0" encoding="UTF-8"?>
          <project>
              <properties>
                  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                  <maven.compiler.source>1.8</maven.compiler.source>
                  <maven.compiler.target>1.8</maven.compiler.target>
              </properties>
          </project>
          

          <project>
          ...
              <build>
                  <plugins>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-compiler-plugin</artifactId>
                          <version>3.6.1</version>
                          <configuration>
                              <source>1.8</source>
                              <target>1.8</target>
                          </configuration>
                      </plugin>
                  </plugins>
              </build>
          </project>
          

          【讨论】:

            猜你喜欢
            • 2011-08-08
            • 1970-01-01
            • 2023-03-17
            • 1970-01-01
            • 2018-05-27
            • 2014-07-12
            • 2020-02-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多