Ben
Ben

Reputation: 41

How to setup error prone with Gradle (getting various errors)

I am trying to setup error prone using Gradle, however everything I have tried gives me some kind of error:

To test, I have a simple hello world Java program using Gradle, which compiles fine without error prone. Then I read: https://github.com/tbroyer/gradle-errorprone-plugin found from the install guide and tried this in the build.gradle:

plugins {
    id 'java'
    id("net.ltgt.errorprone") version "2.3.3"
}

group 'test'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    errorprone("com.google.errorprone:error_prone_core:2.3.3")

}

This gave me this error:

Plugin [id: 'net.ltgt.errorprone', version: '2.3.3'] was not found in any of the following sources:

Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
Plugin Repositories (could not resolve plugin artifact 'net.ltgt.errorprone:net.ltgt.errorprone.gradle.plugin:2.3.3')
  Searched in the following repositories:
    Gradle Central Plugin Repository

I changed 2.3.3 to latest.version and got the same error.

Then I found this https://plugins.gradle.org/plugin/net.ltgt.errorprone and tried version number 0.8.1. I need to be able to use Java 11 so can't use Gradle 4.x as far as I understand.

Also if anybody knows of an actual repository that has used error prone with Gradle that I could look at, I would be very grateful :)

My Gradle wrapper properties is set to use Gradle version 5.4.1

Upvotes: 4

Views: 9386

Answers (1)

dkb
dkb

Reputation: 4596

Answer is for Java version > 8.
If you need setup for java-8 or older, please read this.

  1. Add Gradle plugin to build.gradle file

    plugins {
        id("net.ltgt.errorprone") version "4.0.1"
    }
    

    Release versions

  2. Add dependency to error_prone_core in build.gradle file

    dependencies {
        errorprone("com.google.errorprone:error_prone_core:2.28.0")
    }
    

    Releases

  3. Build project, this will add errorprone configuration namespace

  4. you can customize/enable/disable it by tasks

    tasks.withType(JavaCompile).configureEach {
        options.errorprone.disableWarningsInGeneratedCode = true
        options.errorprone.isEnabled = false // change it to true to enable
    }
    

Note: As mentioned in OP comment by @jb-nizet, gradle plugin and error_prone_core has different versions and independent release cycles.

Sample build.gradle file


plugins {
    id "java"
    id("net.ltgt.errorprone") version "4.0.1"
}

java.sourceCompatibility = JavaVersion.VERSION_21

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    errorprone("com.google.errorprone:error_prone_core:2.28.0")
}

tasks.withType(JavaCompile).configureEach {
    options.errorprone.disableWarningsInGeneratedCode = true
    options.errorprone.isEnabled = true // set it to false if you want to disable the plugin
}

To test plugin
Add following file in your src/main/java folder as mentioned in this page

import java.util.HashSet;
import java.util.Set;

public class ShortSet {
  public static void main (String[] args) {
    Set<Short> s = new HashSet<>();
    for (short i = 0; i < 100; i++) {
      s.add(i);
      s.remove(i - 1);
    }
    System.out.println(s.size());
  }
}

Execute ./gradlew clean build command on terminal and you may see following error

src/main/java/com/demo/ShortSet.java:11: error: [CollectionIncompatibleType] Argument 'i - 1' should not be passed to this method; its type int is not compatible with its collection's type argument Short
      s.remove(i - 1);
              ^
    (see https://errorprone.info/bugpattern/CollectionIncompatibleType)
1 error

FAILURE: Build failed with an exception.

Will this plugin work for kotlin project?

I am not able to make it work, if you know any good resource/documentations, would like to give it a try and update.

P.S. Above sample code would not even compile in kotlin, any need for kotlin?

Ref

Github-error-prone

Upvotes: 4

Related Questions