【问题标题】:Lombok and AspectJ龙目岛和 AspectJ
【发布时间】:2017-01-28 12:53:55
【问题描述】:

我正在尝试将 Lombok 与 AspectJ 和 Maven 结合使用。 所以有什么问题? 当我使用 AspectJ Maven 插件 (www.mojohaus.org/aspectj-maven-plugin/) 时,它会获取源代码并编译它们并忽略 Lombok 所做的更改。我关注了this tutorial 并提出了this code 和AspectJ 工作,但龙目岛死了这条消息:

[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 结合使用?

[编辑] 它有效!现在,当我将项目打包到一个胖 jar 时,它似乎可以工作。 但它仍然不适用于 maven:test 和 IntelliJ。如果有人能解决这个问题,我会很高兴。

最好的问候!

【问题讨论】:

标签: java maven aspectj lombok


【解决方案1】:

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

【讨论】:

  • 我正在使用 IntelliJ。实际上,我需要切换到 javac 才能通过 IntelliJ 完成这项工作。但它有效!
  • @JayCui 如果你切换回javac,我认为你不会在编织
  • @Ghilteras 我实际上找到了另一个解决方案,而无需切换到 javac。假设您也在使用 Intellij。转到文件 > 项目结构 > 模块 > AspectJ,选中选项:编译后编织模式。它应该可以正常工作。
  • 对于以后来这里的任何人来说,这似乎是正确的解决方案。只需查看“”部分和执行配置。如果您仅在测试编译中遇到问题,那是因为您必须为 test-compile 进行单独的配置,否则 aspectj 将编译您的测试代码而不是 maven-compiler,而 lombok 将无法使用这些代码。
  • 大多数人都知道${project.build.directory}target,但您可以使用${project.build.outputDirectory}${project.build.testOutputDirectory} 来表示target/classestarget/test-classes :)
【解决方案2】:

使用 delombok 生成正常的源代码。然后像不使用 Lombok 时一样继续。

将带有 Lombok 注释的代码存储在 main/src/lombok(例如)中,然后让 delombok 插件将这些注释转换为普通代码并进入目录 /delomboked(例如)。

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 如果你切换回javac,我认为你实际上并没有在编织
    • @Ghilteras 我实际上找到了另一个解决方案,而无需切换到 javac。假设您也在使用 Intellij。转到文件 > 项目结构 > 模块 > AspectJ,选中选项:编译后编织模式。它应该可以正常工作。
    【解决方案4】:

    这适用于我的命令行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>
    

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2015-04-04
      • 2014-05-21
      • 2021-03-27
      • 2015-10-18
      • 1970-01-01
      • 2016-06-29
      • 2016-10-08
      • 2011-06-19
      相关资源
      最近更新 更多