AspectJ (annotations) compile time with Ant and NetBeans

I want to use AspectJ to compile with Ant in NetBeans . I want to run it in the Google App Engine, but at the moment this is not significant. AspectJ is based on annotations.
I prefer compilation over time (modification, class toolkit). I would not want to use a custom class loader. How to achieve this?

What I already have:

I tried the AspectJ Annotation Tutorial with NetBeans. I modified build.xml to handle aspectj (using iajc Ant task ) as described. The problem is that this requires adding -javaagent:lib/aspectjweaver.jar (this is not possible in GAE).
Running my build generates this output:

 info compiling C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by before advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:17(from BasicAspect.java)) weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by after advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:23(from BasicAspect.java)) info woven class net.andrewewhite.examples.HelloWorld (from C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java) info Compiler took 2547ms 

When I run the project with the -javaagent , it works fine. (In NetBeans: click on the project> Properties> Run> VM Settings: -javaagent: ./ dist / lib / aspectjweaver.jar). Leave for code from tutorial :

 run: About to make call to print Hello World Hello World! Just made call to print Hello World BUILD SUCCESSFUL (total time: 0 seconds) 

Without an agent (cleared VM parameters), the code works as if it were without AspectJ:

 run: Hello World! BUILD SUCCESSFUL (total time: 0 seconds) 

Sources:

\ TryAspectJ \ SRC \ META-INF \ aop.xml

 <?xml version="1.0" encoding="UTF-8"?> <aspectj> <aspects> <aspect name="net.andrewewhite.aspects.BasicAspect" /> </aspects> </aspectj> 

\ TryAspectJ \ src \ net \ andrewewhite \ aspects \ BasicAspect.java

 package net.andrewewhite.aspects; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class BasicAspect { @Before(" call(void java.io.PrintStream.println(java.lang.String)) " + "&& !within(net.andrewewhite.aspects..*)") public void beforePrintlnCall() { System.out.println("About to make call to print Hello World"); } @After(" call(void java.io.PrintStream.println(java.lang.String)) " + "&& !within(net.andrewewhite.aspects..*)") public void afterPrintlnCall() { System.out.println("Just made call to print Hello World"); } } 

\ TryAspectJ \ SRC \ mesh \ andrewewhite \ Examples \ HelloWorld.java

 package net.andrewewhite.examples; public class HelloWorld { public static void main(String[] argv) { System.out.println("Hello World!"); } } 

\ TryAspectJ \ build.xml

 <?xml version="1.0" encoding="UTF-8"?> <project name="TryAspectJ" default="default" basedir="."> <description>Builds, tests, and runs the project TryAspectJ.</description> <import file="nbproject/build-impl.xml"/> <taskdef classpath="lib/aspectj/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/> <target name="aspectj"> <echo level="info">--- aspectj (start) ---</echo> <iajc destDir="${build.classes.dir}" source="1.6" target="1.6" showweaveinfo="true" verbose="true" > <inpath> <pathelement location="lib/aspectj/aspectjrt.jar"/> <pathelement location="${build.classes.dir}" /> </inpath> <sourceroots> <pathelement location="${src.dir}"/> </sourceroots> <classpath> <pathelement location="${javac.classpath}"/> <pathelement location="${j2ee.platform.classpath}"/> </classpath> </iajc> <echo level="info">--- aspectj finished ---</echo> </target> <target name="-post-compile" depends="aspectj"></target> </project> 

What do I need to add or change?

+6
source share
2 answers

Found a solution. This is the correct build.xml fragment:

 <taskdef classpath="lib/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/> <target name="aspectj"> <echo level="info">--- aspectj (start) ---</echo> <!-- begin fix classpath for this bug https://issues.apache.org/bugzilla/show_bug.cgi?id=40291 --> <condition property="targetos" value="windows" else="unix"> <os family="windows"/> </condition> <!-- converting classpath --> <pathconvert targetos="${targetos}" property="javac.convertedClasspath" > <path path="${javac.classpath}" /> </pathconvert> <!-- end fix classpath --> <iajc source="1.6" target="1.6" showweaveinfo="true" verbose="true" destdir="${build.classes.dir}" > <inpath> <pathelement location="${build.classes.dir}"/> </inpath> <classpath> <pathelement location="${javac.convertedClasspath}" /> </classpath> </iajc> <echo level="info">--- aspectj (finished) ---</echo> </target> <target name="-post-compile" depends="aspectj"></target> 

Calling java -classpath ./lib/aspectjrt.jar -jar TryAspectJ.jar works fine. The strange thing is that from NetBeans (Right clik on project-> Run) the results look as if they were without AspectJ. I think NetBeans does not run projects from build/classes dir as jar. But it's not a problem.

+4
source

You need to add the aspect path element as a subitem to the iajc element.

 <aspectpath> <pathelement location="..." /> </aspectpath> 

The aop.xml file is only needed for weaving AspectJ.

0
source

Source: https://habr.com/ru/post/892672/


All Articles