简体   繁体   中英

No warning for unused import

I am building a gradle java project using sublime, instead of an IDE like eclipse. I have notice when I run the build task, I do not get any warnings about unused imports.

Is there a compiler option in java that can turn on my strict warnings? If so, how do I enable that in my build.gradle?

Thanks.

Warning on unused imports is not something the Java compiler currently handles: https://docs.oracle.com/en/java/javase/14/docs/specs/man/javac.html#examples-of-using--xlint-keys

You can use an external code style tool to take care of that for you. For example, with Spotless

plugins {
    `java`
    id("com.diffplug.gradle.spotless") version "4.0.0"
}

spotless {
    java {
        eclipse()
        removeUnusedImports()
        trimTrailingWhitespace()
        endWithNewline()
    }
}

and the following Java source with unused List import:

import java.util.List;

public class Example {

    public static void main(String[] args) {
        System.out.println("hello");
    }
}

when I try to build the project, I will immediately get an error:

> Task :build FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':buildSrc:spotlessJava'.
> The following files had format violations:
      src\main\java\io\mateo\gradle\build\Example.java
          @@ -1,7 +1,5 @@
           package╖io.mateo.gradle.build;

          -import╖java.util.List;
          -
           public╖class╖Example╖{

           \tpublic╖static╖void╖main(String[]╖args)╖{
  Run 'gradlew spotlessApply' to fix these violations.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM