How to override ant task stored in ant lib directo

2020-07-18 08:57发布

问题:

At my work we use AspectJ in some of our Java projects. To get this to work with ant builds we have been placing aspectjtools.jar within ant/lib/.

I am now working on a particular Java project and need to use a newer version of aspectJ. I don't want to have to get everyone who uses the project to update their local copy of aspectjtools.jar. Instead, I tried adding the newer aspectjtools.jar to the lib directory of the project and adding the following line to build.xml.

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

However, this doesn't work as I hoped as the ANT classloader loads jars from ant/lib/ in preference to the jar I specify in the taskdef classpath.

Is there any way to force ant to pick the jar checked into my project instead?

回答1:

can't you just update the iajc compile target to use the new jar on the classpath?

It's not possible to force the classloader to prefer a given jar file over another one. If you must relate to several version of the same class, then you should consider OSGI.

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

An 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 also have to use another Ant. If you're using Eclipse, try the bundled one directly from the Ant view.

You also have another option, but it's a little bit more difficult. That is to use AspectJ load-time weaving instead. If you go for that option, you can compile with an ordinary compile task, but you have to do the weaving with an JVM agent at startup. You can read more about it here.

I hope this helps!



回答2:

The ultimate answer to my question is no. The is currently no way for ANT to use a task jar from a project in preference to a task jar in the ANT lib directory.



回答3:

This is possible. You need to make use of this extra 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"/>