NyxCode
NyxCode

Reputation: 728

Lombok and AspectJ

I'm trying to use Lombok in combination with AspectJ and Maven. So, what's the problem? When I use the AspectJ Maven Plugin (www.mojohaus.org/aspectj-maven-plugin/), it takes the sources and compiles them and ignores changes made by Lombok. I followed this tutorial and came up with this code and AspectJ works, but Lombok dies with this message:

[WARNING] You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.
Your processor is: org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl
Lombok supports: sun/apple javac 1.6, ECJ

So, does anyone know how to get Lombok in combination with AspectJ working?

Upvotes: 36

Views: 24287

Answers (8)

SoilChang
SoilChang

Reputation: 351

Use ajc to process classes.

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>

                <configuration>
                    <complianceLevel>8</complianceLevel>
                    <source>8</source>
                    <target>8</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <verbose>true</verbose>
                    <Xlint>ignore</Xlint>
                    <encoding>UTF-8</encoding>


                    <!-- IMPORTANT-->
                    <excludes>
                        <exclude>**/*.java</exclude>
                    </excludes>
                    <forceAjcCompile>true</forceAjcCompile>
                    <sources/>
                    <!-- IMPORTANT-->


                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>you.own.aspect.libary</groupId>
                            <artifactId>your-library</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>

                </configuration>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>process-classes</phase>
                        <goals>
                            <!-- use this goal to weave all your main classes -->
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <weaveDirectories>
                                <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                            </weaveDirectories>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>process-test-classes</phase>
                        <goals>
                            <!-- use this goal to weave all your test classes -->
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <weaveDirectories>
                                <weaveDirectory>${project.build.directory}/test-classes</weaveDirectory>
                            </weaveDirectories>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Upvotes: 33

Krzysztof
Krzysztof

Reputation: 2072

SpringBoot AND Java: 16

At this moment (19-11-2022) aspectJ plugin not support 17

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/>
    </parent>
   
   ...
   YOUR GROUP, ARTIFACT NAME, VERSION ETC HERE
   ...

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <scope>compile</scope>
        </dependency>
      
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>

        <dependency>
            <groupId>kkl.lib.dev.tools</groupId>
            <artifactId>lib-dev-tools</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>
                <configuration>
                    <showWeaveInfo/>
                    <sources/>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                    </weaveDirectories>
                    <forceAjcCompile>true</forceAjcCompile>
                    <source>16</source>
                    <target>16</target>
                    <proc>none</proc>
                    <complianceLevel>16</complianceLevel>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        
    </build>

Upvotes: 0

Sushmitha
Sushmitha

Reputation: 1

skip the aspectj. Only this solution worked for me apart from the other solutions mentioned in the stack overflow. Following is the sample configuration(not whole configuration).

<plugin>
    <groupId>dev.aspectj</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <configuration>   
        **<skip>true</skip>**
    </configuration>
</plugin>

Upvotes: 0

MashiMaro
MashiMaro

Reputation: 69

After reseach and testing all day, here is my success build.

Main idea is using javac to compile code first (compliance with lombok) and after that use aspectj only for weaving class.

<build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>unwovenClassesFolder</id>
                        <phase>generate-resources</phase>
                        <configuration>
                            <tasks>
                                <delete dir="${project.build.directory}/unwoven-classes"/>
                                <mkdir dir="${project.build.directory}/unwoven-classes"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <executions>
                    <execution>
                        <!-- Modifying output directory of default compile because non-weaved classes must be stored
                             in separate folder to not confuse ajc by reweaving already woven classes (which leads to
                             to ajc error message like "bad weaverState.Kind: -115") -->
                        <id>default-compile</id>
                        <configuration>
                            <source>16</source>
                            <target>16</target>
                            <outputDirectory>${project.build.directory}/unwoven-classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>

                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>

                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <verbose>true</verbose>
                    <Xlint>ignore</Xlint>
                    <encoding>UTF-8</encoding>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.directory}/unwoven-classes</weaveDirectory>
                    </weaveDirectories>
                    <!-- IMPORTANT-->
                    <excludes>
                        <exclude>**/*.java</exclude>
                    </excludes>
                    <forceAjcCompile>true</forceAjcCompile>
                    <sources/>
                    <!-- IMPORTANT-->
                    <complianceLevel>16</complianceLevel>
                    <source>16</source>
                    <target>16</target>
                </configuration>
                <executions>
                    <execution>
                        <!-- Compile and weave aspects after all classes compiled by javac -->
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <complianceLevel>16</complianceLevel>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

Upvotes: 1

Vivek Sharma
Vivek Sharma

Reputation: 41

I had a configuration/excludes/exclude section with the spring-boot-maven-plugin where the "aspectjweaver" dependency had been declared. The exclude section had "org.projectlombok" in it, and looks like that's why none of my lombok annotations were being processed while building using "mvn clean install"

I initially had this:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <excludes> <!------- THIS IS WHERE my problem started by exluding lombok -->
            <exclude>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </exclude>
        </excludes>
    </configuration>
</plugin>

When I removed the excludes part, then the build started taking the lombok annotations and worked. This is my section now:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
</plugin>

Upvotes: 0

Ivan
Ivan

Reputation: 9

This works for me with command line mvn clean install, but in Eclipse IDE, the problem is not solved, eg. log is not correctly recognized for @Slf4j.

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <verbose>true</verbose>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.7</source>
                <target>1.7</target>
                <complianceLevel>1.7</complianceLevel>
                <!-- <encoding>UTF-8</encoding> -->
                <verbose>false</verbose>
                <Xlint>ignore</Xlint>
                <outxml>true</outxml>
                <forceAjcCompile>true</forceAjcCompile>
                <reweavable>false</reweavable>
                <aspectLibraries>
                    <aspectLibrary> 
                        <groupId>com.aspectj.library.yours</groupId>
                        <artifactId>your-library</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <!-- this is important: start-->
                <sources/>
                <weaveDirectories>
                    <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                </weaveDirectories>
                <!-- this is important: end-->
            </configuration>
            <executions>
                <execution>
                    <!-- The right phase is very important! Compile and weave aspects after all classes compiled by javac -->
                    <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>1.8.9</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.8.9</version>
                </dependency>
            </dependencies>
        </plugin>

Upvotes: 0

ernitingoel
ernitingoel

Reputation: 661

I tried various solutions, finally specifying the javac compiler option like the below one worked enter image description here

Upvotes: 3

Alok P
Alok P

Reputation: 1079

Use delombok to generate normal source code. And then proceed as you would if Lombok were not being used.

Store your Lombok-annotated code in main/src/lombok (for example) and then have the delombok plugin convert these annotations into normal code and into the directory /delomboked (for example).

Upvotes: 4

Related Questions