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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30690295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:00:40  来源:igfitidea点击:

Removing warning messages on IntelliJ IDEA while building Java project

javaintellij-idea

提问by Sandeep Chatterjee

I am using IntelliJ IDEA Community Edition for the first time and using Maven to set up a TDD environment. The code that I am trying to test and the warning messages I had encountered along with the project structure are provided below.

我是第一次使用 IntelliJ IDEA 社区版,并使用 Maven 来设置 TDD 环境。下面提供了我尝试测试的代码以及我遇到的警告消息以及项目结构。

Project Structure:

项目结构:

enter image description here

在此处输入图片说明

Code:

代码:

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));
    }
}

Configuration details:

配置详情:

  • Java compiler:1.8.0_45
  • Maven Version:3.0.5
  • Path to Maven user-settings file:/home/sandeep/Desktop/MyDocs/repos/git-repos/public/MavenCodeBase/settings.xml
  • Path to Maven local repository:/home/sandeep/Desktop/MyDocs/repos/maven-repos
  • pom.xml:http://pastebin.com/462Uytad
  • 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 messages:

警告信息:

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.

Question:

题:

What is causing these messages and what would be a good/recommended way to fix these warning messages?

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

采纳答案by James Schermann

Check java version in your pom.xml(hereyou can find how to do it). Also check java version in Project Structure. And the last what you can do - check compiler version e.g.

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

enter image description here

在此处输入图片说明

回答by Brod

I did all of the above and still had one instance of the warning:

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

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

I went into my project_name.imlfile and replaced the following tag:

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

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

with:

和:

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

And voila, no more error message. Hope this helps someone.

瞧,没有更多的错误信息。希望这可以帮助某人。

回答by Yogesh Umesh Vaity

I had this issue in Gradle project of Java.

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

In build.gradle file, there was a warning that the assignment was not used. I removed the sourceCompatibility = 1.5line in build.gradle file and all the warning messages disappeared.

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

value 1.5 is obsolete and will be removed in a future release

value 1.5 is obsolete and will be removed in a future release

回答by Eduard Streltsov

If the project with Maven, check pom.xmlfile for source and target:

如果项目带有 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>

And in case of Gradle - check build.gradlefor:

在 Gradle 的情况下 - 检查build.gradle

plugins {
    id 'java'
}
sourseCompatibility = 1.8

回答by DarkShadow

In my case none of the above solution worked. But changing language level in project structure did.

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

File -> Project Structure -> Project Settings -> Modules -> under "sources" tab change language level to some upper version.

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

enter image description here

enter image description here

回答by Suban Dhyako

If you have Maven project, open pom.xmlfile and add following code inside your project root:

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

回答by Tom Kruise

The real reason is that, your module target jdk version is not same with the IDE. One move to solve this problem: Preferences->Build,Execution,Deployment->Compiler->Java Compiler->Javac Options: uncheck Use compiler from module target JDK when possibleAlso, others answer about the pom is correct too.

真正的原因是,您的模块目标 jdk 版本与 IDE 不同。解决这个问题的一个步骤:Preferences->Build,Execution,Deployment->Compiler->Java Compiler->Javac Options: uncheck Use compiler from module target JDK as 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>

OR

或者

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