How to override ant task stored in ant lib directory

In my work, we use AspectJ in some of our Java projects. To make this work with ant builds, we place aspectjtools.jar inside ant / lib /.

Now I am working on a specific Java project and should use a newer version of aspectJ. I do not want everyone using the project to update their local copy of aspectjtools.jar. Instead, I tried to add a new aspectjtools.jar to the lib directory of the project and add the following line to build.xml.

<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties" classpath="./lib/aspectjtools.jar" /> 

However, this does not work, as I had hoped, since the ant class loader loads the banks from ant / lib / in preference to the jar, which I specify in the class path of the taskdef.

Is there a way to get ant to select the checkbox checked in my project?

+4
source share
3 answers

The final answer to my question is no. There is currently no way to use ANT to use the jar task from the project, preferably in the java task directory in the ANT lib directory.

+3
source

Can't you just update the iajc compilation target to use the new jar in the classpath?

It is not possible to make classloader prefer a given jar file over another. If you need to apply to multiple versions of the same class, you should consider OSGI.

The simplest solution would be to simply use the libraries from a project or Maven / Ivy repository and ignore the libraries in the global ant folder.

Example:

  <taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"> <classpath> <pathelement location="${basedir.dir}/lib/aspectjtools.jar"/> </classpath> </taskdef> <target name="compile" > <iajc outjar="demo.jar"> <sourceroots> <pathelement location="src" /> </sourceroots> <aspectpath> <pathelement location="aspects_to_be_weaved_with_classes_in_sourceroots.jar" /> </aspectpath> <classpath> <pathelement location="${basedir}/lib/aspectjrt.jar"/> </classpath> </iajc> </target> 

Updated: You must also use a different Ant. If you are using Eclipse, try linking it directly from the ant view.

You also have another option, but it's a little trickier. This means that you should use AspectJ weaving instead. If you move on to this option, you can compile it with a normal compilation task, but at startup you need to do the binding using the JVM agent. You can learn more about this here .

Hope this helps!

+3
source

It is possible. You must use this additional task to create a new class loader.

http://enitsys.sourceforge.net/ant-classloadertask/

  <!--Append the dependent jars to the classpath so we don't have to install them in the ant directory--> <taskdef resource="net/jtools/classloadertask/antlib.xml" classpath="${basedir.dir}/lib/ant-classloadertask.jar"/> <classloader loader="ant.aspectj.loader" parentloader="project"> <classpath> <pathelement location="${basedir.dir}/lib/aspectjtools.jar"/> <pathelement location="${basedir.dir}/lib/ant-classloadertask.jar"/> </classpath> <antparameters parentfirst="false"/> <handler loader="org.apache.tools.ant.AntClassLoader" adapter="org.apache.tools.ant.taskdefs.classloader.adapter.AntClassLoaderAdapter"/> </classloader> <taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties" classpath="${basedir.dir}/lib/aspectjtools.jar"" loaderref="ant.aspectj.loader"/> 
+1
source

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


All Articles