将AspectJ与NetBeans平台开发集成

您是否正在使用NetBeans平台开发项目? 您愿意使用AspectJ来使用AOP吗? 您不知道如何将AspectJ编译器集成到NetBeans的内部版本中?

如果您的回答是“是”,则此帖子适合您。

我决定写这篇技术文章,是因为我在寻找该解决方案之前一直很费力,所以我想分享一下。

讲故事

以前,我不得不面对将AspectJ集成到使用NetBeans Platform源代码实现的Rich Client Application中的问题。 做出的第一个决定是在编译级别集成AspectJ,以便创建包含AOP的已编译源代码。

主要问题是,如何将此后编译与netbeans ant编译文件集成在一起。

解决方案

首先让我们确定必须修改的文件:

  1. common.xml:它位于您的NetBeans安装的线束文件夹中
  2. project.properties:包含必须用AspectJ编译的源代码的模块的

步骤1

下载AspectJ库,并将它们放在NetBeans安装的“ harness”文件夹内的文件夹中。 假设此文件夹名为:aspectj-xxx / lib。

第2步

进入包含要与AspectJ一起编译的源代码的模块,并在其project.properties文件(在重要文件中)中添加以下行:

aspectjcompiler=required

第三步

现在是时候配置common.xml文件了。 启动构建操作时,NetBeans IDE调用的ant目标实际上位于此位置。

该示例是使用NetBeans 7.3.1完成的,但对于以前或将来的更改差异很小。 修改以蓝色突出显示。

更改目标编译,如下所示:

<target name=”compile-nb-javac” depends=”init,up-to-date” unless=”is.jar.uptodate”>
<mkdir dir=”${build.classes.dir}”/>
<depend srcdir=”${src.dir}” destdir=”${build.classes.dir}” cache=”${build.dir}/depcache”>
<classpath refid=”cp”/>
</depend>
<nb-javac srcdir=”${src.dir}” destdir=”${build.classes.dir}” debug=”${build.compiler.debug}” debuglevel=”${build.compiler.debuglevel}” encoding=”UTF-8″
deprecation=”${build.compiler.deprecation}” optimize=”${build.compiler.optimize}” source=”${javac.source}” target=”${javac.target}” includeantruntime=”false”>
<classpath refid=”cp”/>
<compilerarg line=”${javac.compilerargs}”/>
<processorpath refid=”processor.cp”/>
</nb-javac>
<copy todir=”${build.classes.dir}”>
<fileset dir=”${src.dir}” excludes=”${jar-excludes}”/>
</copy>
</target>

添加一个新的目标编译 ,如下所示:

<target name=”compile” depends=”init,up-to-date” unless=”is.jar.uptodate”>
<mkdir dir=”${build.classes.dir}”/>
<depend srcdir=”${src.dir}” destdir=”${build.classes.dir}” cache=”build/depcache”>
<classpath refid=”cp”/>
</depend>
<antcall target=”compile-nb-javac” inheritAll=”true” />
<antcall target=”compile-aspectj” inheritAll=”true” />
</target>

添加新的目标compile-aspectj,如下所示:

<target name=”compile-aspectj” depends=”init,up-to-date” unless=”is.jar.uptodate” if=”aspectjcompiler”>
<property name=”cpProperty” refid=”cp”/>
<property name=”aspectj.lib.dir” location=”${harness.dir}/aspectj-x.x.x/lib”/>
<property name=”aspectjtools.jar” location=”${aspectj.lib.dir}/aspectjtools.jar”/>
<property name=”aspectjrt.jar” location=”${aspectj.lib.dir}/aspectjrt.jar”/>
<taskdef resource=”org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties”>
<classpath>
<pathelement path=”${aspectjtools.jar}”/>
</classpath>
</taskdef>
<iajc destdir=”${build.classes.dir}” source=”${javac.source}” fork=”true”
forkclasspath=”${aspectjtools.jar}” classpath=”${aspectjrt.jar};${cpProperty}”
failonerror=”false” >
<sourceroots>
<pathelement location=”${src.dir}”/>
</sourceroots>
</iajc>
</target>

结论

现在,当您进行清理和构建时,您会看到所选模块的源代码在正常编译之后立即针对AspectJ编译器进行了编译。

您需要确保的是,方面和必须使用其进行编译的源代码都在同一模块中。

参考:Refactoring Ideas博客上,我们的JCG合作伙伴 Marco Di Stefano 将AspectJ与NetBeans平台开发集成

翻译自: https://www.javacodegeeks.com/2013/10/integrate-aspectj-with-netbeans-platform-development.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值