4

I am still beginner in Java. I would like to build a simple Maven project and implement AspectJ to monitor some stuffs. I followed this tutorial and this tutorialbut it all points to use Spring in the Maven, which will complicate some stuffs in the project. Do you have any suggestions for workaround that? I already put AspectJ Maven plugin in the pom.xml but it doesn't work.

Thanks

ps. These are the example codes I would like to get.

MainApp.java

package tester;

public class MainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        HelloWorld a = new HelloWorld();
        a.printHello();
    }
}

HelloWorld.java

package tester;

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void printHello() {
        System.out.println("Spring 3 : Hello ! " + name);
    }}

TestAspect.java

package tester;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;


@Aspect
public class TestAspect {

//  @Pointcut("execution(* /HelloSpring/src/main/java/tester/HelloWorld.setName(..))")
//  private void testing(){
//      
//  }

    @Before("execution(* tester.HelloWorld.printHello(..))")
    public void testBefore2(){
        System.out.println("yoo2");
    }


    @Before("execution(* tester.HelloWorld.setName(String))")
    public void testBefore(){
        System.out.println("yoo");
    }

}

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>test.spring</groupId>
  <artifactId>HelloSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
  <spring.version>4.2.5.RELEASE</spring.version>
  </properties>

  <dependencies>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.8</version>
        </dependency>
  </dependencies>


  <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.8</version>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>
Ihsan Haikal
  • 1,085
  • 4
  • 16
  • 42

2 Answers2

2

Here is a better suited pom, for me it works with the current code.

<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>test.spring</groupId>
<artifactId>HelloSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.8</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>
</build>

Nosfert
  • 382
  • 1
  • 3
  • 11
  • I tried using your POM configuration but it still does not work. Do you have any idea why? Do you think I need to convert the normal Maven project to Maven AspectJ project using Right Click - Configure - Convert to AspectJ Project? – Ihsan Haikal Mar 29 '16 at 07:33
  • What IDE are you using? Have you tried running it via console just with mvn clean install? Im using IntelliJ and i have tried both from console and from IntelliJ and i can run your code with my POM. – Nosfert Mar 29 '16 at 20:56
  • I am using Eclipse Mars and haven't tried from console as it has some problems with it. But I already tried to build it with clean install arguments still AspectJ is not captured. – Ihsan Haikal Mar 30 '16 at 08:34
1

You should add "execution" tag as well:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>${aspectj-maven-plugin.version}</version>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
    <executions>
         <execution>
             <phase>process-sources</phase>
             <goals>
                 <goal>compile</goal>
                 <goal>test-compile</goal>
             </goals>
         </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven.compiler.plugin.version}</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
utkusonmez
  • 1,486
  • 15
  • 22