如何覆盖存储在 ant lib 目录中的 ant 任务

发布于 2024-09-01 01:04:21 字数 515 浏览 2 评论 0原文

在我的工作中,我们在一些 Java 项目中使用 AspectJ。为了使其能够与 ant 构建一起使用,我们将aspectjtools.jar 放置在ant/lib/ 中。

我现在正在开发一个特定的 Java 项目,需要使用更新版本的aspectJ。我不想让每个使用该项目的人都更新他们的本地副本aspectjtools.jar。相反,我尝试将较新的aspectjtools.jar 添加到项目的lib 目录中,并将以下行添加到build.xml 中。

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

然而,这并没有像我希望的那样工作,因为 ANT 类加载器从 ant/lib/ 加载 jar,而不是我在 taskdef 类路径中指定的 jar。

有没有什么办法可以强制蚂蚁选择签入我的项目的罐子?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凯凯我们等你回来 2024-09-08 01:04:21

难道你不能更新 iajc 编译目标以在类路径上使用新的 jar 吗?

不可能强制类加载器优先选择给定的 jar 文件而不是另一个文件。如果您必须涉及同一类的多个版本,那么您应该考虑 OSGI。

最简单的解决方案是仅使用项目或 Maven/Ivy 存储库中的库,并忽略全局 ant 文件夹中的库。

示例:

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

更新:
您还必须使用另一个 Ant。如果您使用的是 Eclipse,请直接从 Ant 视图尝试捆绑的版本。

您还有另一种选择,但它有点困难。即使用 AspectJ 加载时编织来代替。如果您选择该选项,则可以使用普通编译任务进行编译,但必须在启动时使用 JVM 代理进行编织。您可以阅读有关它的更多信息 此处

我希望这有帮助!

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!

Saygoodbye 2024-09-08 01:04:21

我的问题的最终答案是否定的。目前,ANT 无法优先使用项目中的任务 jar 而不是 ANT lib 目录中的任务 jar。

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.

最终幸福 2024-09-08 01:04:21

这是可能的。
您需要利用这个额外的任务来创建一个新的类加载器

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

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"/>
吃颗糖壮壮胆 2024-09-08 01:04:21

我在项目中使用 Spotbugs 时遇到了类似的问题,同时仍然在 ant/lib 中全局安装了 findbugs-ant.jar。我想找到一种方法来完成这项工作,而不必从 ant 安装中删除 findbugs jar(其他项目分支仍在使用)。

虽然我没有发现任何可能在内部 build.xml 声明此内容,但 ant 的 -lib 命令行参数却成功了:

来自 ant 文档:

可以使用 -lib 选项添加要搜索的其他目录。 -lib 选项指定搜索路径。该路径目录中的任何 jar 或类都将添加到 Ant 的类加载器中。 jar 添加到类路径的顺序如下:

  • 按照命令行上 -lib 选项指定的顺序生成 jar
  • 来自 ${user.home}/.ant/lib 的 jar(除非设置了 -nouserlib)
  • 来自 ANT_HOME/lib 的 jar

因此,通过在 -lib 参数中指定我的本地 Spotbugs 安装,我能够运行 ant 构建并使 ant 的类加载器更喜欢这些 Spotbugs jar 文件,而不是 ANT_HOME/lib 中的旧 findbugs jar 文件。

蚂蚁呼叫示例:
ant -lib my/local/path/to/spotbugs/lib Spotbugs

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文