Search code examples
javaaopaspectjaspectaspectj-maven-plugin

AspectJ does not runs without -javaagent argument


I'm trying to use AspectJ in a simple java app, without using of Spring. project is controlled by maven.

Here is the project code, you can see it also on a GitHub

App.java

package ge.jibo.aspectj;

public class App {
    public static void main(String[] args) {
        App app = new App();
        app.print("Message from App object...");
    }

    public void print(String value) {
        System.out.println(value);
    }
}

LoggerAspect.java

package ge.jibo.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class LoggerAspect {

    @Around("execution(!static * *(..))")
    public Object dontLogDuplicates(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        System.out.println("Message from LoggerAspect object");
        return thisJoinPoint.proceed();
    }
}

aop.xml in a resources\META-INF directory

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <aspects>
        <aspect name="ge.jibo.aspectj.LoggerAspect"/>
    </aspects>
    <weaver options="-verbose -showWeaveInfo">
        <include within="ge.jibo.aspectj..*"/>
    </weaver>
</aspectj>

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>ge.jibo.aspectj</groupId>
    <artifactId>aspectj-demo</artifactId>
    <version>1.0</version>

    <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>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.6</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <mainClass>ge.jibo.aspectj.App</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

after mvn install JAR file is generated, than I'm trying to execute JAR using the java -jar aspectj-demo.jar command, but only App object is executed, LoggerAspect does not prints anything.

java -jar aspectj-demo.jar

If I pass -javaagent argument with aspectjweaver-1.9.6.jar to JVM, then Aspect object is executed. enter image description here

  • How to avoid of passing this -javaagent argument on each time?
  • Should I add some plugin in pom.xml to execute aspect directly?

Solution

  • A simple solution/workaround was to add aspectj-maven-plugin for compile-time weaving(in this case aop.xml file is not needed anymore)

    and package projects with its dependencies in to executable jar using maven-assembly-plugin (there are other ways also to package executable jars with libs/dependencies)

    pom.xml

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <verbose>true</verbose>
                    <complianceLevel>1.8</complianceLevel>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>${maven.compiler.source}</source>
                            <target>${maven.compiler.target}</target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        ge.jibo.aspectj.App
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    execution of mvn install generates additional jar file aspectj-demo-jar-with-dependencies.jar

    java -jar aspectj-demo-jar-with-dependencies.jar enter image description here