I am trying to compile a very trivial Helloworld java program with AspectJ using Maven. Even though it works within eclipse, when I create a jar file and try to run from command line, the aspect advices are not executed at all.
Here is my java program:
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
HelloWorld world = new HelloWorld();
world.greet();
System.out.println("Hello you stupid world");
}
private void greet() {
System.out.println("Hello foolish world!!!");
}
}
My aspect file: LogAspect.aj
package hello;
public aspect LogAspect {
pointcut abc() : execution (* hello.HelloWorld.main(..));
before() : abc() {
System.out.println("Aspect:Executing HelloWorld method...");
}
after() : abc() {
System.out.println("Aspect:Finished executing a Helloworld method.");
}
}
This is my pom.xml file:
<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.comviva.hell</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello</name>
<description>world</description>
<packaging>jar</packaging>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
<configuration>
<!-- <complianceLevel>1.7</complianceLevel> -->
<source>1.7</source>
<target>1.7</target>
</configuration>
<!-- IMPORTANT -->
<phase>process-sources</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>hello.HelloWorld</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>hello.HelloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.4</version>
<!-- <scope>compile</scope> -->
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
</dependencies>
This is the output when running through Eclipse (which is correct)
Aspect:Executing HelloWorld method...
Hello foolish world!!!
Hello you stupid world
Aspect:Finished executing a Helloworld method.
However, when I build jar using maven and run the resulting jar, I don't get the aspect advice outputs at all.
D:\projects\hell\target>java -jar hello-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Hello foolish world!!!
Hello you stupid world
D:\projects\hell\target>
Thanks to anyone helping me out on this.
EDIT: After trying suggestion to move maven-plugin out of
Now, I have removed the tags. But I get the error as shown below.