Gradle multi-project does not generate Lombok's goodness
Asked Answered
T

1

6

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.

Twayblade answered 21/8, 2018 at 14:41 Comment(2)
Have you tried setting sha256 = ""? I'm not sure that completely omitting it will skip the integrity check.Vitellus
Yes, but that didn't change the result. I also got rid completely of the lombok { ... } block, and now I'm getting by default org.projectlombok:lombok:1.16.20 on Provided scope. I'm fine with any version, as long as it works initially. I can tweak the versions later on ;)Twayblade
L
6

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.

Lianeliang answered 21/8, 2018 at 17:32 Comment(2)
For some reason it doesn't work as stated in the Lombok's guide, however it does work if I use your approach. I would really like to use the Gradle plugin (and I'll try later on), but ¯_(ツ)_/¯Twayblade
Gaaaaaa. the "disabled by default" was the answer for me. Than you M.Ric!Mulligan

© 2022 - 2024 — McMap. All rights reserved.