AspectJ模块与Maven的依赖关系 - 如何使用依赖模块的Inter-type声明方法

时间:2016-04-19 19:14:10

标签: java maven aop aspectj aspect

这是我的情况:

Workspace-my-project-aj-dependency

我有一个由两个jar模块组成的maven项目my-project-aj-dependency

  • my-project-aj-dependencyJarWithAJ(我有Inter-type declaration的地方,请参阅方面ahah()内的AppWithAj_Ahah.aj方法)
  • my-project-aj-dependencyJarWithoutAJ

我的问题是我想使用第二个模块中第一个模块方面定义的一些声明的方法,但可能我错过了一些东西。

我的poms配置如下:

Maven项目pom(my-project-aj-dependency):

<?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/maven-v4_0_0.xsd">
    <name>MyProjectAjDependency</name>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.madx</groupId>
    <artifactId>my-project-aj-dependency</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <!-- <module>TestMaven-ejb</module> -->
        <module>my-project-aj-dependencyJarWithAJ</module>
        <module>my-project-aj-dependencyJarWithoutAJ</module>
    </modules>

    <properties>
        <maven.compiler.target>1.7</maven.compiler.target>
        <maven.compiler.source>1.7</maven.compiler.source>
        <aspectj.version>1.8.9</aspectj.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.madx</groupId>
                <artifactId>my-project-aj-dependencyJarWithAj</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.madx</groupId>
                <artifactId>my-project-aj-dependencyJarWithoutAj</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
            </plugins>
        </pluginManagement>
    </build>

</project>

Maven模块1 pom(my-project-aj-dependencyJarWithAJ):

<?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>
   <parent>
      <groupId>com.madx</groupId>
      <artifactId>my-project-aj-dependency</artifactId>
      <version>0.0.1-SNAPSHOT</version>
   </parent>
   <groupId>com.madx</groupId>
   <artifactId>my-project-aj-dependencyJarWithAJ</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>my-project-aj-dependencyJarWithAJ</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version>1.8.9</version>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
               <execution>
                  <id>install</id>
                  <phase>install</phase>
                  <goals>
                     <goal>sources</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <!--
                    Have to use version 1.2 since version 1.3 does not appear to work
                    with ITDs
                -->
            <version>1.2</version>
            <dependencies>
               <!--
                        You must use Maven 2.0.9 or above or these are ignored (see
                        MNG-2972)
                    -->
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjrt</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjtools</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
            </dependencies>
            <executions>
               <execution>
                  <goals>
                     <goal>compile</goal>
                     <goal>test-compile</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <outxml>true</outxml>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

Maven模块2 pom(my-project-aj-dependencyJarWithoutAJ):

<?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>
   <parent>
      <groupId>com.madx</groupId>
      <artifactId>my-project-aj-dependency</artifactId>
      <version>0.0.1-SNAPSHOT</version>
   </parent>
   <groupId>com.madx</groupId>
   <artifactId>my-project-aj-dependencyJarWithoutAJ</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>my-project-aj-dependencyJarWithoutAJ</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>com.madx</groupId>
         <artifactId>my-project-aj-dependencyJarWithAj</artifactId>
         <version>${project.version}</version>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
               <execution>
                  <id>install</id>
                  <phase>install</phase>
                  <goals>
                     <goal>sources</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <!--
                    Have to use version 1.2 since version 1.3 does not appear to work
                    with ITDs
                -->
            <version>1.2</version>
            <dependencies>
               <!--
                        You must use Maven 2.0.9 or above or these are ignored (see
                        MNG-2972)
                    -->
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjrt</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjtools</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
            </dependencies>
            <executions>
               <execution>
                  <goals>
                     <goal>compile</goal>
                     <goal>test-compile</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <outxml>true</outxml>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

AppWithAj.java的位置:

package org.my.project.aj.dependencyJarWithAJ;

public class AppWithAj {
    public static void main( String[] args ){
        System.out.println( "Hello World!" );
    }
}

AppWithAj_Ahah.aj是:

package org.my.project.aj.dependencyJarWithAJ;

public aspect AppWithAj_Ahah {
    public String AppWithAj.ahah(){
        return "Ahahahah!";
    }
}

最后App.java是:

package org.my.project.aj.dependencyJarWithoutAJ;
import org.my.project.aj.dependencyJarWithAJ.AppWithAj;

public class App {
    public static void main( String[] args ) {
        System.out.println( "Hello World! " + new AppWithAj().ahah());
    }
}

2 个答案:

答案 0 :(得分:1)

你的解决方案太复杂了:

  • AspectJ(AJ)模块需要AJ Maven插件以及对 aspectjrt 的依赖。到目前为止,这么好。
  • 但声明的方面仅影响其自己模块中的类,通过ITD扩展它。因此,不需要在仅从AJ模块调用方法的纯Java模块上使用AJ编译器。调用方法是由ITD创建的,而另一个模块看起来就像普通的Java一样无关紧要。
  • 因此,所有非AJ模块需求都是对AJ模块的正常依赖,因为它使用了其中一个类。 AJ运行时依赖项是可传递的,应该自动使用。

<强>更新

你问过POM,他们在这里。

Root POM:

