Hot questions for Using AspectJ in aspectj maven plugin

Top Java Programmings / AspectJ / aspectj maven plugin

Question:

I try to use aspectj maven plugin for compile project with aspectj compiler and then I try to package classes into "war" file. Unfortunately, it doesn't work with following configuration (pom.xml):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.liferay.maven.plugins</groupId>
            <artifactId>liferay-maven-plugin</artifactId>
            <version>${liferay.maven.plugin.version}</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                </execution>
            </executions>
            <configuration>
                <autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
                <appServerDeployDir>${liferay.app.server.deploy.dir}</appServerDeployDir>
                <appServerLibGlobalDir>${liferay.app.server.lib.global.dir}</appServerLibGlobalDir>
                <appServerPortalDir>${liferay.app.server.portal.dir}</appServerPortalDir>
                <liferayVersion>${liferay.version}</liferayVersion>
                <pluginType>portlet</pluginType>

            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.7</source>
                <target>1.7</target>
                <showWarnings>true</showWarnings>
                <failOnError>true</failOnError>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilationLevel>1.7</compilationLevel>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.7.4</version>
    <type>jar</type>
</dependency>

After mvn clean install I see following exceptions:

[INFO] --- aspectj-maven-plugin:1.7:compile (default) @ tvbs-portlet ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[ERROR] Missing message: configure.incompatibleComplianceForSource in: org.aspectj.ajdt.ajc.messages
    <unknown source file>:<no line information>

[ERROR] no sources specified
    <unknown source file>:<no line information>

[ERROR] AspectJ Compiler 1.8.2

    Usage: <options> <source file | @argfile>..

AspectJ-specific options:
    -inpath <list>      use classes in dirs and jars/zips in <list> as source

Could anybody suggest me some solution?


Answer:

It seems like a known issue http://jira.codehaus.org/browse/MASPECTJ-125

You can fix it by adding the following to your pom file.

<complianceLevel>1.6</complianceLevel>

Question:

I've been trying to to compile a project that relies on aspectJ-maven-plugin it works fine with the compilanceLevel 1.9(java 9) but when it comes to java 10 apparently it is not supported?

ERROR]  Compliance options:
[ERROR]     -1.3               use 1.3 compliance (-source 1.3 -target 1.1)
[ERROR]     -1.4             + use 1.4 compliance (-source 1.3 -target 1.2)
[ERROR]     -1.5 -5 -5.0       use 1.5 compliance (-source 1.5 -target 1.5)
[ERROR]     -1.6 -6 -6.0       use 1.6 compliance (-source 1.6 -target 1.6)
[ERROR]     -1.7 -7 -7.0       use 1.7 compliance (-source 1.7 -target 1.7)
[ERROR]     -1.8 -8 -8.0       use 1.8 compliance (-source 1.8 -target 1.8)
[ERROR]     -1.9 -9 -9.0       use 1.9 compliance (-source 1.9 -target 1.9)
[ERROR]     -source <version>  set source level: 1.3 to 1.9 (or 6, 6.0, etc)
[ERROR]     -target <version>  set classfile target: 1.1 to 1.9 (or 6, 6.0, etc)

its there anyway to workaround this?


Answer:

I had this problem too. Since the ajc documentation is laughably out of date, I finally found the problem in the source code. For Java 10, you cannot use 1.10 for the compliance level, source, and target. You must use 10.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <configuration>
        <!-- MUST use 10, NOT 1.10 or ajc breaks -->
        <complianceLevel>10</complianceLevel>
        <source>10</source>
        <target>10</target>
    </configuration>
    ...
</plugin>

Question:

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>

Answer:

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>

Question:

I am trying get the AspectJ weaving working in a simple Maven project, and not sure where it is going wrong : when I run the code using "mvn exec:java", I dont see expected output.

I am sure the code is working, because I tried the same in STS, where it works fine. I just wanted to get the AspectJ working in a Maven project.

Any hints to how to debug this kind of issues will be much appreciated.

<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>com.aop</groupId>
<artifactId>aop1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>aop1</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.3</version> <!-- specify your version -->
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.7</source>
                            <target>1.7</target>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <outxml>true</outxml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <mainClass>com.aop.aop1.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Aspect file in same folder as code :

    package com.aop.aop1;


public aspect aspect {

    pointcut secureAccess()
        : execution(* *.foo(..));

    before() : secureAccess() {
        System.out.println("BEHOLD the power of AOP !!!");
    }
}

Java file :

package com.aop.aop1;
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        foo();
    }
    public static void foo() {
        System.out.println(" IN FOO.");
    }
}

Answer:

There are several problems with your configuration:

  • The aspect should be named Aspect with a capital "A" instead of aspect which is a reserved keyword.
  • The POM is missing a closing </project> tag.
  • The POM has a <pluginManagement> section, but no separate <plugins> section, i.e. you provide defaults for your plugins, but do not actually declare that you want to use them. So either you use a stand-alone <plugins> section without <pluginManagement> or you redeclare the plugins in an additional <plugins> section.
  • The aspectj-maven-plugin needs a <version>. You forgot to specify one.
  • The aspectj-maven-plugin also needs a <complianceLevel> configuration.
  • You use compile-time weaving, so you do not need the <outxml> setting. It is only needed for load-time weaving.
  • The aspectjrt dependency needs at least version 1.7.4 in order to be compatible with the version used in aspectj-maven-plugin 1.6 by default in order to compile your sources.

In addition to that, I recommend to use newer versions of Maven plugins and dependencies such as JUnit and exec-maven-plugin if you do not have any compelling reasons to use older ones. I also recommend to use the latest AspectJ version 1.8.2 and also specify to use that one internally in aspectj-maven-plugin.

Working pom.xml with minimal changes:

<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>com.aop</groupId>
    <artifactId>aop1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>aop1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                    <complianceLevel>1.7</complianceLevel>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.7</source>
                            <target>1.7</target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <mainClass>com.aop.aop1.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Working pom.xml with recommended changes:

<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>com.aop</groupId>
    <artifactId>aop1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>AOP Sample</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <aspectj.version>1.8.2</aspectj.version>
        <java.source-target.version>1.7</java.source-target.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <!-- IMPORTANT -->
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>${java.source-target.version}</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3</version>
                <configuration>
                    <mainClass>com.aop.aop1.App</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.dstovall</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <configuration>
                            <onejarVersion>0.96</onejarVersion>
                            <mainClass>com.aop.aop1.App</mainClass>
                            <attachToBuild>true</attachToBuild>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>OneJAR googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
            <!--<scope>runtime</scope>-->
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

BTW, the onejar-maven-plugin is just a goodie which I like to use in order to build a stand-alone uber JAR (a.k.a. fat JAR) containing everything you need to run the software, i.e. your classes/aspects plus the AspectJ runtime. You can just run the program with

java -jar aop1-0.0.1-SNAPSHOT.one-jar.jar

The output should be similar to mvn exec:java just without the Maven stuff:

Hello World!
BEHOLD the power of AOP !!!
IN FOO.

Question:

I have the following aspectJ pointcut:

@Around(value="execution(* *(*,Map<String, Object>)) && @annotation(com.xxx.annotations.MyCustomAnnotation)")

As you can see, this pointcut only matches methods, annotated with com.xxx.annotations.MyCustomAnnotation, which have 2 arguments - the first one is arbitrary and the second one must be of type Map<String, Object>.

Is there a way to configure the aspectj-maven-plugin to force compilation errors if it find methods that are annotated with com.xxx.annotations.MyCustomAnnotation, but don't match the signature * *(*,Map<String, Object>) ?

Or in other words, :

@com.xxx.annotations.MyCustomAnnotation
public void test(String s, Map<String, String> m) {
   ...
}

-> I want this to produce compile time error because Map<String, String> != Map<String, Object>


Answer:

You do it directly within an aspect, no need to configure it in AspectJ Maven plugin. Here is a little sample:

Marker annotation:

package de.scrum_master.app;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation {}

Sample application class:

package de.scrum_master.app;

import java.util.Map;

public class Application {
    public void notAnnotated(String s, Map<String, Object> m) {}

    @MyCustomAnnotation
    public void correctSignature(String s, Map<String, Object> m) {}

    @MyCustomAnnotation
    public void wrongSignature(String s, Map<String, String> m) {}
}

Aspect declaring compile error on method signature mismatch:

package de.scrum_master.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareError;

@Aspect
public class PointcutChecker {
    @DeclareError(
        "execution(* *(..)) && " +
        "@annotation(de.scrum_master.app.MyCustomAnnotation) && " +
        "!execution(* *(*, java.util.Map<String, Object>))"
    )
    static final String wrongSignatureError =
        "wrong method signature for @MyCustomAnnotation";
}

When compiling this code you will see the following error in Eclipse as a code annotation and in the problem view (Maven console would show the same error when performing AspectJ Maven's compile goal):

Question:

I have a project that uses compiled aspects and weaves them at compile time. I want to add Lombok, but unfortunately AJC is not supported by Lombok. Since this project doesn't have any sources of aspects on its own i configured AspectJ Maven plugin to do post-compile weaving instead, after compiling with Javac+Lombok.

Here is config for AspectJ Maven plugin:

<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectory>${project.build.outputDirectory}</weaveDirectory>

It's attached to compile phase right after Maven Compiler plugin compile. That way Lombok + Javac will be invoked first and later AJC will perform weaving on Javac's generated class files.

Is there any limitations/disadvantages when performing bytecode weaving on javac generated classes?

Maybe there is a better approach of making Maven+Lombok+Aspects+Idea work together without issues.

Here is a minimal example project: https://github.com/Psimage/aspectj-and-lombok


Answer:

When in the other question you asked me in a comment I actually thought that you had problems with your approach, but it is working. The only thing I had to do in order to run the test directly from IDE (IntelliJ IDEA) is to actually delegate application and test runners to Maven because otherwise IDEA does not get Lombok + AspectJ applied at the same time.

If your approach works, use it. But actually AspectJ Maven suggests another approach: compiling with Maven compiler first to another output directory, then use that directory as weave directory for the AspectJ compiler. The sample POM there does not work 100%, though, because when specifying an output directory for Javac on the command line that directory needs to exist, it will not be created by the compiler. So you need some ugly Antrun action, too:

<plugins>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <id>unwovenClassesFolder</id>
        <phase>generate-resources</phase>
        <configuration>
          <tasks>
            <delete dir="${project.build.directory}/unwoven-classes"/>
            <mkdir dir="${project.build.directory}/unwoven-classes"/>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <!-- Modifying output directory of default compile because non-weaved classes must be stored
             in separate folder to not confuse ajc by reweaving already woven classes (which leads to
             to ajc error message like "bad weaverState.Kind: -115") -->
        <id>default-compile</id>
        <configuration>
          <compilerArgs>
            <arg>-d</arg>
            <arg>${project.build.directory}/unwoven-classes</arg>
          </compilerArgs>
        </configuration>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <configuration>
      <aspectLibraries>
        <aspectLibrary>
          <groupId>me.yarosbug</groupId>
          <artifactId>aspects</artifactId>
        </aspectLibrary>
      </aspectLibraries>

      <forceAjcCompile>true</forceAjcCompile>
      <sources/>
      <weaveDirectories>
        <weaveDirectory>${project.build.directory}/unwoven-classes</weaveDirectory>
      </weaveDirectories>
    </configuration>
    <executions>
      <execution>
        <phase>process-classes</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
  </plugin>

</plugins>

I would suggest another approach as an alternative:

  1. Create an unwoven Java module, doing Java + Lombok stuff there.
  2. Create a separate module for AspectJ binary weaving, using the Java module as a weave dependency. Because your unit test depends on both Lombok and AspectJ, put the test in this module.

The advantage is that you don't need to fiddle around with multiple compilers, execution phases, output directories, Antrun etc.


Update:

I cloned your GitHub MCVE and this commit on branch master reflects what I have explained in my sample XML above.

I also created a branch multi-phase-compilation with another commit which effectively refactors the project according to my alternative idea. I am just quoting the commit message:

Multi-phase compilation: 1. Java + Lombok, 2. AspectJ binary weaving

There are many changes (sorry, I should have split them into multiple
commits):
  - Marker annotation renamed to @marker and moved to separate module
    because the main application should not depend on the aspect module.
    Rather both application and aspect now depend on a common module.
  - New module "main-app-aspectj" does only AspectJ binary weaving on
    the already lomboked Java application.
  - Both application modules have slightly different unit tests now: One
    checks that Lombok has been applied and AspectJ has not, the other
    checks that both have been applied.
  - Aspect pointcut limits matching to "execution(* *(..))" in order to
    avoid also matching "call()" joinpoints.

The end result is that now we have a clear separation of concerns, clear
dependencies, no more scripted Ant build components and the new option
to use the lomboked code optionally with or without aspects applied
because both types or JARs are created during the build.

Feel free to add my fork as another remote to your Git repository and pull in my changes from there. If you prefer me to send you pull requests in order to make it easier, just let me know.

Question:

I use Eclipse Kepler, Java 1.7. Part of my pom.xml is below. As I see during maven compile, no logs about weaving at all. Neither I have any errors. Aspect doesn't work neither. What am I doing wrong? As I see in some examples, this pom should work. I installed AspectJ tools to Eclipse.

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>${compiler.version}</source>
                <target>${compiler.version}</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>${compiler.version}</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
            </plugin>

Edited: This is my aspect code:

package util;

import java.util.logging.Logger;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StopWatch;

@Aspect
public class MyAspect {
    private static final Logger logger = Logger.getLogger(MyAspect.class.getName());

    @Around("execution(public * filter.RolesFilter.doFilter(..))")
    public Object loggingMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        Object retVal = joinPoint.proceed();

        stopWatch.stop();

        StringBuffer logMessage = new StringBuffer();
        logMessage.append(joinPoint.getTarget().getClass().getName());
        logMessage.append(".");
        logMessage.append(joinPoint.getSignature().getName());
        logMessage.append("(");
        // append args
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            logMessage.append(args[i]).append(",");
        }
        if (args.length > 0) {
            logMessage.deleteCharAt(logMessage.length() - 1);
        }
        logMessage.append(")");
        logMessage.append(" execution time: ");
        logMessage.append(stopWatch.getTotalTimeMillis());
        logMessage.append(" ms");
        System.out.println(logMessage.toString());
        logger.info(logMessage.toString());
        return retVal;
    }

}

The method I want to weave with my aspect:

package filter;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RolesFilter implements Filter {
    Logger logger = Logger.getLogger(RolesFilter.class.getName());

    public RolesFilter() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException {
        logger.info("This is logger message");
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        String reqURI = req.getRequestURI();
        String userName = (String) session.getAttribute("username");
        Character role = (Character) session.getAttribute("role");
        try {
            if (role != null && role.charValue() == 'A') {
                if (reqURI.indexOf("/admin/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/view/roles/admin/home.xhtml");
                }
            } else if (role != null && role.charValue() == 'B') {
                if (reqURI.indexOf("/biller/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/biller.xhtml");
                }
            } else if (role != null && role.charValue() == 'C') {
                if (reqURI.indexOf("/customer/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/customer.xhtml");
                }
            } else {
                res.sendRedirect(req.getContextPath()
                        + "/login.xhtml");
            }
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Also, here is maven clean compile output:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for myproject:war:0.0.1-SNAPSHOT
[WARNING] The expression ${build.sourceDirectory} is deprecated. Please use ${project.build.sourceDirectory} instead.
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ myproject ---
[INFO] Deleting /Users/user1/git/myproject/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.1:compile (default-compile) @ myproject ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 41 source files to /Users/user1/git/myproject/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.007 s
[INFO] Finished at: 2015-02-16T10:00:23-08:00
[INFO] Final Memory: 12M/245M
[INFO] ------------------------------------------------------------------------

Answer:

Your Maven log shows no sign of aspectj-maven-plugin being executed at all, consequently there is no weaving. I suppose you have put your plugin config into the <pluginManagement> section, but have not referenced the configured plugin from the <plugins> section. This would explain the missing plugin output. I.e. you need to do this:

<pluginManagement>
    <plugins>
        <!-- Configure plugin here -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                (...)
            </configuration>
            (...)
    </plugins>
</pluginManagement>

(...)

<plugins>
    <!-- Refer to the pre-configured plugin here in order to actually use it -->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
    </plugin>
</plugins>

Question:

When i use maven-apsectj-plugin and maven-compiler-plugin compile phase will execute both plugins compile goal. This results in compilation with javac first and then full recompilation with ajc.

Is this double compilation necessary? It seems like i can just turn maven-compiler-plugin off and everything works just fine.

I'm using "default" configuration as stated in the usage of maven-compiler-plugin:

<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.13</version>
    </dependency>
    ...
  </dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.11</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  <build>
  ...
</project>

Answer:

Yes, you could deactivate Maven Compiler plugin because the AspectJ compiler is a regularly refreshed fork of Eclipse Java Compiler. Thus is can also compile your Java files.

But if the situation is more complex, e.g. you use Maven Compiler to also compile Groovy files or files in other modules and want to configure it only once in <pluginManagement>, maybe deactivating it is not such a nice option. There is a way to make both plugins play nice together, see my other answers

  • https://stackoverflow.com/a/24341085/1082681
  • https://stackoverflow.com/a/50997238/1082681

Basically you configure Maven Compiler to use <useIncrementalCompilation>false</useIncrementalCompilation> and AspectJ Maven to use <phase>process-sources</phase>. More information is in the linked answers.

Then you will see output like this:

[INFO] --- aspectj-maven-plugin:1.12.1:compile (default) @ openbook_cleaner ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] 
[INFO] --- aspectj-maven-plugin:1.12.1:test-compile (default) @ openbook_cleaner ---
[WARNING] No sources found skipping aspectJ compile
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ openbook_cleaner ---
[INFO] Nothing to compile - all classes are up to date

Question:

I can't configure Weblogic 12c to work with AspectJ. Reading some posts I have done something to try to configure it, but I can't reach a result. My project works with maven and aspectj maven plugin. My configuration it's the following:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>co.example</groupId>
<artifactId>PruebaAspectJ</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>basicWebapp</name>
<parent>
    <groupId>com.oracle.weblogic.archetype</groupId>
    <artifactId>wls-common</artifactId>
    <version>12.1.3-0-0</version>
</parent>
<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
</dependencies>
<build>
    <finalName>basicWebapp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <target>1.8</target>
                <source>1.8</source>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>${java.source-target.version}</source>
                <target>${java.source-target.version}</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>${java.source-target.version}</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
                <aspectDirectory>src/java/aspectos</aspectDirectory>
                <!--<sources>
                    <source>
                        <basedir>src/main/java</basedir>
                        <includes>
                            <include>**/*.aj</include>
                            <include>**/*.java</include>
                        </includes>
                    </source>
                </sources>-->
                <outputDirectory>${project.reporting.outputDirectory}/aspectj-report</outputDirectory>

            </configuration>
            <executions>
                <execution>
                    <!-- IMPORTANT -->
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</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.7</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.8</java.source-target.version>
    <aspectj.version>1.8.7</aspectj.version>
</properties>

My Aspect

package aspectos;

public aspect Logger {
pointcut logger() : call(* co.example..*(..));

before() : logger() {
    System.out.println("#### Signatura: "+thisJoinPointStaticPart.getSignature());
    boolean entro = false;
    for (int i = 0; i < thisJoinPoint.getArgs().length; i++) {
        if(!entro){
            System.out.println("#### Argumentos: ");
            entro=true;
        }
        System.out.println("\t"+thisJoinPoint.getArgs()[i].getClass().toString());
    }
    System.out.println("#### Target: "+thisJoinPoint.getTarget().getClass().toString());
}

after() returning(Object r): logger(){
    if(r!=null){
        System.out.println("#### Objeto retornado: "+r.getClass().getSimpleName());
    }
}

after() throwing(Throwable e): logger(){
    System.out.println("#### Excepcion: "+e.getMessage());
}
}

So, When I run mvn clean install this error is shown:

Errors shown by AspectJ

I know Spring has compatibility with AspectJ but I can't use it, I just need the configuration shown above. If someone want to help me I have all the code of the example in this repo in github:

https://github.com/afdecastro879/aspectJPrueba

Finally, I'm developing my project Using IntelliJ Idea IDE.

Thanks for all


Answer:

The aspect as such looks a bit long-winded, but okay (except for the package name typo co.example instead of com.example in your pointcut). What I would recommend you to do, though, is to use a standard Maven directory layout instead of customising paths in your AspectJ Maven plugin, especially because IntelliJ IDEA plays so nicely with Maven projects, being able to auto-update IDEA project settings whenever you change the POM etc.

You should remove those two parameters from your AspectJ Maven configuration:

<aspectDirectory>src/java/aspectos</aspectDirectory>
<!-- ... -->
<outputDirectory>${project.reporting.outputDirectory}/aspectj-report</outputDirectory>

I cloned your repo and fixed the POM and a few other things (typos in package names in pointcuts, committed binaries etc.) for you. I also created a pull request in order to enable you to easily integrate my changes into your repo. Last, but not least, I added a sample stand-alone application with a main method so as to be able to quickly test the whole thing.

Now Maven says:

[INFO] --- aspectj-maven-plugin:1.8:compile (default) @ PruebaAspectJ ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] Join point 'method-execution(java.lang.String com.example.AccountBean.getName())' in Type 'com.example.AccountBean' (AccountBean.java:29) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(void com.example.AccountBean.setName(java.lang.String))' in Type 'com.example.AccountBean' (AccountBean.java:33) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(float com.example.AccountBean.getAmount())' in Type 'com.example.AccountBean' (AccountBean.java:37) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(void com.example.AccountBean.setAmount(float))' in Type 'com.example.AccountBean' (AccountBean.java:41) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(java.lang.String com.example.AccountBean.getMsg())' in Type 'com.example.AccountBean' (AccountBean.java:45) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(void com.example.AccountBean.deposit())' in Type 'com.example.AccountBean' (AccountBean.java:50) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-call(void com.example.AccountBean.setName(java.lang.String))' in Type 'de.scrum_master.app.Application' (Application.java:8) advised by before advice from 'aspectos.Logger' (Logger.aj:9)
[INFO] Join point 'method-call(void com.example.AccountBean.setName(java.lang.String))' in Type 'de.scrum_master.app.Application' (Application.java:8) advised by afterReturning advice from 'aspectos.Logger' (Logger.aj:22)
[INFO] Join point 'method-call(void com.example.AccountBean.setName(java.lang.String))' in Type 'de.scrum_master.app.Application' (Application.java:8) advised by afterThrowing advice from 'aspectos.Logger' (Logger.aj:28)
[INFO] Join point 'method-call(void com.example.AccountBean.setAmount(float))' in Type 'de.scrum_master.app.Application' (Application.java:9) advised by before advice from 'aspectos.Logger' (Logger.aj:9)
[INFO] Join point 'method-call(void com.example.AccountBean.setAmount(float))' in Type 'de.scrum_master.app.Application' (Application.java:9) advised by afterReturning advice from 'aspectos.Logger' (Logger.aj:22)
[INFO] Join point 'method-call(void com.example.AccountBean.setAmount(float))' in Type 'de.scrum_master.app.Application' (Application.java:9) advised by afterThrowing advice from 'aspectos.Logger' (Logger.aj:28)

And the sample application yields this output:

#### Signatura: void com.example.AccountBean.setName(String)
#### Argumentos: 
    class java.lang.String
#### Target: class com.example.AccountBean
Executing aspectj
#### Signatura: void com.example.AccountBean.setAmount(float)
#### Argumentos: 
    class java.lang.Float
#### Target: class com.example.AccountBean
Executing aspectj
com.example.AccountBean@1be6f5c3

Process finished with exit code 0

Question:

I have this situation. This is my Village.java:

public class Village{

    private Integer vid;
    private String villageName;
    private String district;

    public Integer getVid() {
        return vid;
    }
    public void setVid(Integer vid) {
        this.vid = vid;
    }
    public String getVillageName() {
        return villageName;
    }
    public void setVillageName(String villageName) {
        this.villageName = villageName;
    }
    public String getDistrict() {
        return district;
    }
    public void setDistrict(String district) {
        this.district = district;
    }
}

This is my Dao.java interface:

public interface Dao<T> {
    public void insert();
    public void update();
    public void delete();
}

This is my aspect Village_Dao.aj (you can just ignore the static methods logic):

import org.apache.ibatis.session.SqlSession;
import com.madx.finance.data.utils.persistence.Dao;
import com.madx.finance.data.utils.factory.ConnectionFactory;

public aspect Village_Dao {
    declare parents: Village implements Dao<Village>;

    public void Village.insert() {
        Village.insertVillage(this);
    }

    public void Village.update() {
        Village.updateVillage(this);
    }

    public void Village.delete() {
        Village.deleteVillage(this.getVid());
    }

    public Village Village.getData() {
        return Village.getDataVillage(this.getVid());
    }

    public static void Village.insertVillage(Village village) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        mapper.insertVillage(village);
        session.commit();
        session.close();
    }

    public static void Village.updateVillage(Village village) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        mapper.updateVillage(village);
        session.commit();
        session.close();
    }

    public static void Village.deleteVillage(Integer id) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        mapper.deleteVillage(id);
        session.commit();
        session.close();
    }

    public static Village Village.getDataVillage(Integer id) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        Village village = mapper.selectVillage(id);
        session.close();
        return village;
    }
}

I'm trying without success to convert the Village_Dao.aj to an annotated version Village_Dao_Java.java. I just managed to make the class implements Dao but I can't manage to write the methods (insert, update e delete separately in this file Village_Dao_Java.java).

This is version (still not complete) of the Village_Dao_Java.java (I read this link but I couldn't get it work for this case):

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

import com.madx.finance.data.utils.persistence.Dao;

@Aspect
public class Village_Dao_Java {

    @DeclareParents("com.madx.demo.Village")
    private Dao<Village> implementedInterface;
}

Answer:

What you want is not possible with @AspectJ style, you need to use the more powerful native syntax. (Why do you want to switch anyway?)

The reason behind this is that the Java compiler can only convert @DeclareParents to a form in which Village is a subclass of whatever interface implementation you define in your aspect, for example something like this:

@Aspect
public class Village_Dao_Java {
    public static class VillageDao implements Dao<Village> {
        @Override
        public void insert() {
            Village.insertVillage(this);
        }

        @Override
        public void update() {
            Village.updateVillage(this);
        }

        @Override
        public void delete() {
            Village.deleteVillage(this.getVid());
        }

        public Village getData() {
            return getDataVillage(this.getVid());
        }

    }

    @DeclareParents(
        value = "de.scrum_master.app.Village",
        defaultImpl = VillageDao.class
    )
    private Dao<Village> villageDao;
}

But this approach has several problems:

  • The methods try to access static methods from the future VillageDao subclass Village, so you cannot declare the static methods in VillageDao but have to declare them in Village directly.
  • If instead you would declare them directly in VillageDao you could call them e.g. via VillageDao.insertVillage(this), but then the signature public static void insertVillage(Village village) would no longer fit because this is a VillageDao, not its own subclass Village.
  • For similar reasons you cannot call this.getVid() because this is a VillageDao and not a Village, but the getter method has a fixed signature in the original Village class.

And so forth. The Java compiler just is not powerful enough to do what the AspectJ compiler does: weave code right into the original class files.

Ergo: Please stick with native syntax. Not only is it more powerful but IMO also more readable. I never understood why so many people try to coax the powerful AspectJ into the poor alternative @AspectJ syntax. Somehow they seem to believe that they get a gain from pure Java syntax. I disagree. They just get a degradation of technical means and bad syntax never meant to be used for full-fledged AOP.

Question:

I want to retrieve the calling method where a specific method is called. Example : The method I consider :

public void methodA(int a, int b){...}

is called in a test method and also in the program itself

@Test
public void testMethodA(
... some code...
objectClassA.methodA(x,y);
)}

