'AspectJ - Aspect from external JAR

I've added a github repo that show exactly my problem:

https://github.com/runefist/AspectJ-Stackoverflow-Q


In short, I have a project we'll call it ProjectA. ProjectA is a microprofile-REST-server. Another project, we'll call this ProjectB, is a dependency for ProjectA (and other projects).

  • ProjectA (microprofile-REST-server)
  • ProjectB (dependency of ProjectA)

ProjectB contains an Aspect:

@Aspect
public class ElasticSenderAspect {

    @After("@annotation(elasticsender)") // && execution(* *(..))
    public void after(JoinPoint joinPoint, ElasticSender elasticsender) {
         WebsiteBehaviour websiteBehaviour = new WebsiteBehaviour();  
         websiteBehaviour.setBehaviourFunc(elasticsender.behaviourFunction());
         websiteBehaviour.setBehaviourType(elasticsender.behaviourType());
         ElasticWebsiteBehaviour.sendWebsiteBehaviour(websiteBehaviour);
    }
}

In ProjectA I have a function:

@GET
@Operation(description = "Authenticate")
@ElasticSender(behaviourFunction = "Authenticate")
public Response authenticate(@HeaderParam("authorization") String authString) {
    if (authString == null) {
        return StandardResponseMessages.GENERAL_NO_AUTHENTICATION.getResponse();
    }
    WebAccount webAccount = webAccountService.find(getUsernameFromAuth(authString));
    boolean correct = false;
    //TODO: CHECK IF ACCOUNT IS VERIFIED
    if (webAccount != null) {
        if (webAccount.getUsername() != null && webAccount.getPassword() != null) {
            if (AuthenticatorUtility.basicAuthenticate(webAccount.getUsername(), webAccount.getPassword(), authString)) {
                correct = true;
            }
        }
    }
    if (correct) {
        return Response.ok(generateTokenString(webAccount)).build();
    } else {
        return StandardResponseMessages.GENERAL_WRONG_AUTHENTICATION.getResponse();
    }
}

I've tested the Aspect, which works when I use it in the SAME project, so NOT as dependency.

ProjectA - 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>com.runefist</groupId>
    <artifactId>FeestjesDoen-Server</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <name>FeestjesDoen-Server</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>BendingHeroes-repo</id>
            <url>xxx</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <!-- MAIN DEPENDENCY -->
        <dependency>
            <groupId>com.runefist</groupId>
            <artifactId>WebRest-Utilities</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- MICROPROFILE SWAGGER UI -->
        <dependency>
            <groupId>org.microprofile-ext.openapi-ext</groupId>
            <artifactId>swagger-ui</artifactId>
            <version>1.0.1</version>
            <scope>runtime</scope>
        </dependency>
        <!-- KAFKA NEEDED -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <encoding>UTF-8 </encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

ProjectB - 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>com.runefist</groupId>
    <artifactId>WebRest-Utilities</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- MICROPROFILE REPLACES JAVAEE -->
        <dependency>
            <groupId>org.eclipse.microprofile</groupId>
            <artifactId>microprofile</artifactId>
            <version>2.1</version>
            <type>pom</type>
        </dependency>
        <!-- FOR CREATING JWT TOKENS -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
            <type>jar</type>
        </dependency>
        <!-- FOR THE USE OF HIBERNATE -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.3.1.Final</version>
        </dependency>
        <!-- MYSQL CONNECTOR FOR HIBERNATE -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
            <scope>compile</scope>
        </dependency>
        <!-- FOR JSON CONVERSION -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>
        <!-- ElasticSearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.5.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.5.4</version>
            <type>jar</type>
        </dependency>
        <!-- AspectJ | to add behaviour to methods -->
        <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>
        <!-- Junit - TEMP added to test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                    <verbose>true</verbose>
                    <Xlint>ignore</Xlint>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                             <!--use this goal to weave all your main classes--> 
                            <goal>compile</goal>
                             <!--use this goal to weave all your test classes--> 
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <!-- put your configurations here -->
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Problem:

Aspect happens when tested in ProjectB, aspect does NOT happen when tested in ProjectA. What do I need to add to ProjectA pom to make it work, or what do I need to change to ProjectB pom to make it work?



Solution 1:[1]

In project A you need to add B as an aspect library in the AspectJ Maven configuration in addition to adding it as a Maven dependency as you already did:

            <configuration>
                <!-- (...) -->
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.runefist</groupId>
                        <artifactId>ProjectB</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>

I cloned and tested your project, it works this way.

Solution 2:[2]

I've got same problem and adding spring-aop dependency to projectB helped me.

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

And there is no need to add aspectj plugin in projectA, just

    <dependency>
        <groupId>com.runefist</groupId>
        <artifactId>ProjectB</artifactId>
        <version>1.0.0</version>
    </dependency>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kriegaex
Solution 2