这是我的情况:

java - 使用Maven的AspectJ模块依赖-如何使用依赖模块的Inter-type声明方法-LMLPHP

我有一个由两个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());
    }
}

最佳答案

您的解决方案太复杂了:


AspectJ(AJ)模块需要AJ Maven插件和对Aspectjrt的依赖。到目前为止,一切都很好。
但是声明的方面仅影响其自身模块中的类,并通过ITD对其进行扩展。因此,无需在仅从AJ模块调用方法的纯Java模块上使用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和纯Java POM中都删除Exec Maven,以获得更短的POM。

07-25 20:23