繁体   English   中英

Maven 的 AspectJ 模块依赖 - 如何使用依赖模块的类型间声明方法

[英]AspectJ module dependency with Maven - How get working the Inter-type declarations methods of a dependency module

这是我的情况:

Workspace-my-project-aj-dependency

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

  • my-project-aj-dependencyJarWithAJ (我有一个Inter-type 声明,请参阅下面AppWithAj_Ahah.aj方面的ahah()方法)
  • 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());
    }
}

您的解决方案太复杂了:

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

更新:

你要求 POM,他们在这里。

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

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

我用这个 pom 配置让它工作:

项目 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 这不是错误,顺便说一句,这不允许您使用自动完成广告填充代码以减少...)。 这是快照: 警告,如果另一个没有 Aj 性质

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM