Multi-module MAVEN project using Eclipse's AspectJ Yi phase weaving

Source: Internet
Author: User

ASPECTJ is a sub-project under the Eclipse Foundation, ASPECTJ provides two ways to implement cut-off code weaving

    • Weaving in the Yi Period: three kinds of cases

      Knitting Yi class Files

      Knitting Yi after weaving

      Class loading when weaving in

    • Run-time weaving


This article is introduced in the practice of using Yi class file weaving


ASPECTJ provides two ways to implement the facets

Based on the. aj File Description

Based on Java annotations


The following introduction is based on Java annotations

<properties><aspectj.version>1.8.9</aspectj.version></properties>< dependencymanagement>    <dependencies>         <dependency>    <groupId>org.aspectj</groupId>     <artifactid>aspectjrt</artifactid>    <version>${aspectj.version} </version></dependency><dependency>             <groupId>org.aspectj</groupId>             <artifactId>aspectjtools</artifactId>             <version>${aspectj.version}</version>         </dependency>    </dependencies>< Dependencymanagement><dependencies><dependency>    <groupid>org.aspectj</groupid>    < artifactid>aspectjrt</artifactid></dependency><dependency>    < Groupid>org.aspectj</groupid>    <artifactid>aspectjtools</artifactid ></dependency></dependencies><plugins>  <plugin>     <groupid>org.codehaus.mojo</groupid>    <artifactid>aspectj-maven-plugin </artifactId>    <version>1.10</version>    < configuration>        <source>1.8</source>         <target>1.8</target>         <complianceLevel>1.8</complianceLevel>        < xaddserialversionuid>true</xaddserialversionuid>        <showweaveinfo>true</ showweaveinfo>     </configuration>            <executions>        <execution >               <id> compile_with_aspectj</id>                <goals>                   <goal>compile</goal>                </goals>             </execution>            < execution>                <id>test-compile_with_aspectj</id >               <goals>                    <goal>test-compile</goal>                </goals>            </ execution>     </executions>          </plugin></plugins>

To re-compile the Yi project, you can find the class AJC generated in the generated class file.


If in a multi-module project, in a specific sub-module declaration Slice class, define the Pointcut expression, but the connection point is scattered in each other module, AJC Scan to the tangent expression, only in this module scan corresponding connection point, the other modules of the connection point is no way to compile Yi Phase cut-in section, AJC is not going to Yi other modules when you are not scanning for a tangent expression that matches the current connection point. In order to solve this problem, the AJC plugin needs to be defined in the parent pom file.

<properties><aspectj.version>1.8.9</aspectj.version></properties>< dependencymanagement>  <dependencies>     <dependency>     <groupId>org.aspectj</groupId>    <artifactId> aspectjrt</artifactid>    <version>${aspectj.version}</version></ dependency><dependency>        <groupid>org.aspectj< /groupid>        <artifactid>aspectjtools</artifactid>         <version>${aspectj.version}</version>      </dependency>     dependency>           <groupid>com.xx.xx</groupid>  <artifactid>xx_ Aspectj</artifactid>  <version>t.0.0.1-snapshot</version>     </dependency>   </ dependencies><dependencymanagement><dependencies><dependency>    < Groupid>org.aspectj</groupid>    <artifactid>aspectjrt</artifactid> </dependency><dependency>    <groupId>org.aspectj</groupId>     <artifactId>aspectjtools</artifactId></dependency></dependencies> <plugins>    <plugin> <groupId>org.codehaus.mojo</groupId>  <artifactId>aspectj-maven-plugin</artifactId> <version>1.10</version>     <configuration>    <source>1.8</source>     <target>1.8</target>    <compliancelevel>1.8</ Compliancelevel>    <xaddserialversionuid>true</xaddserialversionuid>             <showWeaveInfo>true</showWeaveInfo>     <aspectLibraries>                   <aspectLibrary>                         <groupId> com.aiatss.coast</groupid><artifactid>xx_aspectj</artifactid>                    </aspectLibrary>                     </aspectLibraries>    </configuration>    < Executions>       <execution>                        <id>compile_with_aspectj</id>                          <goals>                             <goal> compile</goal>                         </goals>                </execution>                <execution>                         <id>test-compile_with_ aspectj</id>                         <goals>                              <goal>test-compile</goal>                         </goals>                  </execution >    </executions> <dependencies>     < Dependency>          <groupid>com.xx.xx</groupid >  <artifactid>xx_aspectj</artifactid>  <version>t.0.0.1-snapshot</version>      </dependency>      <dependency> < groupid>org.aspectj</groupid>              <artifactId>aspectjtools</artifactId>              <version>${aspectj.version}</version>     </ dependency>      </dependencies>  </plugin> </ Plugins>

