Skip to content
Martin Ledvinka edited this page May 27, 2024 · 5 revisions

Note: This applies to JOPA prior to version 2.0.0. Since this version, JOPA works with Lombok just fine.

Lombok is a library for generating boilerplate code like getters/setters, equals/hashCode, toString, constructors and other commonly defined methods. This makes it quite useful for making object model classes more succinct. For example, a JOPA entity using Lombok to generate getters, setters, the default no-arg constructor, equals/hashCode and toString may look something like this:

@Data
@OWLClass(iri = Vocabulary.Student)
public class Student implements Serializable {

    @Id
    private URI uri;

    @ParticipationConstraints(nonEmpty = true)
    @OWLDataProperty(iri = Vocabulary.p_key, simpleLiteral = true)
    private String key;

    @ParticipationConstraints(nonEmpty = true)
    @OWLDataProperty(iri = Vocabulary.p_firstName)
    private String firstName;

    @ParticipationConstraints(nonEmpty = true)
    @OWLDataProperty(iri = Vocabulary.p_lastName)
    private String lastName;

    @OWLDataProperty(iri = Vocabulary.p_emailAddress)
    private String email;
}

However, Lombok is an annotation processor, and it has been quite problematic to get it working with AspectJ weaving required by JOPA.

Nevertheless, a configuration supporting both these tools at the same time is possible (at least with Maven and in IntelliJ IDEA).

Lombok + AspectJ in Maven

Besides the usual Lombok dependency, the AspectJ maven plugin needs to be configured to handle the Lomboked source files. The following POM snippet shows configuration of the AspectJ maven plugin based on this comment on the corresponding Lombok issue.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.14.0</version>
    <configuration>
        <complianceLevel>${jdk.version}</complianceLevel>
        <source>${jdk.version}</source>
        <target>${jdk.version}</target>
        <!-- These are added for integration with Lombok -->
        <forceAjcCompile>true</forceAjcCompile>
        <sources/>
        <weaveDirectories>
            <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
        </weaveDirectories>
        <!-- These were added for integration with Lombok -->
        <aspectLibraries>
            <aspectLibrary>
                <groupId>cz.cvut.kbss.jopa</groupId>
                <artifactId>jopa-impl</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The compiler plugin should also be configured to run the Lombok annotation processor. With this configuration, entity classes processed by Lombok are then weaved by AspectJ compiler.

Full setup can be found in the Example04 pom.xml in the JOPA examples repository. This example project demonstrates the ability to use Lombok-based JOPA entity classes both in application and test code.

Lombok + AspectJ in IntelliJ IDEA

Using Lombok and AspectJ in Maven is all good, but what one usually wants is being able to run code (or tests) from the IDE. IntelliJ IDEA supports both Lombok and AspectJ, to make them work both together it is necessary to:

  1. Enable annotation processing (to let Lombok process the source code)
  2. Use Ajc as the Java compiler
  3. IMPORTANT: Enable Post-compile weave mode on module level. Note that as per the documentation, your project should not contain .aj files. For JOPA, this is no problem, since its aspects are written as Java classes.