9

我想在我的 IntelliJ Idea 中启用 AspectJ 编译器,因为我有几个方面想要在编译时进行编织。

同时,我在我的代码库中使用了 Lombok 和 Mapstruct。

这两个需要额外的注释处理,这必须在ajc开始之前进行。我为 Lombok 和 Mapstruct 安装了两个插件。它们独立工作正常,正在生成源代码。但是当我启用ajc并打勾Enable annotation processing options,然后构建一个项目时,我得到:

Error:(9, 0) ajc: Internal error in the mapping processor: java.lang.NullPointerException   at org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding.sourceField(FieldBinding.java:425)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.determineSourceStart(TypeElementImpl.java:108)      at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.getSourceStart(TypeElementImpl.java:72)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.compare(TypeElementImpl.java:65)    at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.compare(TypeElementImpl.java:1)     at java.util.TimSort.countRunAndMakeAscending(TimSort.java:360)     at java.util.TimSort.sort(TimSort.java:234)     at java.util.Arrays.sort(Arrays.java:1512)      at java.util.ArrayList.sort(ArrayList.java:1462)    at java.util.Collections.sort(Collections.java:175)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl.getEnclosedElements(TypeElementImpl.java:166)    at org.mapstruct.ap.internal.util.workarounds.SpecificCompilerWorkarounds.replaceTypeElementIfNecessary(SpecificCompilerWorkarounds.java:99)    at org.mapstruct.ap.internal.util.Executables.getAllEnclosedExecutableElements(Executables.java:99)     at org.mapstruct.ap.internal.model.common.Type.getAllMethods(Type.java:633)     at org.mapstruct.ap.internal.model.common.Type.getPropertyReadAccessors(Type.java:496)      at org.mapstruct.ap.internal.model.BeanMappingMethod$Builder.build(BeanMappingMethod.java:168)      at org.mapstruct.ap.internal.processor.MapperCreationProcessor.getMappingMethods(MapperCreationProcessor.java:376)      at org.mapstruct.ap.internal.processor.MapperCreationProcessor.getMapper(MapperCreationProcessor.java:151)      at org.mapstruct.ap.internal.processor.MapperCreationProcessor.process(MapperCreationProcessor.java:122)    at org.mapstruct.ap.internal.processor.MapperCreationProcessor.process(MapperCreationProcessor.java:76)     at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:283)     at org.mapstruct.ap.MappingProcessor.processMapperTypeElement(MappingProcessor.java:263)    at org.mapstruct.ap.MappingProcessor.processMapperElements(MappingProcessor.java:221)   at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:156)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:139)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatcher.java:121)   at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:159)    at org.aspectj.org.eclipse.jdt.internal.compiler.Compiler.processAnnotations(Compiler.java:931)     at org.aspectj.org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:437)    at org.aspectj.org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:417)    at org.aspectj.ajdt.internal.core.builder.AjBuildManager.performCompilation(AjBuildManager.java:1036)   at org.aspectj.ajdt.internal.core.builder.AjBuildManager.performBuild(AjBuildManager.java:272)      at org.aspectj.ajdt.internal.core.builder.AjBuildManager.batchBuild(AjBuildManager.java:185)    at com.intellij.lang.aspectj.build.AjJpsCompiler.doBuild(AjJpsCompiler.java:249)    at com.intellij.lang.aspectj.build.AjJpsCompiler.build(AjJpsCompiler.java:119)      at com.intellij.lang.aspectj.build.AjTranslatingBuilder.doBuild(AjTranslatingBuilder.java:114)      at com.intellij.lang.aspectj.build.AjBuilderBase.build(AjBuilderBase.java:74)   at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1328)      at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:1008)     at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:1075)   at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:969)     at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:798)      at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:380)     at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:178)    at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:139)     at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:302)   at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:135)    at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler.lambda$channelRead0$0(BuildMain.java:228)   at org.jetbrains.jps.service.impl.SharedThreadPoolImpl.lambda$executeOnPooledThread$0(SharedThreadPoolImpl.java:42)     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)      at java.util.concurrent.FutureTask.run(FutureTask.java:266)     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)      at java.lang.Thread.run(Thread.java:748)  

此外,我看到了一堆其他ajc编译错误,因为它无法找到应该由 Lombok 生成的方法。

我该如何解决这些问题?

4

3 回答 3

4

嗨,我有类似的问题。

修复是让 aspectj-maven-plugin 仅在编译类后才开始编织。

它在这里更详细地描述了https://www.mojohaus.org/aspectj-maven-plugin/examples/weaveDirectories.html

我需要:

(1) 指定已编译的类将通过的文件夹<weaveDirectories >

(2) 将 maven-compiler-plugin 生命周期的阶段更改为运行“进程类”或更高版本。

这就是我的 pom 的样子。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${org.projectlombok.version}</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>-Amapstruct.defaultComponentModel="spring"</compilerArg>
                    <compilerArg>-Amapstruct.unmappedTargetPolicy=ERROR</compilerArg>
                    <compilerArg>-Amapstruct.suppressGeneratorTimestamp=true</compilerArg>
                    <compilerArg>-Amapstruct.suppressGeneratorVersionInfoComment=true</compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <showWeaveInfo>${showWeaveInfo}</showWeaveInfo>
                <showWeaveInfo>true</showWeaveInfo>
                <Xlint>ignore</Xlint>
                <complianceLevel>${java.compiler.version}</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>false</verbose>
                <verbose>true</verbose>
                <forceAjcCompile>true</forceAjcCompile>
                <sources/>
                <weaveDirectories>
                    <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                </weaveDirectories>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.lib.test</groupId>
                        <artifactId>test1</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>com.lib.test2</groupId>
                        <artifactId>test2</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <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>
        </plugin>
    </plugins>
</build>
于 2019-10-10T06:58:59.637 回答
4

Post-compile weave mode在 IntelliJ 中启用为我解决了这个问题( https://www.jetbrains.com/help/idea/using-the-aspectj-ajc-compiler.html)。

于 2021-02-02T08:13:42.063 回答
0

如果您有相同项目的方面来源,那么您应该在@jun-huh 答案中添加一些行代码并删除<aspectLibraries>。以下pom.xml内容适用于您在同一个项目中使用方面的情况。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>az.sanco</groupId>
    <artifactId>aspect</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>
                <configuration>
                    <forceAjcCompile>true</forceAjcCompile>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                    </weaveDirectories>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                    <proc>none</proc><!-- annotation process already finished, so we don't need to run again-->
                </configuration>
                <executions>
                    <execution>
                        <id>copy-only-aspect-files</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <!--when `javac` compile works it does not copy *.aj files to the 
                            output directory. We need to run `ajc` compile during `compile` phase and 
                            copy only *.aj files to weave our classes-->
                            <includes>
                                <include>**/*.aj</include><!--IMPORTANT-->
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
于 2021-12-26T20:54:43.903 回答