在构建Java项目时删除IntelliJ IDEA上的警告消息


72

我第一次使用IntelliJ IDEA社区版,并使用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.

题:

是什么导致这些消息,以及解决这些警告消息的好/推荐方法是什么?


2
您的pom没有显示编译器插件的任何配置,请尝试在pom.xml中设置-source和-target
A4L 2015年

Answers:


91

pom.xml中检查Java版本(在这里您可以找到如何做)。还要在Project Structure中检查Java版本。最后是您可以做的-检查编译器版本,例如

在此处输入图片说明


4
好。因此,如果我正确理解,则将File -> Project Structure --> Project(Setting Project SDK to above 1.5)File -> Settings -> Build, Execution, Deployment, Compiler -> Java Compiler和将目标字节码版本设置为1.5以上之间存在差异。
Sandeep Chatterjee 2015年

是的,上面列出的所有Java sdk设置必须相同!
James Schermann

5
它们必须始终相同,但不能位于同一位置,这是IntelliJ我最不喜欢的功能之一。
理查德·拉斯特

在某个地方是否有配置设置,在创建新的IntelliJ IDEA项目时,该配置设置将始终使其设置为合理的值(即根据所选的JDK)?
史蒂夫·投手

70

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

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

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


1
谢谢。有同样的问题。这样解决了!

1
也为我工作。还需要在Schermann答案以及此处的答案中进行更改。
罗勒·布尔克

我的.iml文件没有该文件。相反,它看起来像这样:<?xml version =“ 1.0” encoding =“ UTF-8”?> <module type =“ JAVA_MODULE” version =“ 4” />
emirhosseini

@emirhosseini这取决于您如何设置项目。您的.iml文件中可能没有该标签。如果没有看到上面的建议。:)祝你好运
布罗德(Brod)

18

如果项目使用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

10

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

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

在此处输入图片说明


9

如果您有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>

2

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

在build.gradle文件中,有一条警告,指出未使用分配。我删除了sourceCompatibility = 1.5build.gradle文件中的这一行,所有警告消息均消失了。

值1.5已过时,将在以后的版本中删除


0

真正的原因是,您的模块目标jdk版本与IDE不同。解决此问题的一种方法是:Preferences-> Build,Execution,Deployment-> Compiler-> Java Compiler-> Javac选项:取消选中在可能的情况下从模块目标JDK使用编译器 另外,关于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>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.