Class B {
...
 public void methodB(){
    objectClassA.methodA(x,y);
   }
}

What I want to obtain somehow is the inside or at least the signature of testMethodA and methodB To do that I thought AspectJ could help me, so I looked into that and ended up writing this poincut pointcut pcmethodA(): execution(* A.methodA(..) );

and my advice looked something like this

before(): pcmethodA() {
        System.out.println("[AspectJ] Entering " + thisJoinPoint);
        System.out.println("[AspectJ] Signature " + thisJoinPoint.getSignature());
        System.out.println("[AspectJ] SourceLocation "+ thisJoinPoint.getSourceLocation());

But this returns

[AspectJ] Entering execution(void com.example.somePackage.A.methodA(int, int)
[AspectJ] Signature com.example.somePackage.A.methodA(int, int)
[AspectJ] SourceLocation A.java:25   /** Line number of the methodA in the file **/

It is my first time using AspectJ , is there any object or way to retreive the calling method of my found joinpoints? testMethodA and methodB

Thanks


Answer:

Let me recreate your situation first with a few sample classes + driver application:

package de.scrum_master.app;

public class Foo {
  public void methodA(int a, int b) {
    System.out.println("methodA: " + a + ", " + b);
  }
}
package de.scrum_master.app;

public class DummyTest {
  public void testSomething() {
    new Foo().methodA(33, 44);
  }
}
package de.scrum_master.app;

public class Application {
  public void doSomething() {
    new Foo().methodA(11, 22);
  }

  public static void main(String[] args) {
    new Application().doSomething();
    new DummyTest().testSomething();
  }
}

Now try call() in combination with thisEnclosingJoinPointStaticPart in your aspect:

package de.scrum_master.aspect;

import de.scrum_master.app.Foo;

public aspect MyAspect {
  pointcut pcmethodA() : call(* Foo.methodA(..));

  before() : pcmethodA() {
    System.out.println("[AspectJ] Executing: " + thisJoinPoint);
    System.out.println("[AspectJ] Called by: " + thisEnclosingJoinPointStaticPart);
  }
}

The console log when running Application:

[AspectJ] Executing: call(void de.scrum_master.app.Foo.methodA(int, int))
[AspectJ] Called by: execution(void de.scrum_master.app.Application.doSomething())
methodA: 11, 22
[AspectJ] Executing: call(void de.scrum_master.app.Foo.methodA(int, int))
[AspectJ] Called by: execution(void de.scrum_master.app.DummyTest.testSomething())
methodA: 33, 44

See? No need to juggle stack traces if you just want to determine caller and callee.

Question:

I am working with AspectJ at the moment. I seperated AspectJ code in a dependency. Within that dependency everything works as intended. But as soon as I import it in another project only some functionality does not work anymore. When using the defaultImpl of @DeclareParents, the interface is shown within the compiled code but not the default Implementation. Here is my code to show what I mean (every code snippet is its own File):

AspectJ code:

public interface IAspect
{
    String hello();
}

public class IAspectDefaultImpl implements IAspect
{
    @Override
    public String hello()
    {
        return "hello";
    }
}
@Aspect
public class AspectJ
{

    @DeclareParents(value = "@SomeAnnotation*", defaultImpl = IAspectDefaultImpl.class)
    private IAspect implementedInterface;
}

Target Class in a different project:

@SomeAnnotation
public class MyClass
{
    private final int myValue;

    public MyClass(final int wert)
    {
        this.myValue = wert;
    }


    public int getMyValue()
    {
        return myValue;
    }
}

Maven throws me:

The type MyClass must implement the inherited abstract method IAspect.hello()

Which implies that it works partially. When looking at the decompiled .class files the targeted Class does in fact implement IAspect. The method defined in IAspectDefaultImpl is still missing tho.

My pom is set up like in this example.

I am not sure where I should start to look for errors. Any help is apreciated.


Answer:

Thanks for the MCVE. But hey, you don't use Git in order to commit 7z or ZIP archives, you ought to commit source code. I forked your project and fixed that, restructured and simplified your POMs and also fixed the main problem.

See my pull request and the commits in it for further details.


Concerning your problem, I can confirm that it occurs if you use @DeclareParents the way you do in an aspect library.

Actually, according to AspectJ maintainer Andy Clement there are certain problems with @DeclareParents when using it to provide parent interfaces + implementations in annotation style. The native AspectJ syntax via declare parents is not affected by that, but for annotation-style syntax Andy provided an alternative called @DeclareMixin, see the AspectJ manual. There he mentions that he is even considering to deprecate the defaultImpl argument of @DeclareParents in favour of @DeclareMixin.

So my bugfix (or workaround) for your problems is to actually replace

@DeclareParents(value = "@de.example.aspect.SomeAnnotation *", defaultImpl = IAspectDefaultImpl.class)
private IAspect implementedInterface;

by

@DeclareMixin("@de.example.aspect.SomeAnnotation *")
public static IAspect createIAspectImplementation() {
    return new IAspectDefaultImpl();
}

This works with aspect libraries.

I will discuss with Andy about whether it makes sense to file a bug ticket for your problem or if he won't fix it anyway because there is a viable and recommended alternative.

Question:

I am working with AspectJ at the moment. I seperated AspectJ code in a dependency. Within that dependency everything works as intended. But as soon as I import it in another project only some functionality does not work anymore. When using the defaultImpl of @DeclareParents, the interface is shown within the compiled code but not the default Implementation. Here is my code to show what I mean (every code snippet is its own File):

AspectJ code:

public interface IAspect
{
    String hello();
}

public class IAspectDefaultImpl implements IAspect
{
    @Override
    public String hello()
    {
        return "hello";
    }
}
@Aspect
public class AspectJ
{

    @DeclareParents(value = "@SomeAnnotation*", defaultImpl = IAspectDefaultImpl.class)
    private IAspect implementedInterface;
}

Target Class in a different project:

@SomeAnnotation
public class MyClass
{
    private final int myValue;

    public MyClass(final int wert)
    {
        this.myValue = wert;
    }


    public int getMyValue()
    {
        return myValue;
    }
}

Maven throws me:

The type MyClass must implement the inherited abstract method IAspect.hello()

Which implies that it works partially. When looking at the decompiled .class files the targeted Class does in fact implement IAspect. The method defined in IAspectDefaultImpl is still missing tho.

My pom is set up like in this example.

I am not sure where I should start to look for errors. Any help is apreciated.


Answer:

Thanks for the MCVE. But hey, you don't use Git in order to commit 7z or ZIP archives, you ought to commit source code. I forked your project and fixed that, restructured and simplified your POMs and also fixed the main problem.

See my pull request and the commits in it for further details.


Concerning your problem, I can confirm that it occurs if you use @DeclareParents the way you do in an aspect library.

Actually, according to AspectJ maintainer Andy Clement there are certain problems with @DeclareParents when using it to provide parent interfaces + implementations in annotation style. The native AspectJ syntax via declare parents is not affected by that, but for annotation-style syntax Andy provided an alternative called @DeclareMixin, see the AspectJ manual. There he mentions that he is even considering to deprecate the defaultImpl argument of @DeclareParents in favour of @DeclareMixin.

So my bugfix (or workaround) for your problems is to actually replace

@DeclareParents(value = "@de.example.aspect.SomeAnnotation *", defaultImpl = IAspectDefaultImpl.class)
private IAspect implementedInterface;

by

@DeclareMixin("@de.example.aspect.SomeAnnotation *")
public static IAspect createIAspectImplementation() {
    return new IAspectDefaultImpl();
}

This works with aspect libraries.

I will discuss with Andy about whether it makes sense to file a bug ticket for your problem or if he won't fix it anyway because there is a viable and recommended alternative.

Question:

I am trying to work with AssertJ in IntelliJ Community Edition.

It's not working as expected. Where do am I making a mistake? Any help/insights would be very much appreciated.

Technology Ref:

  1. IntelliJ IDEA 2018.2.4 (Community Edition)

  2. java version "1.8.0_77"


package org.kayd;

public class Client {
public static void main(String[] args) {
    Client data = new Client();
    data.data();
}

public void data() {
    System.out.println("kayd");
}
}

Aspect Class

package org.kayd;

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;

@Aspect
public class AspectTest {

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

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

    @After("execution(org.kayd.Client.data())")
    public void after() {
        System.out.println("log");
    }

}

AOP.xml

<aspectj>
    <aspects>
        <aspect name="org.kayd.AspectTest"/>
    </aspects>
</aspectj>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.kayd</groupId>
    <artifactId>AOP</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.9</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>1.8</source>
                    <target>1.8</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>${java.version}</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Screenshot

Ref: I already looked into these questions but still not working.


Answer:

I just had a little bit of time and recreated your Maven project on my machine. What I said in my comment is actually true, so I am quoting myself here in order to turn it into an answer:

At first glance two things strike me as odd:

  • You apply compile-time weaving, but also provide an aop.xml which is only needed for load-time weaving. So which way do you want to go? For CTW you can just remove it.
  • Secondly, the pointcut execution(org.kayd.Client.data()) should yield a compile error because the syntax is invalid (no return type specified for the method signature). You should rather use something like execution(* org.kayd.Client.data()) or execution(void org.kayd.Client.data()).

I want to add that using after as a method name is not advised because in AspectJ native syntax it is a reserved keyword. The compiler does not complain, but still you should be careful here.

I modified your aspect like this to avoid the Maven compile error and also to see some more on the console:

package org.kayd;

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;

@Aspect
public class AspectTest {
  @Pointcut("execution(* *(..))")
  public void defineEntryPoint() {}

  @Before("defineEntryPoint()")
  public void log(JoinPoint joinPoint) {
    System.out.println("Before advice on " + joinPoint);
  }

  @After("execution(org.kayd.Client.data())")
  public void afterAdvice(JoinPoint joinPoint) {
    System.out.println("After advice on " + joinPoint);
  }
}

I also pimped your POM a bit because you use the latest AspectJ version on one hand, but an old version of AspectJ Maven plugin depending on an older version, which leads to strange log output on the Maven console. Furthermore, I added a One-JAR packager and Maven Exec plugin, so you have your whole application in one Über-JAR and can just run it without anything else on the classpath.

<?xml version="1.0" encoding="UTF-8"?>
<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.kayd</groupId>
  <artifactId>AOP</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.8</java.source-target.version>
    <aspectj.version>1.9.2</aspectj.version>
    <main-class>org.kayd.Client</main-class>
  </properties>

  <build>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
            <source>${java.source-target.version}</source>
            <target>${java.source-target.version}</target>
            <!-- IMPORTANT -->
            <useIncrementalCompilation>false</useIncrementalCompilation>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>aspectj-maven-plugin</artifactId>
          <version>1.11</version>
          <configuration>
            <!--<showWeaveInfo>true</showWeaveInfo>-->
            <source>${java.source-target.version}</source>
            <target>${java.source-target.version}</target>
            <Xlint>ignore</Xlint>
            <complianceLevel>${java.source-target.version}</complianceLevel>
            <encoding>${project.build.sourceEncoding}</encoding>
            <!--<verbose>true</verbose>-->
            <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
          </configuration>
          <executions>
            <execution>
              <!-- IMPORTANT -->
              <phase>process-sources</phase>
              <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
          <dependencies>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjtools</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjweaver</artifactId>
              <version>${aspectj.version}</version>
            </dependency>
          </dependencies>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.4.0</version>
          <configuration>
            <mainClass>${main-class}</mainClass>
          </configuration>
        </plugin>
        <plugin>
          <groupId>com.jolira</groupId>
          <artifactId>onejar-maven-plugin</artifactId>
          <version>1.4.4</version>
          <executions>
            <execution>
              <goals>
                <goal>one-jar</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <onejarVersion>0.96</onejarVersion>
            <mainClass>de.scrum_master.app.FooBar</mainClass>
            <attachToBuild>true</attachToBuild>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>com.jolira</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <configuration>
          <mainClass>${main-class}</mainClass>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
      </plugin>
    </plugins>

  </build>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
  </dependencies>

</project>

Then do something like mvn clean verify and afterwards either of those two:

mvn exec:java

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< org.kayd:AOP >----------------------------
[INFO] Building AOP 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ AOP ---
Before advice on execution(void org.kayd.Client.main(String[]))
Before advice on execution(void org.kayd.Client.data())
kayd
After advice on execution(void org.kayd.Client.data())
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.043 s
[INFO] Finished at: 2018-12-12T12:07:59+07:00
[INFO] ------------------------------------------------------------------------

Or if you want to run the Über-JAR:

java -jar target\AOP-1.0-SNAPSHOT.one-jar.jar

Before advice on execution(void org.kayd.Client.main(String[]))
Before advice on execution(void org.kayd.Client.data())
kayd
After advice on execution(void org.kayd.Client.data())

Question:

I'm new to AOP and I need to use AspectJ on my project. I need to use around advice but I have a problem using it, I've the following code in my .aj class,

pointcut checkUser(ProceedingJoinPoint jp,User user): call(* com.example.UserAccount.MyUI.checkUser(..))&& args(jp,user);

void around(ProceedingJoinPoint jp,User user) throws Throwable : checkUser(jp,user){
    // Condition checks one of the user boolean property
    if(condition){
        jp.proceed();
    }else{
        // Do nothing
    }   
}

but I get this warning all the time,

advice defined in Aspects.UserAccount has not been applied [Xlint:adviceDidNotMatch]

By the way, I tried it without ProceedingJoinPoint and tried just proceed(); but then got this warning, too few arguments to proceed, expected 1

I'm thankful for any single help or hint!

Reza


Answer:

First I recommend to read the AspectJ documentation in order to learn the syntax. As you are using native AspectJ syntax, this is like learning a new programming language or at least a Java extension. What you are doing is mix native syntax with annotation-based syntax. Try to stick with one. I am sure that you did not find this in any tutorial but ended up with that syntax via trial and error.

You do not need to bind a joinpoint parameter in native syntax because it is there implicitly and automatically. The automatically bound joinpoint is always named thisJoinPoint as all tutorials surely show you. Only in annotation-based syntax you need to bind the joinpoint and can name it as you wish, but even then I recommend to stick with thisJoinPoint because then refactoring from annotation to native syntax is easier and your eyes get used to spotting that variable name in your aspect code.

The warning you get means that the pointcut you defined does not match any part of your code, at least not any part which is visible to the aspect weaver or compiler. There could be plenty of reasons why this can occur, e.g. misspelled package or class names, wrong around advice return type (return type must be Object for non-void methods or more specifically match what the method you want to intercept returns). Assuming that e.g. checkUser(..) returns a boolean, the around advice should do the same. I made up an example using your package and class names. Besides, package names should be lower-case but I used yours, assuming they are really package names and not inner classes:

Helper class:

package com.example.UserAccount;

public class User {
  private String name;

  public User(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  @Override
  public String toString() {
    return "User(" + name + ")";
  }
}

Class targeted by aspect + sample main method:

package com.example.UserAccount;

public class MyUI {
  public boolean checkUser(User user) {
    return user.getName().toUpperCase().contains("ADMIN");
  }

  public static void main(String[] args) {
    MyUI ui = new MyUI();
    System.out.println(ui.checkUser(new User("Administrator")));
    System.out.println(ui.checkUser(new User("john")));
    System.out.println(ui.checkUser(new User("xander")));
    System.out.println(ui.checkUser(new User("admiral")));
    System.out.println(ui.checkUser(new User("SySaDmiN")));
  }
}

As you can see, we expect an output of "true" for the first and last entry, but "false" for the ones in between due to the check logic I made up for checkUser(..).

Now let us write an aspect which also returns "true" for a user named "Xander", e.g. in order to give him admin rights or whatever. I am making this up because you did not provide an MCVE as you always should on StackOverflow, but just an incoherent code snippet which keeps everyone trying to answer your question guessing what the heck you might want to achieve and how to reproduce your problem.

Aspect:

package Aspects;

import com.example.UserAccount.User;
import com.example.UserAccount.MyUI;

public aspect UserAccount {
  pointcut checkUser(User user) :
    execution(boolean MyUI.checkUser(*)) && args(user);

  boolean around(User user) : checkUser(user) {
    System.out.println(thisJoinPoint + " -> " + user);
    if (user.getName().equalsIgnoreCase("xander"))
      return true;
    return proceed(user);
  }
}

I just imported the MyUI class, so there is no need to use a fully-qualified class name here. Again, this is an advantage of native syntax, in annotation-based syntax you would have to use the fully qualified name.

I also replaced the generic * MyUI.checkUser(..) (which would also work) by the more explicit boolean MyUI.checkUser(*) because we already know that the method returns a boolean and has exactly one parameter, which both we assume anyway by returning a boolean from the around advice and by binding exactly one parameter via args(). You could also be even more specific and use boolean MyUI.checkUser(User).

Furthermore, I am using execution() rather than call() because it is more efficient, as it weaves the advice code just into the executing method once instead of five times for each method call in the main method. You only need to use call() if the MyUI class is out of reach of the AspectJ weaver/compiler, i.e. because it is not in the module you compile with AspectJ Maven.

Console log:

execution(boolean com.example.UserAccount.MyUI.checkUser(User)) -> User(Administrator)
true
execution(boolean com.example.UserAccount.MyUI.checkUser(User)) -> User(john)
false
execution(boolean com.example.UserAccount.MyUI.checkUser(User)) -> User(xander)
true
execution(boolean com.example.UserAccount.MyUI.checkUser(User)) -> User(admiral)
false
execution(boolean com.example.UserAccount.MyUI.checkUser(User)) -> User(SySaDmiN)
true

Et voilà, the aspect works. It makes the target method return "true" for user "xander".

Question:

I create a apring-boot project which has two module test-aop and test-web. I have upload this project in github https://github.com/zhxjouc/test .In test-aop, I define some AOP class and I want it cuts the test-web module. And I think the codes might be OK, but the aspectj-maven-plugin might be not work. I tried to modify the configuration for all day but it still not work. The following is the error while run mvn clean install

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.9:compile (default) on project test-web: AJC compiler errors:
abort ABORT -- (RuntimeException) Problem processing attributes in com/test/aop/TestLogAspect.class
Problem processing attributes in com/test/aop/TestLogAspect.class
java.lang.RuntimeException: Problem processing attributes in com/test/aop/TestLogAspect.class
    at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:387)
    at org.aspectj.weaver.bcel.BcelObjectType.<init>(BcelObjectType.java:162)
    at org.aspectj.weaver.bcel.BcelWorld.buildBcelDelegate(BcelWorld.java:410)
    at org.aspectj.weaver.bcel.BcelWorld.addSourceObjectType(BcelWorld.java:487)
    at org.aspectj.weaver.bcel.BcelWorld.addSourceObjectType(BcelWorld.java:453)
    at org.aspectj.weaver.bcel.BcelWeaver.addAspectsFromJarFile(BcelWeaver.java:263)
    at org.aspectj.weaver.bcel.BcelWeaver.addLibraryJarFile(BcelWeaver.java:236)
    at org.aspectj.ajdt.internal.core.builder.AjBuildManager.initBcelWorld(AjBuildManager.java:874)
    at org.aspectj.ajdt.internal.core.builder.AjBuildManager.performBuild(AjBuildManager.java:249)
    at org.aspectj.ajdt.internal.core.builder.AjBuildManager.batchBuild(AjBuildManager.java:185)
    at org.aspectj.ajdt.ajc.AjdtCommand.doCommand(AjdtCommand.java:112)
    at org.aspectj.ajdt.ajc.AjdtCommand.runCommand(AjdtCommand.java:60)
    at org.aspectj.tools.ajc.Main.run(Main.java:371)
    at org.aspectj.tools.ajc.Main.runMain(Main.java:248)
    at org.codehaus.mojo.aspectj.AbstractAjcCompiler.execute(AbstractAjcCompiler.java:537)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.RuntimeException: bad WeaverState.Kind: -115.  File was :<Unknown>::0
    at org.aspectj.weaver.WeaverStateInfo.read(WeaverStateInfo.java:170)
    at org.aspectj.weaver.AjAttribute.read(AjAttribute.java:105)
    at org.aspectj.weaver.bcel.Utility.readAjAttributes(Utility.java:101)
    at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:383)
    ... 36 more

Here is pom configurations and I really want to where is the error. Please help me.

pom in test-group(parent)

<?xml version="1.0" encoding="UTF-8"?>
<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>com.test</groupId>
<artifactId>test-group</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>test-aop</module>
    <module>test-web</module>
</modules>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>

<properties>
    <aspectjweaver.version>1.8.9</aspectjweaver.version>
    <aspectjrt.version>1.8.9</aspectjrt.version>
    <compiler.version>1.8</compiler.version>
    <maven.compiler.version>3.3</maven.compiler.version>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectjrt.version}</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.9</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <forceAjcCompile>true</forceAjcCompile>
                    <source>${compiler.version}</source>
                    <target>${compiler.version}</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>1.8</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                    </weaveDirectories>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectjrt.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectjrt.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

