【问题标题】:Maven + AspectJ - all steps to configure itMaven + AspectJ - 配置它的所有步骤
【发布时间】:2012-09-14 11:51:22
【问题描述】:

我在将方面应用到我的 Maven 项目时遇到问题。可能我错过了一些东西,所以我列出了步骤。你能检查一下它是否正确吗?

假设projectA 是一个方面类,而在projectB 类中,应该通过方面进行更改。

  • 使用AspectJ类创建maven项目ProjectA
  • 添加Aspectj插件和依赖
  • 将ProjectA 作为依赖添加到projectB pom.xml
  • 添加到projectBpom.xml插件
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.target}</target>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>ProjectA</groupId>
                <artifactId>ProjectA</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
</plugin>
  • 添加 aspectj 依赖项

在所有这些步骤之后,我的问题是,在编译过程中我得到:

[WARNING] advice defined in AspectE has not been applied [Xlint:adviceDidNotMatch]

然后当我运行我的程序时:

Exception in thread "FeatureExcutionThread" java.lang.NoClassDefFoundError: AspectE

【问题讨论】:

标签: java maven aspectj


【解决方案1】:

我跟踪了您的一些较早的问题,以试图找出您真正想要做的事情。我想出了以下结构和代码,对我来说效果很好。

$树。 . ├── pom.xml ├── 项目A | ├── pom.xml | └── 源 | └── 主要 | └── 方面 | └── com | └── 堆栈溢出 | └── 方面 | ├── AspectL.java | └── Trace.aj └── 项目B ├── pom.xml └── 源 └── 主要 └── java └── com └── 堆栈溢出 └── App.java

pom.xml

<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>org.stackoverflow</groupId>
    <artifactId>Q12423965</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <modules>
        <module>ProjectA</module>
        <module>ProjectB</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.stackoverflow</groupId>
                <artifactId>Q12423965-ProjectA</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

ProjectA/pom.xml

<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>org.stackoverflow</groupId>
        <artifactId>Q12423965</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12423965-ProjectA</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>
    </dependencies>

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

我创建了两个不同的方面。一个使用@AspectJ 注释,另一个被定义为经典的 AspectJ 方面。

AspectL.java

package com.stackoverflow.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author maba, 2012-09-18
 */
@Aspect
public class AspectL {

    @Pointcut("execution(* main(..))")
    public void defineEntryPoint() {
    }

    @Before("defineEntryPoint()")
    public void aaa(JoinPoint joinPoint) {
        System.out.println("aspect before");
    }

    @After("defineEntryPoint()")
    public void bbb(JoinPoint joinPoint) {
        System.out.println("aspect after");
    }
}

Trace.aj

package com.stackoverflow.aspects;

public aspect Trace {
    pointcut publicMethodExecuted(): execution(public !static * *(..));

    after(): publicMethodExecuted() {
        System.out.printf("Enters on method: %s. \n", thisJoinPoint.getSignature());

        Object[] arguments = thisJoinPoint.getArgs();
        for (int i =0; i < arguments.length; i++){
            Object argument = arguments[i];
            if (argument != null){
                System.out.printf("With argument of type %s and value %s. \n", argument.getClass().toString(), argument);
            }
        }
        System.out.printf("Exits method: %s. \n", thisJoinPoint.getSignature());
    }
}

这两个文件是 ProjectA 模块的一部分,并且是 jar 的一部分。

现在我们要使用这些方面,并将它们编织到 ProjectB 的代码中。

ProjectB/pom.xml

<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>org.stackoverflow</groupId>
        <artifactId>Q12423965</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12423965-ProjectB</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>
        <dependency>
            <groupId>org.stackoverflow</groupId>
            <artifactId>Q12423965-ProjectA</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <configuration>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.stackoverflow</groupId>
                            <artifactId>Q12423965-ProjectA</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.stackoverflow.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

App.java

package com.stackoverflow;

/**
 * @author maba, 2012-09-17
 */
public class App {

    public void hello(String name) {
    }

    public static void main(String[] args) {
        App app = new App();
        app.hello("world");
    }
}

我从顶级 pom 构建一切:

mvn clean install

然后进入ProjectB目录并运行app:

mvn exec:java

结果是:

aspect before
Enters on method: void com.stackoverflow.App.hello(String). 
With argument of type class java.lang.String and value world. 
Exits method: void com.stackoverflow.App.hello(String). 
aspect after

因此得出结论,这两个方面都有效,并且 maven 设置也有效。

【讨论】:

  • 这真的是一个非常好的关于Maven项目之间编译时间编织的描述。我正在寻找相同的。这正是我想要的。感谢分享。
  • -1,因为它根本不起作用。我自己花了一整天的时间试图让这个例子发挥作用。它将与 Eclipse AJDT 一起使用,但如果您尝试仅使用 maven 编译它,则不会。您必须在 jar 中添加 maven-shade-plugin 或 maven-assembly-plugin aspectj 库。
  • -1 to @user3663882(如果可以的话)因为没有详细说明什么“不起作用”,没有错误消息,没有版本,不知道你遇到了什么问题。跨度>
  • 感谢您的回答。我已将此答案转换为 repo 以便快速试用。 github.com/brsanthu/aspectj-test
  • 谢谢。如果我能投票 10 次,我会的。这是现实世界中 AspectJ 缺少的简单且非常有用的“操作方法”指南。很好的答案。
【解决方案2】:

这对我有用,必须添加对外部依赖项的查找(请参阅 weaveDependencies)才能对其执行编织,您应该将其包含在 ProjectA pom 文件中:

<dependencies>
  <dependency>
    <groupId>com.ProjectB</groupId>
    <artifactId>ProjectB</artifactId>
  </dependency>
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.5</version>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.2</version>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal><!-- to weave all your main classes -->
            <goal>test-compile</goal><!-- to weave all your test classes -->
          </goals>
        </execution>
      </executions>
      <configuration>
        <weaveDependencies>

          <weaveDependency>
            <groupId>com.ProjectB</groupId>
            <artifactId>ProjectB</artifactId>
          </weaveDependency>
        </weaveDependencies>
        <showWeaveInfo>true</showWeaveInfo>

        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.target}</target>
      </configuration>
    </plugin>
  </plugins>
</build>

希望对你有帮助...

【讨论】:

  • 这引入了反向依赖 A ---depends-on--> B。这将导致循环依赖。而且我们必须提前知道哪些应用需要编织。
【解决方案3】:

我和你有同样的问题。我找到的唯一解决方案是为 aspectJ 项目中的每个方面类创建一个包装器,以让外部项目可以访问它

【讨论】:

  • 很高兴听到您解决了这个问题 :) 不幸的是我不懂解释。您能否提供更多详细信息/示例?
【解决方案4】:

我记得我也遇到过这个问题,但是通过切换到基于 Java 注释的方面解决了这个问题。你可以试试看。

【讨论】:

  • 我已经在使用@AspectJ,所以很遗憾它无济于事。您还记得您为使其正常工作所做的任何其他更改吗?
【解决方案5】:

看一个现实生活中的例子:

  • 这是我们定义方面并编译它们的模块:pom.xml。
  • 这是我们在另一个项目中使用它们的方式:pom.xml(注意突出显示的行)。

您的问题是您没有将ProjectA 作为ProjectB 的运行时依赖项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    相关资源
    最近更新 更多