You can enhance your classes at build time. The main use for the enhancement process is to add the activate interface implementation to the persisted classes. A load time enhancement example is available here.
This topic explains the individual elements of the ant task.
For transparent activation/persistence you need following dependencies at compile time. (see also the dependency overview)
This is an example script. Below it each part of it is campaigned and the alternatives to it.
<target name="enhance"> <!-- Change these according to your project --> <property name="target" value="./target/classes/"/> <property name="libraries" value="./lib/"/> <path id="project.classpath"> <pathelement path="${target}"/> <fileset dir="${libraries}"> <include name="*.jar"/> </fileset> </path> <!-- We enhance with an additional Ant-run step. You can put this also in an extra file --> <typedef resource="instrumentation-def.properties" classpathref="project.classpath" loaderRef="instrumentation.loader"/> <!-- We filter by our annotation --> <typedef name="annotation-filter" classname="com.db4odoc.tp.enhancement.AnnotationFilter" classpathref="project.classpath" loaderRef="instrumentation.loader"/> <db4o-instrument classTargetDir="${target}" verbose="true"> <classpath refid="project.classpath"/> <sources dir="${target}"> <include name="**/*.class"/> </sources> <transparent-activation-step> <annotation-filter/> </transparent-activation-step> </db4o-instrument> </target>
Tag/Property | Explanation |
---|---|
property target | Specifies to which classes the enhancement is applied. |
property libraries | Specifies where the libraries are. This is used for setting up the classpath of the enhancement step. The enhancement should have access to all classes, otherwise class not found exceptions might occur. |
path project.classpath | This represents the whole project classpath: Your code and all libraries. This classpath is passed to the enhancer step. |
typedef instrumentation-def.properties | Loads the predefined tasks from the db4o jars. Make sure that all required db4o dependencies are on the classpath. |
typedef annotation-filter |
This defines a regular Java class as part of the Ant script. This way you can implement your own filter. The name attribute specifies how the task is named within the Ant script. |
db4o-instrument | The db4o-instrument task, which can do all available instrumentations. In the attribute 'classTargetDir' you specify where to put the enhanced result. |
db4o-instrument->classpath | Here we need to specify the classpath for the enhancer-step. Refer to classpath which includes the application code and the libraries. |
db4o-instrument->sources | Specifies which class-files are enhanced |
transparent-activation-step |
This tag specifies that the transparent activation/persistence enhancement is applied. This tag expects that you specify one more filters to filter out the classes which need to be enhance. For the example filter take a look at the example. |
native-query-step | You also can specify the native query enhancing step. Usually this is done at runtime, but can be done a compile time. Just add it the the db4o-instrument tag. |
As alternative quick way you can use the 'db4o-enhance'-task. There you just can specify the location of the class files which you want to enhance. It works best when you put all your persistent classes in a separate package or project.
<target name="simpleEnhanceStep"> <db4o-enhance classtargetdir="${basedir}/bin" jartargetdir="../libs/java/" nq="false" ta="true" collections="true" verbose="true"> <classpath refid="project.classpath"/> <sources dir="${basedir}/bin"> <include name="com/db4odoc/tp/**/*.class"/> </sources> </db4o-enhance> </target>
As demonstrated in the example you can include a enhancing Ant script in your Maven build. All descriptions above applies. However you can use the built in Maven classpath. Just use the 'maven.compile.classpath' in the Ant script.