pom in test-aop

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
    <artifactId>test-group</artifactId>
    <groupId>com.test</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-aop</artifactId>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

pom in test-web

<?xml version="1.0" encoding="UTF-8"?>
<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>com.test</groupId>
<artifactId>test-group</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>test-aop</module>
    <module>test-web</module>
</modules>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>

<properties>
    <aspectjweaver.version>1.8.9</aspectjweaver.version>
    <aspectjrt.version>1.8.9</aspectjrt.version>
    <compiler.version>1.8</compiler.version>
    <maven.compiler.version>3.3</maven.compiler.version>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectjrt.version}</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.9</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <forceAjcCompile>true</forceAjcCompile>
                    <source>${compiler.version}</source>
                    <target>${compiler.version}</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>1.8</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                    </weaveDirectories>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectjrt.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectjrt.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>


Answer:

If you use Spring, what is the reason for you to use the AspectJ compiler at all? For the simple use case "Spring component method interception" proxy-based Spring AOP is sufficient. You have not shown any aspect and application code, that is why I am asking. Before we try to solve an AspectJ Maven problem let us make sure you actually need the plugin at all.

Question:

I'd like to define aspectj joinpoints not using annotations and string constants like this:

@Before("execution(* my.class.getText(..))")

but using aspecj language, like in this example:

pointcut myMethod(): myClass() && execution(* *(..));

or

before (): getText() {
    Trace.traceEntry("" + thisJoinPointStaticPart.getSignature());
}

Is there any good examples of how to add aspectj to the project to make maven compile this language properly and how to re-write @Before, @Around etc. annotations in aspecj language?


Answer:

Have you ever considered using Google or Bing? You might have found

Here on SO I have also answered many questions about how to configure AspectJ Maven Plugin, e.g. here.

I hope this was helpful.

Question:

Java + Spring + Maven application. Unable to make Internal call from annotation based public method.

Prerequisite

  1. Java-Version: 1.7.
  2. Project: AspectProject > Post build it will create jar file.
  3. Client: AspectClient : which has dependency of "AspectProject".

AspectProject

  1. pom.xml
<properties>
        <java.version>1.7</java.version>    
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <springframework.version>4.1.2.RELEASE</springframework.version>        
        <org.aspectj-version>1.7.0</org.aspectj-version>
  </properties>

  <dependencies>

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

     <!-- AspectJ dependencies -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>

  </dependencies>

   <build> 
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>   
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
                <!-- compile for Java 1.7 -->
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
        </plugin>
        <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <complianceLevel>${maven.compiler.source}</complianceLevel>
                            <source>${maven.compiler.source}</source>
                            <target>${maven.compiler.target}</target>
                        </configuration>
                    </execution>
                </executions>
        </plugin>
    </plugins>
  </build>
  1. AspectProvider
@Aspect
public class AspectProvider {   
    /**
     * This is the point cut for all get Method with @TestAnnotation annotation
     */
    @Pointcut("execution(* get*()) && @annotation(aTestAnnotation)")
    public void getMethodPointcut(TestAnnotation aTestAnnotation) {}


    @Around("getMethodPointcut(aTestAnnotation)")
    public Object getConfiguration(ProceedingJoinPoint iJoinPoint, TestAnnotation aTestAnnotation) throws Throwable {
        return getValueFromISTCompositeConfiguration(iJoinPoint, aTestAnnotation);
    }

    private Object getValueFromISTCompositeConfiguration(final ProceedingJoinPoint iProceedingJoinPoint, TestAnnotation aTestAnnotation) throws Throwable {

        Object aReturnValue = null;
        if (aTestAnnotation.value() != null) {
            System.out.println("ASPECT: Returning annotation value.");
            aReturnValue = aTestAnnotation.value();
        } else {
            System.out.println("MISSING_GETTER_PROPERTY");
        }    
        if(aReturnValue == null){
             aReturnValue = iProceedingJoinPoint.proceed();
        }
        return aReturnValue;
    }
}
  1. Annotation "TestAnnotation"
@Component
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {  
    String value();
}

AspectClient

  1. pom.xml
<properties>
        <java.version>1.7</java.version>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <aspectProject.version>0.0.1-SNAPSHOT</aspectProject.version>
        <spring-framework.version>4.1.2.RELEASE</spring-framework.version>
        <org.aspectj-version>1.7.0</org.aspectj-version>
    </properties>

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

        <!-- AspectProject dependencies -->
        <dependency>
            <groupId>com.example.aop</groupId>
            <artifactId>AspectProject</artifactId>
            <version>${aspectProject.version}</version>
        </dependency>
    </dependencies>

<build>
  <sourceDirectory>src/main/java/</sourceDirectory>
  <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <!-- compile for Java 1.7 -->
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
           </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <aspectLibraries>
                        <aspectLibrary>
                             <groupId>com.example.aop</groupId>
                             <artifactId>AspectProject</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <complianceLevel>${maven.compiler.source}</complianceLevel>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal> 
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                </dependencies>
            </plugin> 
  </plugins>    
</build>
  1. Service Class
@Component
public class TestService {

    private String value;

    public void internalCall() {
        System.out.println("INTERNAL_CALL :"+ getValue());
    }

    @TestAnnotation("RETURNED_FROM_ASPECT_CALL") 
    public String getValue() {
        return value;
    }

    public void setValue(String iValue) {
        this.value = iValue;
    }

}
  1. Spring context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- Enable AspectJ style of Spring AOP -->
    <context:component-scan base-package="com.example.aop.client" />

    <aop:aspectj-autoproxy />

    <bean name="TestService" class="com.example.aop.client.service.TestService" />

    <!-- Configure Aspect Beans, without this Aspects advice wont execute -->
    <bean name="aspectProvider" class="com.example.aop.aspect.AspectProvider"/> 

</beans>
  1. Main class
public class SpringMain {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        TestService aTestService = ctx.getBean("TestService", TestService.class);

        System.out.println("EXTERNAL_CALL: "+aTestService.getValue());
        aTestService.internalCall();        
        ctx.close();
    }

}

