alicjasalamon
alicjasalamon

Reputation: 4291

Why Aspects in Maven cannot be applied?

I have problem with adding aspects to maven project. I've prepared small demo:

Project1 contains only one basic class

public class Sender {

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

Its 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>

and Project2 contains one aspect:

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

and its 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>

but result, when I try to run Sender is

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

Do you have any advice? Am I configuring this projects in proper way? Are poms OK? Any help will be appreciated.

Upvotes: 1

Views: 366

Answers (1)

yodamad
yodamad

Reputation: 1510

You have a version conflict : aspectj-maven-plugin in version 1.3 use AspectJ version : 1.6.7

link v1.3

If you want to use 1.6.11, you can use plugin version 1.4

link v1.4

Upvotes: 1

Related Questions