Search code examples
javamavenaspectjaspectj-maven-plugin

aspectj - Intertype method declaration not working with Maven


I'm having troubles compiling a project with Maven and AspectJ.

This the POM excerpt for aspectj-maven-plugin (taken from effective 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>

When launching an mvn clean deploy I got these lines:

[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()')

Just to reduce the scope to a single case:

[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()')

So, it looks like the method getId() is correctly injected into the class Band. But when Maven proceeds to tests compilation and unit testing:

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

and, amongst other error details, I got

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

as if all my intertype declared stuff weren't injected, after all.

To check if there was some error in the code, I tried running the JUnit tests from within Eclipse, and everything worked fine.

So I guess there is something in the maven compilation process that I didn't set up correctly.

Can anyone help me understand what is causing the problem?


Solution

  • Finally came up with a solution. Leaving track here.

    What I did to solve the issue was configuring the maven-compiler-plugin like that:

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

    where the most important part is <useIncrementalCompilation>false</useIncrementalCompilation>