<?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>com.madx</groupId>
    <artifactId>my-project-aj-dependency</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MyProjectAjDependency</name>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.7</java.source-target.version>
        <aspectj.version>1.8.9</aspectj.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>com.madx</groupId>
                <artifactId>my-project-aj-dependencyJarWithAj</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.madx</groupId>
                <artifactId>my-project-aj-dependencyJarWithoutAj</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <!-- IMPORTANT -->
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.8</version>
                    <configuration>
                        <!--<showWeaveInfo>true</showWeaveInfo>-->
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.source-target.version}</complianceLevel>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <!--<verbose>true</verbose>-->
                        <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
                    </configuration>
                    <executions>
                        <execution>
                            <!-- IMPORTANT -->
                            <phase>process-sources</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>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <modules>
        <module>my-project-aj-dependencyJarWithAJ</module>
        <module>my-project-aj-dependencyJarWithoutAJ</module>
    </modules>

</project>

使用AspectJ的模块:

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

    <parent>
        <groupId>com.madx</groupId>
        <artifactId>my-project-aj-dependency</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>my-project-aj-dependencyJarWithAJ</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
    </dependencies>
</project>

普通Java模块:

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

    <parent>
        <groupId>com.madx</groupId>
        <artifactId>my-project-aj-dependency</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>my-project-aj-dependencyJarWithoutAJ</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>org.my.project.aj.dependencyJarWithoutAJ.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.madx</groupId>
            <artifactId>my-project-aj-dependencyJarWithAj</artifactId>
        </dependency>
    </dependencies>
</project>

请注意:我添加了Exec Maven插件仅用于演示目的,因此您可以执行以下操作:

mvn clean install
mvn -pl my-project-aj-dependencyJarWithoutAJ exec:java

然后你应该在控制台上看到这样的东西:

(...)
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ my-project-aj-dependencyJarWithoutAJ ---
Hello World! Ahahahah!

从root和普通Java POM中删除Exec Maven,以获得更短的POM。

答案 1 :(得分:0)

我使用了这个pom配置:

Project 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/maven-v4_0_0.xsd">
   <name>abc</name>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.madx</groupId>
   <artifactId>abc</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>pom</packaging>
   <modules>
      <module>abc1</module>
      <module>abc2</module>
   </modules>
   <properties>
      <maven.compiler.target>1.7</maven.compiler.target>
      <maven.compiler.source>1.7</maven.compiler.source>
      <aspectj.version>1.8.9</aspectj.version>
   </properties>
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>com.madx</groupId>
            <artifactId>abc1</artifactId>
            <version>${project.version}</version>
         </dependency>
         <dependency>
            <groupId>com.madx</groupId>
            <artifactId>abc2</artifactId>
            <version>${project.version}</version>
         </dependency>
      </dependencies>
   </dependencyManagement>
   <build>
      <pluginManagement>
         <plugins />
      </pluginManagement>
   </build>
</project>

第一个模块的Pom:

<?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>
   <parent>
      <groupId>com.madx</groupId>
      <artifactId>abc</artifactId>
      <version>0.0.1-SNAPSHOT</version>
   </parent>
   <groupId>com.madx</groupId>
   <artifactId>abc1</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>abc1</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version>1.8.9</version>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
               <execution>
                  <id>install</id>
                  <phase>install</phase>
                  <goals>
                     <goal>sources</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <!--
                    Have to use version 1.2 since version 1.3 does not appear to work
                    with ITDs
                -->
            <version>1.2</version>
            <dependencies>
               <!--
                        You must use Maven 2.0.9 or above or these are ignored (see
                        MNG-2972)
                    -->
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjrt</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjtools</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
            </dependencies>
            <executions>
               <execution>
                  <goals>
                     <goal>compile</goal>
                     <goal>test-compile</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <outxml>true</outxml>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

第二个模块的Pom:

<?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>
   <parent>
      <groupId>com.madx</groupId>
      <artifactId>abc</artifactId>
      <version>0.0.1-SNAPSHOT</version>
   </parent>
   <groupId>com.madx</groupId>
   <artifactId>abc2</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>abc2</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>com.madx</groupId>
         <artifactId>abc1</artifactId>
         <version>${project.version}</version>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
               <execution>
                  <id>install</id>
                  <phase>install</phase>
                  <goals>
                     <goal>sources</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <!--
                        Have to use version 1.2 since version 1.3 does not appear to work
                        with ITDs
                    -->
            <version>1.2</version>
            <dependencies>
               <!--
                            You must use Maven 2.0.9 or above or these are ignored (see
                            MNG-2972)
                        -->
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjrt</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
               <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjtools</artifactId>
                  <version>${org.aspectj-version}</version>
               </dependency>
            </dependencies>
            <executions>
               <execution>
                  <goals>
                     <goal>compile</goal>
                     <goal>test-compile</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <outxml>true</outxml>
               <source>${java-version}</source>
               <target>${java-version}</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

我在这里展示了它的工作原理:AspectJ module dependency with Maven

修改 似乎如果nonAj项目没有AspectJ性质,它会在.aj定义的方法附近发出警告(虽然它只是启动它就可以正常工作,对于eclipse来说这不是错误,顺便说一句,这不允许你使用自动完成广告填充代码与游戏...)。这是快照: Warning if the other has not a Aj nature