Compilation error - Groovy and Lombok
Asked Answered
M

2

7

Here is my Maven command

mvn clean compile test-compile test

for this project

but I am facing with

[ERROR] no more tokens - could not parse error message: Groovy:unable to resolve class Delegate , unable to find class for annotation [ERROR] 12. ERROR in D:\Projects\lombok-groovy-example-master\src\main\groovy\prystasj\lombok\example\groovy\Rocket.groovy (at line 5) [ERROR] @Data

mvn --version

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T22:39:06+03:00)

java -version

java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

Code from repository

<properties>
    <groovy.version>2.0.5</groovy.version>
    <java.version>1.6</java.version>
    <lombok.version>0.11.4</lombok.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
    <compilerId>groovy-eclipse-compiler</compilerId>
    <fork>true</fork>
    <verbose>false</verbose>
    <source>${java.version}</source>
    <target>${java.version}</target>
    <encoding>${project.build.sourceEncoding}</encoding>
    <compilerArguments>
    <javaAgentClass>lombok.core.Agent</javaAgentClass>
    </compilerArguments>
    </configuration>
    <dependencies>
    <dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-eclipse-compiler</artifactId>
    <version>2.7.0-01</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>${lombok.version}</version>
    </dependency>//...

Class (file on git differs!)

@Data
public class Rocket {
}
Muzz answered 16/9, 2017 at 18:30 Comment(0)
W
16

You shouldn't use Lombok for Groovy, it is intended to be used only with Java.

Groovy has built-in annotation @Canonical which does what you want:

  • it creates useful equals, hashCode and toString methods
  • it creates no-arg and tuple constructor

So in your case use:

@Canonical
public class Rocket {}

Additionally you don't need to create getters and setters for fields in Groovy. If you add any field to your class, Groovy would create getters and setters.

Welby answered 16/9, 2017 at 20:26 Comment(11)
Is this an accepted answer? I am astonished. Groovy hails to - from their documentation - "seamlessly integrate with all existing Java classes and libraries" I want to use Java and Groovy interchangeably.... Using Groovy basically als glue code for existing Java Libraries .... so this is for me not an acceptable answerAbradant
Lombok is "hack" which uses the annotation processor to create missing methods in Java classes. It's not Java language standard feature. Groovy provides a similar feature using @Canonical, which also generate code on the compilation. Both mechanisms don't work together. I don't understand why you're angry on me :)Macassar
I just know a ) that Lombok is widely used in the Java landscape b) i don't have the choice to write everything in Groovy and i don't really think that was the intention of Groovy - to substitute Java - but to augment Java. c ) Annotation Preprocessors are part the Java Language d) If it's a hack i don't dare to judge, i simply know that in other Java Libraries Annotation Processors are also used. For me it is also not clear if your are speaking officially for Groovy or if this just reflects your personal experience. If first it would be helpful if you point to documentation.Abradant
Lombok requires special plugins/java agents to make IDEs to recognize code it generates, it's definitely a hack. Everyone on SO writes from its own experience. Remember, you can always provide your own answer in case you think this one is wrong/misleading/no longer relevant etc.Macassar
An opinion, your opinion and with out any argumentation and facts. So definitely not helpful. For the above mentioned reasons. In same way i could say Groovy is a “hack” ... emphasizis could, You can be sure that i don’t relay on this answer and am investigating what the problems are effectively.... there are with all jvm based languages interoperability issues , Kotlin, Scala , Groovy Jruby etc etc .... so there nothing new thereAbradant
And above all the answer doesn't even answer the question at hand, because ibasically the problem is simply that groovy doesn't support Lombok annotations. Consuming a Java Lombok annotated Class works fine , except if using the @Builder Annotations of Lombok. I still have to narrow that down.Abradant
See also my Answer here: #55304521Abradant
@chenrici, I find this answer helpful. I'm just starting to use Groovy and wanted to know if I should include Lombok, the answer is no. Thanks for asking the question, for your and other's comments and the answer.Gainsay
@chenrici, if you did get this working let us know how you did it.Gainsay
@Gainsay thanks. Unfortunately no , i didn't. See : github.com/chhex/groovylombok In away a little sad, specially since Annotation Pre Processor's are more and more used in a productive way (Lombok, Mapstruct etc). If you find a way, i d also love to hear about itAbradant
@chenrici, I do use Lombok in my Java code (who doesn't, :-)), but haven't (yet) tried to import/use existing code with Groovy to see if the problem happens. I'll keep you posted if I doGainsay
G
0

I was also having issue on using Lombok with groovy. (It wasn't generating any code) So I just changed my model class to .java and did the lombok stuff on the java class. Since we can intermix java and groovy code without any issue, I'm now able to use it without any issue.

Gerard answered 27/9, 2023 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.