aspectj - объявление метода Intertype не работает с Maven
У меня проблемы с компиляцией проекта с Maven и AspectJ.
Это выдержка из POM aspectj-maven-plugin
(взят из действующего ПОМ):
<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()
правильно вводится в класс Band
, Но когда 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]
как будто все мои объявленные интертип вещи не были введены, в конце концов.
Чтобы проверить, есть ли какая-либо ошибка в коде, я попытался запустить тесты JUnit из Eclipse, и все работало нормально.
Так что я предполагаю, что в процессе компиляции Maven есть что-то, что я неправильно настроил.
Может кто-нибудь помочь мне понять, что является причиной проблемы?
1 ответ
Наконец-то придумали решение. Оставляя след здесь.
Для решения этой проблемы я настроил 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>
Чтобы межтиповое объявление работало с аспектом j-maven-plugin, необходимо настроить maven-compiler-plugin, добавив опцию failOnError:
<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