OUTPUT:

ASPECT: Returning annotation value. 
EXTERNAL_CALL:RETURNED_FROM_ASPECT_CALL 
INTERNAL_CALL: **null**

Expected:

ASPECT: Returning annotation value.
EXTERNAL_CALL: RETURNED_FROM_ASPECT_CALL
INTERNAL_CALL: **RETURNED_FROM_ASPECT_CALL**

Please advice in case if I am missing any entry or required to change configuration.Thanks in advance for your valuable time.


Answer:

What you do is a bit strange because on the one hand you configure Spring to use (auto) proxy-based Spring AOP, on the other hand you use AspectJ Maven Plugin to use native AspectJ and do compile-time weaving. Please decide which way you want to go:

  • For Spring AOP you do not need the AspectJ compiler, but then you are stuck with the proxy-based "AOP lite" approach which comes at the cost of internal calls not being intercepted by aspects because they do not go through proxies but through this (the original object).
  • For full-blown AspectJ you can configure Spring to use LTW (load-time weaving) as described in manual chapter Using AspectJ with Spring applications. Alternatively, you can also use compile-time weaving, but this is not necessary unless you have performance problems during application start-up.

Question:

I have the following method:

public class MonitorInterface {

    // this is the method you have to call to trigger the monitor
    public static void event(String eventName, HashMap params) { 
        System.out.println("Entering event method");
    }

}

and the following aspect :

package aspects;

import com.path.for.MonitorInterface;
import java.util.HashMap;
public aspect _asp_connector0 {
    private pointcut eventP():
        execution(public static void event(String, HashMap));

    before(): eventP(){
        System.out.println("Test pointcut weave");
    }
}

which basically adds a Sys.out.print to the previous method

As for the pom.xml I am using the following plugins mainly:

 <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <complianceLevel>1.7</complianceLevel>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8</encoding>
        <verbose>true</verbose>
        <showWeaveInfo>true</showWeaveInfo>
        <sources>
            <source>
            <basedir>src/main/resources</basedir>
            <includes>
                <include>**/_asp_connector0.aj</include>
            </includes>
            <excludes>
                <exclude>**/*.java</exclude>
                    <exclude>**/*.lrv</exclude>
                    <exclude>**/*.txt</exclude>
                </excludes>
            </source>
        </sources>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
         <mainClass>path.to.main.Example</mainClass>
    </configuration>
</plugin>
<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
       <execution>
          <configuration>
            <onejarVersion>0.96</onejarVersion>
            <mainClass>path.to.main.Example</mainClass>
            <attachToBuild>true</attachToBuild>
          </configuration>
          <goals>
              <goal>one-jar</goal>
          </goals>
       </execution>
  </executions>
</plugin>

When i compile (using mvn clean install) and run the generated jar file however, I am never getting the weaved code in the desired method.

Alternatively I tried to run them using the ajc compiler manually as follows:

ajc -outjar testMain.jar -target 1.5 -source 1.5 src\main\java\path\to\Example.java src\main\java\path\to\MonitorInterface.java
set CLASSPATH=%CLASSPATH%;.\testMain.jar
ajc -outjar testAsp.jar -target 1.5 -source 1.5 src\main\resources\aspects\_asp_connector0.aj
set CLASSPATH=%CLASSPATH%;.\testAsp.jar
aj path.to.Example

This results in the warning

_asp_connector0.aj:12 [warning] advice defined in aspects._asp_connector0 has not been applied [Xlint:adviceDidNotMatch]

but the new println still doesn't appear

How can I solve this, or at least debug this more efficiently?

Note: With maven, the class file for the aspect is being generated, the code is just not being weaved into the actual method


Answer:

Since your aspect works in a plain java AspectJ project in Eclipse - at least for me - the problem must be your weaving configuration. One thing sticks out immediately:

<basedir>src/main/resources</basedir>

Why are you trying to apply your aspect to your resources and not your code(src/main/java as default in maven projects)? Changing that line and removing the includes/excludes, I get the following from the aspectj-maven-plugin:

[INFO] Join point 'method-execution(void program.MonitorInterface.event(java.lang.String, java.util.HashMap))' in Type 'program.MonitorInterface' (MonitorInterface.java:7) advised by before advice from 'aspects._asp_connector0' (_asp_connector0.aj:11)

So the advice is used and applied.

Is there a reason for you to specify these specific includes/excludes?

<exclude>**/*.java</exclude>

That alone will make it pretty much impossible to advise any Java-code, because you are excluding all Java-code from compile time weaving. I'd suggest removing all includes/excludes and only adding them piece by piece IFF you have a specific problem that can be solved by includes/excludes. For the usecase you describe here, they are entirely unnecessary and actually problematic in their current configuration.

Question:

There is a method for create an Aspect introduction conditionally? what i want is to extend a class using Spring AOP conditionally:

@Aspect
public class Test1Aspect {
    @DeclareParents(value="com.test.testClass",defaultImpl=Test1Impl.class)
    public ITest iTest;
}

@Aspect
public class Test2Aspect {
    @DeclareParents(value="com.test.testClass",defaultImpl=Test2Impl.class)
    public ITest iTest;
}

so testClass extends Test1Impl or Test2Impl depending of a properties file where i set that option, its possible? how i can exclude Aspects for being called, i try to use aspectj-maven-plugin but it don't exclude my Aspects:

pom.xml

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.5</version>
    <configuration>
        <sources>
            <source>
                <basedir>src/main/java</basedir>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </source>
        </sources>
    </configuration>
    <executions>
        <execution>
            <goals>
                <!-- use this goal to weave all your main classes -->
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

EDIT

I remove the aspectj-maven-plugin and using only Spring AOP, following is the configuration and the test aspect:

Aplication.java

@Configuration
@ComponentScan(basePackages= {
        "demo"
        //"demo.aspect"
})
@EnableAutoConfiguration(exclude=AopAutoConfiguration.class)
//@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
@EnableAspectJAutoProxy
public class Application {

    public static final Logger LOGGER = LogManager.getLogger(Application.class);

    @Bean
    public testService testService() {
        return new testService();
    }

