简体   繁体   中英

How to override ant task stored in ant lib directory

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?

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!

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.

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"/>

I just had a similar problem when using Spotbugs in a project while still having findbugs-ant.jar installed globally in ant/lib . I wanted to find a way to get this working without having to delete the findbugs jar from the ant installation (that's still used by other project branches).

While I did not find any possibility to declare this inside build.xml , ant's -lib command line argument did the trick:

From ant documentation:

Additional directories to be searched may be added by using the -lib option. The -lib option specifies a search path. Any jars or classes in the directories of the path will be added to Ant's classloader. The order in which jars are added to the classpath is as follows:

  • jars in the order specified by the -lib options on the command line
  • jars from ${user.home}/.ant/lib (unless -nouserlib is set)
  • jars from ANT_HOME/lib

So by specifying my local spotbugs installation in the -lib argument, I was able to run the ant build and make ant's class loader prefer these spotbugs jar files to the older findbugs jar in ANT_HOME/lib.

example ant call:
ant -lib my/local/path/to/spotbugs/lib spotbugs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM