java - How to tell maven to compile a project as an aspectj project -
my project:
src/main/aspects
:
com |---badmitrii |---trace.aj
src/main/java
:
com |---badmitrii |---app.java
i wrote following pom:
<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/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.badmitrii</groupid> <artifactid>aspectj-test</artifactid> <packaging>jar</packaging> <version>1.0-snapshot</version> <name>aspectj-test</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjrt</artifactid> <version>1.6.11</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>aspectj-maven-plugin</artifactid> <configuration> <aspectlibraries> <aspectlibrary> <groupid>com.badmitrii</groupid> <artifactid>aspectj-test</artifactid> </aspectlibrary> </aspectlibraries> </configuration> </plugin> <plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>exec-maven-plugin</artifactid> <version>1.2.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainclass>com.badmitrii.app</mainclass> </configuration> </plugin> <plugin> <!-- build executable jar --> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-jar-plugin</artifactid> <version>2.4</version> <configuration> <archive> <manifest> <addclasspath>true</addclasspath> <classpathprefix>lib/</classpathprefix> <mainclass>com.badmitrii.app</mainclass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
but jar still being compiled java-project , when run java -jar <jar-name>
didn't desirable aspectj
result.
how tell maven compile jar respect aspects?
upd:
app.java:
package com.badmitrii;
public class app { public static void main( string[] args ) { app = new app (); a.m(); } public void m(){ system.out.println("test"); } }
trace.aj:
package com.badmitrii.aspects; public aspect trace { pointcut publicmethodexecuted(): execution(public !static * *(..)); after(): publicmethodexecuted() { system.out.printf("enters on method: %s. \n", thisjoinpoint.getsignature()); object[] arguments = thisjoinpoint.getargs(); (int =0; < arguments.length; i++){ object argument = arguments[i]; if (argument != null){ system.out.printf("with argument of type %s , value %s. \n", argument.getclass().tostring(), argument); } } system.out.printf("exits method: %s. \n", thisjoinpoint.getsignature()); } }
try configuring plugin the documentation describes:
in order apply compiled aspects own sources need setup jar files weave in plugin configuration shown below.
which be
<plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>aspectj-maven-plugin</artifactid> <configuration> <aspectlibraries> <aspectlibrary> <groupid>com.badmitrii</groupid> <artifactid>aspectj-test</artifactid> </aspectlibrary> </aspectlibraries> </configuration> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin>
updated after code post:
however, yours not pre-compiled aspect. don't need configuration of aspectlibraries, need goal:
<plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>aspectj-maven-plugin</artifactid> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin>
also, should add version of maven-plugin:
<artifactid>aspectj-maven-plugin</artifactid> <version>1.7</version>
this compile aspect if comment out printf lines (they not written correctly, , beyond scope of question). may test system.out.println("i inside aspect") inside aspect see works.
like:
package com.badmitrii.aspects; public aspect trace { pointcut publicmethodexecuted(): execution(public !static * *(..)); after(): publicmethodexecuted() { // system.out.printf("enters on method: %s. \n", thisjoinpoint.getsignature()); system.out.println("inside aspect - after"); object[] arguments = thisjoinpoint.getargs(); (int =0; < arguments.length; i++){ object argument = arguments[i]; if (argument != null){ // system.out.printf("with argument of type %s , value %s. \n", argument.getclass().tostring(), argument); } } // system.out.printf("exits method: %s. \n", thisjoinpoint.getsignature()); } }
Comments
Post a Comment