Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle multi-project does not generate Lombok's goodness

I have a multi-project in Gradle. The build.gradle script looks like:

buildscript {    
  repositories {
    jcenter()
    mavenCentral()
    maven { url "https://plugins.gradle.org/m2/" }
  }

  dependencies {
    classpath "com.github.jengelman.gradle.plugins:shadow:2.0.4"
    classpath "io.franzbecker:gradle-lombok:1.14"
  }
}

allprojects {
  //apply plugin: "base"
}

subprojects {
  apply plugin: "com.github.johnrengelman.plugin-shadow"
  apply plugin: "idea"
  apply plugin: "java"
  apply plugin: "io.franzbecker.gradle-lombok"

  group = "io.shido"
  version = "0.1.0-SNAPSHOT"

  sourceCompatibility = JavaVersion.VERSION_1_8
  targetCompatibility = JavaVersion.VERSION_1_8

  repositories {
    jcenter()
    mavenCentral()
  }

  dependencies {
    // [start] Research
    //compileOnly "org.projectlombok:lombok:1.18.2"
    // [end] Research

    testCompile "nl.jqno.equalsverifier:equalsverifier:2.4.5"
    testCompile "org.junit.jupiter:junit-jupiter-api:$junit_version"

    testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"

    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_version"
  }

  //=================================================================================================
  //  P L U G I N S
  //=================================================================================================

  lombok {
    version = "1.18.2"
  }
  //=================================================================================================
  //  T A S K S
  //=================================================================================================

  // shadowJar { ... }

  test {
    useJUnitPlatform()
  }
}

I have a messages project then with this build.script:

plugins {
  id "java-library"
}

repositories {
  jcenter()
  mavenCentral()
}

...and a core project with this build.script:

plugins {
  id "io.spring.dependency-management" version "1.0.6.RELEASE"
}

dependencies {
  compile project(":messages")
}

All of that should be OK.

If I write a simple class in messages:

package io.shido.event;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@Getter
@Builder
@ToString
@EqualsAndHashCode(of = "name")
class Prototype {
  private String id;

  private String name;
}

...and then a unit test for the same:

package io.shido.event;

import org.junit.jupiter.api.Test;

final class PrototypeTest {
  @Test
  void instantiate() {
    final Prototype event = Prototype.???
  }
}

I'm expecting I can use a builder for that class right there, but there is nothing generated.

Am I missing something in the setup? Everything compiles, but I can't see anything being generated for Lombok. Not sure what else to try.

like image 324
x80486 Avatar asked Aug 21 '18 14:08

x80486


1 Answers

If you are using IDEA and recent version of Gradle (I think >= 4.7) you could use the following setup which is working fine in my different projects:

  1. install Lombok plugin for IDEA , from Settings->Plugins configuration panel.
  2. In your Gradle build script you can get rid of the lombok plugin declaration and lombok block : you just need to add the following dependencies on your project(s).

    ext{
        lombokVersion = '1.16.20'
        junitVersion = '4.12'
    }
    dependencies {
        compileOnly "org.projectlombok:lombok:${lombokVersion}"
        annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
        // other libs ...
    
        // test dependencies
        testCompile group: 'junit', name: 'junit', version: "${junitVersion}"
    }
    
  3. After project re-import, make sure to enable Annotation Processing in IDEA, from Settings -> Build,Execution,Deployment -> Compiler-> AnnotationProcessors menu : there is a checkbox "Enable annotation processing" which is disabled by default.

This should work fine, and you'll be able to use Lombok features in your main code and in unit test as well.

like image 93
M.Ricciuti Avatar answered Sep 22 '22 16:09

M.Ricciuti