使用DataNucleus ant插件脚本enhance已经编译后的Classes

  DataNucleus 支持 maven 与 ant 去 enhance Java 字节码文件,

  如果对这方面不太理解的同学可以去看看 enhance的概念: http://www.datanucleus.org/products/accessplatform/jdo/enhancer.html

  本次我们使用ant脚本并使用JDO去enhance classes:

  首先在自己工程下的lib中添加以下包:

  

  说明一下,core是datanucleus的核心包,datanucleus的自定义ant任务也在里面,

       api-jdo是datanucleus支持JDO规范的包,jdo-API就是JDO规范的包

         log4j主要用于日志输出,可以没有。

  然后我们在src目录下添加一个需要持久化的实体类

package ztj;

import javax.jdo.annotations.Column;
import javax.jdo.annotations.PersistenceCapable;

@PersistenceCapable(table = "entity")
public class Entity {
    @Column(length = 25)
    private String name;
    @Column(length = 25)
    private String mobile;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

}

 

这个类非常简单,我们使用JDO的注解进行标识,而不去以写配置文件的方式,@PersistenceCapable表明此类可持续化,表名为entity

接下来我们去写ant脚本,我先直接贴出来:

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="enhance" basedir=".">
    <target name="prepare">
        <delete dir="build" />
        <mkdir dir="build" />
    </target>
    <!-- datanucleus enhancer 需要的classPath -->
    <path id="enhancer.path">
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement location="build"/>
    </path>
    <!-- compile需要的classPath -->
    <path id="compile.path">
        <filelist dir="lib">
            <file name="jdo-api-3.1.jar" />
        </filelist>
    </path>

    <target name="compile" depends="prepare">
        <javac srcdir="src" destdir="build" classpathref="compile.path" encoding="UTF-8">
        </javac>
    </target>

    <target name="enhance" depends="compile">
        <taskdef name="enhancer" classpathref="enhancer.path" classname="org.datanucleus.enhancer.EnhancerTask">
        </taskdef>
        <!-- enhancer -->
        <enhancer classpathref="enhancer.path" dir="build" verbose="true">
            <fileset dir="build">
                <include name="/**/*.class" />
            </fileset>
            <sysproperty key="log4j.configuration" value="file:log4j.properties" />
        </enhancer>
    </target>
</project>

这个ant脚本我写的非常简单,要注意的其实就只有2点:

1对于enhancer任务的classpath一定是要包括要增强的字节码目录进去,否则不会进行增强

2enhancer任务中需要指定要扫描增强的文件,我的实现方式是写Fileset中去匹配所有class为后缀的文件,更简单的方法是写filesuffixes属性为class,会去自动扫描classPath中的class文件:

<enhancer classpathref="enhancer.path" filesuffixes="class" dir="build" verbose="true">
            <sysproperty key="log4j.configuration" value="file:log4j.properties" />
        </enhancer>

最后的输出为:

我这个写的非常简单,还是建议看看官方的文档,在上面我给出的地址中,也有关于ant的使用说明,官方还有各种demo提供

gitHub地址:https://github.com/datanucleus/samples-jdo

 

转载于:https://www.cnblogs.com/zhangtianji/p/5664962.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值