0

我在使用 Maven 和 AspectJ 编译项目时遇到了麻烦。

这是 POM 的摘录aspectj-maven-plugin(取自有效的 POM):

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>default-aj-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-core</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
        <execution>
            <id>default-aj-test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-core</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-test</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
        <execution>
            <id>default-aj-generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
        <execution>
            <id>default-aj-generate-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.8.5</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <configuration>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <forceAjcCompile>true</forceAjcCompile>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <aspectDirectory>src/main/aspects</aspectDirectory>
        <testAspectDirectory>src/test/aspects</testAspectDirectory>
    </configuration>
</plugin>

启动时,mvn clean deploy我得到了以下几行:

[INFO] --- aspectj-maven-plugin:1.7:test-compile (default-aj-generate-test-sources) @ jcz-persistence-jpa ---
[INFO] 'org.jcz.persistence.jpa.model.Band' (Band.java:45) is annotated with @Entity type annotation from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:79)
[INFO] 'org.jcz.persistence.jpa.model.Band' (Band.java:45) is annotated with @UUID type annotation from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:80)
[INFO] Extending interface set for type 'org.jcz.persistence.jpa.model.Band' (Band.java) to include 'org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID' (UUIDSupportAspect.aj)
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'boolean org.jcz.persistence.EntitySupport.equals(java.lang.Object)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.lang.String org.jcz.persistence.EntitySupport.toSuperString()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.lang.String org.jcz.persistence.EntitySupport.toString()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'org.jcz.persistence.UniqueIdentifier org.jcz.persistence.EntitySupport.uniqueIdentifier()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.util.Comparator<java.lang.reflect.Method> org.jcz.persistence.EntitySupport.retrieveComparatorForAction(java.lang.Class<? extends java.lang.annotation.Annotation>)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.lang.String org.jcz.persistence.EntitySupport.retrieveDescriptionForAction(java.lang.annotation.Annotation)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.invokeActions(java.lang.Class<? extends java.lang.annotation.Annotation>)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPrePersist()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPostPersist()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPreUpate()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPostUpdate()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPreRemove()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPostRemove()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped field from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'java.lang.String org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.id')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'java.io.Serializable org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.getId()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'void org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.setId(java.io.Serializable)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'void org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.ensureId()')

只是为了将范围缩小到一个案例:

[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'java.io.Serializable org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.getId()')

因此,看起来该方法getId()已正确注入到 classBand中。但是当 Maven 继续测试编译和单元测试时:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project jcz-persistence-jpa: Compilation failure: Compilation failure:

并且,在其他错误细节中,我得到了

[ERROR] /home/stefano/projects/java/jcz-persistence-jpa/src/test/java/org/jcz/persistence/jpa/ServiceTestCase.java:[182,45] cannot find symbol
[ERROR] symbol:   method getId()
[ERROR] location: variable persisted of type org.jcz.persistence.jpa.model.Band
[ERROR] /home/stefano/projects/java/jcz-persistence-jpa/src/test/java/org/jcz/persistence/jpa/ServiceTestCase.java:[182,60]

毕竟,好像我所有的 intertype 声明的东西都没有被注入。

为了检查代码中是否有错误,我尝试在 Eclipse 中运行 JUnit 测试,一切正常。

所以我猜maven编译过程中有些东西我没有正确设置。

谁能帮助我了解导致问题的原因?

4

2 回答 2

0

终于想出了一个解决办法。离开这里。

我为解决这个问题所做的配置是maven-compiler-plugin这样的:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
        <source>1.${java.version}</source>
        <target>1.${java.version}</target>
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>

最重要的部分是<useIncrementalCompilation>false</useIncrementalCompilation>

于 2015-11-15T20:37:33.100 回答
0

要使 inter-type 声明与 aspectj-maven-plugin 一起工作,需要通过添加选项 failOnError 来配置 maven-compiler-plugin:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <failOnError>false</failOnError>
            </configuration>
        </plugin>

解决方案取自https://fw-geekycoder.blogspot.com/2010/05/aspectj-inter-type-declaration.html

于 2020-07-24T22:06:32.007 回答