java Lombok岛和AspectJ

doinxwow  于 2022-11-20  发布在  Java
关注(0)|答案(6)|浏览(200)

我试图将Lombok与AspectJ和Maven结合使用。那么,问题是什么呢?当我使用AspectJ Maven插件(www.mojohaus.org/aspectj-maven-plugin/)时,它获取源代码并编译它们,忽略Lombok所做的更改。我遵循this tutorial,得出了this code和AspectJ,但Lombok却带着以下消息死去:

[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

那么,有人知道如何让Lombok与AspectJ结合使用吗?
[EDIT]它工作了!现在,当我把项目打包到一个大jar里的时候,它似乎工作了。但是它仍然不工作于maven:test和IntelliJ。如果有人能修复这个问题,我会很高兴的。
顺祝商祺!

qnzebej0

qnzebej01#

使用ajc处理类。

<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>
kx7yvsdv

kx7yvsdv2#

使用delombok生成正常的源代码。然后像你没有使用Lombok一样继续。
例如,将带Lombok注解的代码存储在main/src/lombok中,然后让delombok插件将这些注解转换为普通代码并放入目录/delomboked中。

ct3nt3jp

ct3nt3jp3#

我尝试了各种解决方案,最后指定了javac编译器选项,如下面的

ffscu2ro

ffscu2ro4#

这对我的命令行mvn clean install有效,但是在Eclipse IDE中,这个问题没有得到解决,例如,log没有被正确识别为@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>
mfuanj7w

mfuanj7w5#

我有一个configuration/excludes/excludes节,其中声明了“aspectjweaverdependencyexcludes节中有“org.projectlombok”,看起来这就是为什么在使用“mvn clean install”构建时没有处理我的lombok注解的原因。
我最初有这样的想法:

<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>

当我删除了excludes部分后,构建就开始接受lombok注解并开始工作了。

<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>
v9tzhpje

v9tzhpje6#

SpringBoot和Java:16

目前(19-11-2022)aspectJ插件不支持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>

相关问题