Like the above configuration, if the Submodule PROJECT_ASPECTJ is also referenced as a submodule of the parent pom file, then the Yi error will be:

The projects in the reactor contain a cyclic reference:edge between ' vertex{label= ' Com.xx.xx:xx_aspectj:t.0.0.1-snapshot '} ' and ' Vertex{label= ' com.xx.xx:framework:t.0.0.1-snapshot '} ' introduces to cycle in the graph com.xx.xx:framework:t.0 .0.1-snapshot--com.xx.xx:xx_parent:t.0.0.1-snapshot--Com.xx.xx:xx_aspectj:t.0.0.1-snapshot Com.xx.xx:framework:t.0.0.1-snapshot, [Help 1]

This is a cyclic reference problem, that is: the XX_ASPECTJ module refers to the parent Pom project, and the parent Pom Project also refers to the XX_ASPECTJ project, which Maven thinks is a mistake and should not appear, using the following method to transform


<properties><aspectj.version>1.8.9</aspectj.version></properties>< dependencymanagement>  <dependencies>     <dependency>     <groupId>org.aspectj</groupId>    <artifactId> aspectjrt</artifactid>    <version>${aspectj.version}</version></ dependency><dependency>        <groupid>org.aspectj< /groupid>        <artifactid>aspectjtools</artifactid>         <version>${aspectj.version}</version>      </dependency>     dependency>           <groupid>com.xx.xx</groupid>  <artifactid>xx_ Aspectj</artifactid>  <version>t.0.0.1-snapshot</version>     </dependency>   </ dependencies><dependencymanagement><dependencies><dependency>    < Groupid>org.aspectj</groupid>    <artifactid>aspectjrt</artifactid> </dependency><dependency>    <groupId>org.aspectj</groupId>     <artifactId>aspectjtools</artifactId></dependency></dependencies> <plugins>    <plugin> <groupId>org.codehaus.mojo</groupId>  <artifactId>aspectj-maven-plugin</artifactId> <version>1.10</version>     <configuration>    <source>1.8</source>     <target>1.8</target>    <compliancelevel>1.8</ Compliancelevel>    <xaddserialversionuid>true</xaddserialversionuid>             <showWeaveInfo>true</showWeaveInfo>     <aspectLibraries>                   <aspectLibrary>                         <groupId> com.aiatss.coast</groupid><artifactid>xx_aspectj</artifactid>                    </aspectLibrary>                     </aspectLibraries>    </configuration>    < Executions>       <execution>                        <id>compile_with_aspectj</id>                          <goals>                             <goal> compile</goal>                         </goals>                </execution>                <execution>                         <id>test-compile_with_ aspectj</id>                         <goals>                              <goal>test-compile</goal>                         </goals>                  </execution >    </executions>   </plugin> </plugins>

Then block the parent Pom plug-in in the Xx_aspectj subproject, as follows:

<plugins><plugin>    <groupId>org.codehaus.mojo</groupId>     <artifactid>aspectj-maven-plugin</artifactid>    <version >1.10</version>    <configuration>         <source>1.8</source>        <target>1.8 </target>        <compliancelevel>1.8</compliancelevel >          <xaddserialversionuid>true</ xaddserialversionuid>                     <showWeaveInfo>true</showWeaveInfo>     </configuration>     <executions>         <execution>                         <id>compile_with_aspectj</id>                        < phase>none</phase>                     </execution>                     <execution>                          <id>test-compile_with_aspectj</id>                        <phase> None</phase>                    </ Execution>    </executions> </plugin></plugins>

This is, what if the XX_ASPECTJ project is going to refer to other public submodules (scale: Xx_common, xx_repository)? Can only be disabled from the parent Pom aspectj plug-in in the Xx_common, Xx_repository project, so the facets in the XX_ASPECTJ module will not be able to cut into the Xx_common, Xx_repository project Connection points



In practice, refer to the article: http://www.mojohaus.org/aspectj-maven-plugin/multimodule/multimodule_strategy.html

Multi-module MAVEN project using Eclipse's AspectJ Yi phase weaving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.