为什么不能应用Maven中的方面?

时间:2012-09-12 08:27:03

标签: java maven aspectj

我有向maven项目添加方面的问题。我准备了小型演示:

Project1只包含一个基本类

public class Sender {

    public static void main(String[] args) {
        System.out.println("here main");
    }
}

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>Project1</groupId>
    <artifactId>Project1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <build>

        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.3</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>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>Project2</groupId>
                            <artifactId>Project2</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
            </plugin>

        </plugins>
    </build>


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

        <dependency>
            <groupId>Project2</groupId>
            <artifactId>Project2</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
</project>

Project2包含一个方面:

@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");
    }
}

及其pom

<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>Project2</groupId>
  <artifactId>Project2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>

  <build>

     <sourceDirectory>src</sourceDirectory>
    <plugins>
       <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.3</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>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
        </configuration>
      </plugin>

    </plugins>
  </build>

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

  </dependencies>
</project>

但结果是,当我尝试运行Sender

Exception in thread "main" java.lang.NoSuchMethodError: AspectL.aspectOf()LAspectL;
    at Sender.main(Sender.java:6)

你有什么建议吗?我是否以适当的方式配置此项目? pom可以吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您遇到版本冲突:版本1.3中的aspectj-maven-plugin使用AspectJ版本:1.6.7

link v1.3

如果要使用1.6.11,可以使用插件版本1.4

link v1.4