    @Bean
    @Conditional(TestCondition.class) //CLASS THAT ONLY RETURNS TRUE OR FALSE
    public TestAspect testAspect() {
        LOGGER.info("TEST ASPECT BEAN");
        TestAspect aspect = Aspects.aspectOf(TestAspect.class);
        return aspect;
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

TestAspect

//@Component
//@Profile("asdasd")
//@Configurable
//@Configuration
@Aspect
public class TestAspect{
    public static final Logger LOGGER = LogManager.getLogger(TestAspect.class);

    @Autowired
    private testService testService;

    public TestAspect() {
        LOGGER.info("TEST ASPECT INITIALIZED");
    }

    @Around("execution(* demo.testControllerEX.test(*))")
    public String prevent(ProceedingJoinPoint point) throws Throwable{
        LOGGER.info("ASPECT AROUND " + testService); // ALWAYS CALLED NO MATTER IF THE CONDITION IS FALSE, THE ONLY DIFFERENCE IS THAT testService IS NULL WHEN THE CONDITION IS FALSE.
        String result = (String)point.proceed();
        return result;
    }

    /*@DeclareParents(value="(demo.testControllerEX)",defaultImpl=TestControllersImpl.class)
    private ITestControllerEX itestControllerEX;*/
}

Answer:

Finally i found the solution, the main problem is that in my Eclipse project i Enable Spring Aspects tooling in the option menu of Spring tools (Right click on project) and that in some manner was compiling my aspects with traditional Aspectj before Spring AOP, so that Explains why no matter which Conditional i was using over the aspect was always applied.

So the solution is do not enable Spring Aspectj Tools. or if is enabled do right click in project AspectJ Tools -> Remove AspectJ Capability.

Question:

I am trying to give a default implementation to Methods declared by java.lang#Object. Here is an MCVE for the problem I encounter. In the interface IAspect I declare an equals Method. In the implementation of this class I define it as always false. So in theory the main method of the example should output false but it outputs true. When decompiling the class said method is not there. I know that the project setup works since for the other Method in the interface AspectJ correctly generates the needed code. Is it even possible to do this with Annotations? I usually wrote my AspectJ code with the AspectJ-Syntax. Which worked as intended with no flaws. But I am not sure if I am able to extract the AspectJ code in a seperate library like in my MCVE if I use the AspectJ-Syntax.

So my actual questions:

  • Is it possible to give default implementations to non abstract Methods that are inherited from other Classes and not an interface that defines the Aspects?
  • Is it possible to use AspectJ-Syntax in a library and add this library as AspecJ-Dependency so that my code still uses the externally defined aspects?

Depending on the answers I will decide how to further progress.


Answer:

Just like in my answer to your previous question, again I created a pull request #2 for you. What it does:

  • Convert your annotation-style aspect into a native syntax one, declaring the interface and its method implementations inline, getting rid of three of your previous classes. This is more elegant and readable IMO. I do not like annotation-style syntax anyway, and as you found out already, the latter has limitations which native syntax does not have.

  • Re-structure your two separate Maven modules into a root/parent POM used to configure commonly used dependencies and plugins which are then only referenced and amended with other configuration details where necessary in the two child POMs. The library POM now has AspectJ Maven plugin in it again because we need the AJ compiler also for the aspect library due to the native syntax. Of course you can use native syntax aspects in a library and this module shows you how.

  • Another benefit is that now you can just call mvn clean package on the parent POM and the library will automatically be built and packaged before it is used by the application module. No more ugly need to use mvn clean install on the library first. Actually, install is not needed at all in order to build your application.

Now if after mvn clean package you run the application from the root directory, you will see the following:

$ java -jar example/target/example-1.0.0-SNAPSHOT.one-jar.jar
Test
false
false
hello

This is what you want, isn't it?

Question:

I have created a test for my aspectj class.

When I execute my test it works fine to "Run as TestNG" from Eclipse.

Then when I execute it in maven:

mvn clean test

I get the following error:

[15:15] [eraonel/git/java-runtime-stats] -> mvn clean test
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building java-runtime-stats 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ java-runtime-stats ---
[INFO] Deleting /repo/eraonel/git/java-runtime-stats/target
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (default) @ java-runtime-stats ---
[INFO] Executing tasks

main:
     [echo] BUILDING : /repo/eraonel/git/java-runtime-stats/src/main/java/com/company/commonlibrary/javaruntimestats/Version.java
     [echo] BUILD 2018-10-24 13:18 UTC : /repo/eraonel/git/java-runtime-stats/src/main/java/com/company/commonlibrary/javaruntimestats/Version.java
[INFO] Executed tasks
[INFO]
[INFO] --- maven-java-formatter-plugin:0.6.1-threadsafe:format (default) @ java-runtime-stats ---
[INFO] Using 'UTF-8' encoding to format source files.
[INFO] Number of files to be formatted: 21
[INFO] Successfully formatted: 1 file(s)
[INFO] Fail to format        : 0 file(s)
[INFO] Skipped               : 20 file(s)
[INFO] Approximate time taken: 0s
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-runtime-stats ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /repo/eraonel/git/java-runtime-stats/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ java-runtime-stats ---
[INFO] Compiling 15 source files to /repo/eraonel/git/java-runtime-stats/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-runtime-stats ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 11 resources
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ java-runtime-stats ---
[INFO] Compiling 6 source files to /repo/eraonel/git/java-runtime-stats/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /repo/eraonel/git/java-runtime-stats/src/test/java/com/company/commonlibrary/javaruntimestats/aspects/DeprecatedMethodsAspectTest.java:[17,13] cannot find symbol
  symbol:   class DeprecatedMethodsAspect
  location: class com.company.commonlibrary.javaruntimestats.aspects.DeprecatedMethodsAspectTest
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.127 s
[INFO] Finished at: 2018-10-24T15:18:08+02:00
[INFO] Final Memory: 33M/730M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project java-runtime-stats: Compilation failure
[ERROR] /repo/eraonel/git/java-runtime-stats/src/test/java/com/company/commonlibrary/javaruntimestats/aspects/DeprecatedMethodsAspectTest.java:[17,13] cannot find symbol
[ERROR] symbol:   class DeprecatedMethodsAspect
[ERROR] location: class com.company.commonlibrary.javaruntimestats.aspects.DeprecatedMethodsAspectTest
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

What am I missing here? Why is the aspectj not compiled before the test case? Is there a way to change this?

I used this example ( for the library part) to follow since I am creating a lib that should be used for other applications:

AspectJ: How to weave an aspect library into a Java project

TestClass:

/**
 * Unit test to see if pointcut works as expected in ${@link DeprecatedMethodsAspect}
 */
public class DeprecatedMethodsAspectTest {

    private DeprecatedMethodsAspect aspect;
    private DeprecatedMethods deprecatedMethodsMock;
    private DeprecatedMethodsApp app;

    @BeforeClass
    public void setUp() throws Exception {
        app = new DeprecatedMethodsApp();
        deprecatedMethodsMock = mock(DeprecatedMethods.class);
        when(deprecatedMethodsMock.isActive()).thenReturn(true);
        aspect = Aspects.aspectOf(DeprecatedMethodsAspect.class);
        aspect.setDeprecatedMethods(deprecatedMethodsMock);

    }

    @Test
    public void testSumIsMatched() throws Throwable {
        app.sum(1, 2);
        verify(deprecatedMethodsMock, times(1)).collect(any(JoinPoint.class));

    }

    @Test(description = " we should not gather information from methods annotated @Beta.")
    public void testSubIsNotMatched() throws Throwable {
        app.sub(2, 1);
        verify(deprecatedMethodsMock, times(0)).collect(any(JoinPoint.class));

    }

}

This is excerpt from my pom.xml

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Compile scope dependencies -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <!-- IMPORTANT -->
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>${java.version}</version>
                    <configuration>
                        <showWeaveInfo>true</showWeaveInfo>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.version}</complianceLevel>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <!--<verbose>true</verbose> -->
                        <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn> -->
                    </configuration>
                    <executions>
                        <execution>
                            <!-- IMPORTANT -->
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

Answer:

You've included the aspectj-maven-plugin in <pluginManagement/> but you haven't included it in <plugins>

Try adding:

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

under the <build/> element.

See also: Maven: What is pluginManagement?

Question:

I'm having troubles compiling a project with Maven and AspectJ.

This the POM excerpt for aspectj-maven-plugin (taken from effective POM):

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>default-aj-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-core</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
        <execution>
            <id>default-aj-test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-core</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-test</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
        <execution>
            <id>default-aj-generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
        <execution>
            <id>default-aj-generate-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.jcz</groupId>
                        <artifactId>jcz-persistence</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <showWeaveInfo>true</showWeaveInfo>
                <verbose>true</verbose>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <aspectDirectory>src/main/aspects</aspectDirectory>
                <testAspectDirectory>src/test/aspects</testAspectDirectory>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.8.5</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <configuration>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <forceAjcCompile>true</forceAjcCompile>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <aspectDirectory>src/main/aspects</aspectDirectory>
        <testAspectDirectory>src/test/aspects</testAspectDirectory>
    </configuration>
</plugin>

When launching an mvn clean deploy I got these lines:

[INFO] --- aspectj-maven-plugin:1.7:test-compile (default-aj-generate-test-sources) @ jcz-persistence-jpa ---
[INFO] 'org.jcz.persistence.jpa.model.Band' (Band.java:45) is annotated with @Entity type annotation from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:79)
[INFO] 'org.jcz.persistence.jpa.model.Band' (Band.java:45) is annotated with @UUID type annotation from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:80)
[INFO] Extending interface set for type 'org.jcz.persistence.jpa.model.Band' (Band.java) to include 'org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID' (UUIDSupportAspect.aj)
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'boolean org.jcz.persistence.EntitySupport.equals(java.lang.Object)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.lang.String org.jcz.persistence.EntitySupport.toSuperString()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.lang.String org.jcz.persistence.EntitySupport.toString()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'org.jcz.persistence.UniqueIdentifier org.jcz.persistence.EntitySupport.uniqueIdentifier()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.util.Comparator<java.lang.reflect.Method> org.jcz.persistence.EntitySupport.retrieveComparatorForAction(java.lang.Class<? extends java.lang.annotation.Annotation>)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'java.lang.String org.jcz.persistence.EntitySupport.retrieveDescriptionForAction(java.lang.annotation.Annotation)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.invokeActions(java.lang.Class<? extends java.lang.annotation.Annotation>)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPrePersist()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPostPersist()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPreUpate()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPostUpdate()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPreRemove()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.EntitySupportAspect' (EntitySupportAspect.aj:'void org.jcz.persistence.EntitySupport.doPostRemove()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped field from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'java.lang.String org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.id')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'java.io.Serializable org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.getId()')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'void org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.setId(java.io.Serializable)')
[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'void org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.ensureId()')

Just to reduce the scope to a single case:

[INFO] Type 'org.jcz.persistence.jpa.model.Band' (Band.java) has intertyped method from 'org.jcz.aspects.persistence.UUIDSupportAspect' (UUIDSupportAspect.aj:'java.io.Serializable org.jcz.aspects.persistence.UUIDSupportAspect$WithUUID.getId()')

So, it looks like the method getId() is correctly injected into the class Band. But when Maven proceeds to tests compilation and unit testing:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project jcz-persistence-jpa: Compilation failure: Compilation failure:

and, amongst other error details, I got

[ERROR] /home/stefano/projects/java/jcz-persistence-jpa/src/test/java/org/jcz/persistence/jpa/ServiceTestCase.java:[182,45] cannot find symbol
[ERROR] symbol:   method getId()
[ERROR] location: variable persisted of type org.jcz.persistence.jpa.model.Band
[ERROR] /home/stefano/projects/java/jcz-persistence-jpa/src/test/java/org/jcz/persistence/jpa/ServiceTestCase.java:[182,60]

as if all my intertype declared stuff weren't injected, after all.

To check if there was some error in the code, I tried running the JUnit tests from within Eclipse, and everything worked fine.

So I guess there is something in the maven compilation process that I didn't set up correctly.

Can anyone help me understand what is causing the problem?


Answer:

Finally came up with a solution. Leaving track here.

What I did to solve the issue was configuring the maven-compiler-plugin like that:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
        <source>1.${java.version}</source>
        <target>1.${java.version}</target>
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>

where the most important part is <useIncrementalCompilation>false</useIncrementalCompilation>