Maven+AspectJ-配置它的所有步骤



我在将方面应用到我的maven项目时遇到了问题。也许我遗漏了什么,所以我列出了一个步骤清单。你能检查一下它是否正确吗?

比方说,在projectA中是一个方面类,而在projectB类中,应该通过方面来更改。

  • 使用AspectJ类创建maven项目ProjectA
  • 添加Aspectj插件和依赖项
  • ProjectA作为依赖项添加到projectBpom.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

我追踪了你以前的一些问题,试图找出你真正想做的事情。我提出了以下结构和代码,对我来说效果很好。

$tree。。├──pom.xml├──项目A|├──pom.xml|└──src|└──主要的|└──方面|└──com|└──栈溢出|└──方面|├──AspectL.java|└──Trace.aj└──项目B├──pom.xml└──src└──主要的└──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目录并运行应用程序:

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设置也有效。

这对我来说很有效,a必须添加对外部依赖项的查找(请参阅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>

希望它能帮助。。。

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

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

看看一个真实的例子:

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

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

最新更新