4 Enhancer
Managed classes must be enhanced after compilation to be managed by EntityManager. The syntax
of invoking Enhancer is:
java -classpath classpath com.cmobilecom.jpa.processor.Enhancer
-d outputDir -persistenceXmlClasspath persistence_xml_classpath
class_root_directory1 class_root_directory2 ...
- -classpath: the cmobilecom-jpa-processor.jar and its dependencies must be in
the classpath. If managed classes of a class hierarchy are enhanced separately,
superclasses should be enhanced before subclasses. The directory or jar where
superclasses are must be added to the classpath when subclasses are enhanced.
To enable logging, add a SLF4J logging provider to the classpath.
- -d outputDir: (optional)where to put enhanced classes. default: override
original classes.
- -persistenceXmlClasspath: the classpath to load META-INF/persistence.xml and
referenced XML mapping files.
- class_root_directories: one or more root directories under which managed classes
to be enhanced.
Enhance managed classes after they are compiled.
Java SE/Jakarta EE
Example: enhance managed classes in the source set "test".
configurations {
processor
}
dependencies {
// annotation processor: generate static metamodel
processor 'com.cmobilecom:cmobilecom-jpa-processor:2.2.2'
}
// StaticMetamodelGenerator/Enhancer: classpath for loading META-INF/persistence.xml and
// referenced orm mapping xml files.
def persistenceXmlClasspath = files('src/test/resources').asPath;
compileTestJava {
// annotation processor: generate static metamodel source code which will be
// added into sourceSet.java.srcDirs
options.annotationProcessorPath = configurations.processor
options.compilerArgs << '-ApersistenceXmlClasspath=' + persistenceXmlClasspath
doLast {
javaexec {
classpath = configurations.processor
main 'com.cmobilecom.jpa.processor.Enhancer'
args "-persistenceXmlClasspath", persistenceXmlClasspath
args sourceSets.test.output.classesDirs.singleFile.absolutePath
}
}
}
Android
Example: enhance managed classes in the source set "androidTest".
configurations {
processor
}
dependencies {
// annotation processor(static metamodel generator) and enhancer
processor 'com.cmobilecom:cmobilecom-jpa-processor:2.2.2'
// Android does not have javax.annotation that is required to compile generated sources,
// but it is not needed at runtime.
compileOnly group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
annotationProcessor configurations.processor
}
afterEvaluate {
// StaticMetamodelGenerator/Enhancer: classpath for loading META-INF/persistence.xml and
// referenced orm mapping xml files.
def persistenceXmlClasspath = files('src/androidTest/resources').asPath;
compileDebugAndroidTestJavaWithJavac {
options.compilerArgs << '-ApersistenceXmlClasspath=' + persistenceXmlClasspath
doLast {
javaexec {
classpath = configurations.processor
main 'com.cmobilecom.jpa.processor.Enhancer'
args "-persistenceXmlClasspath", persistenceXmlClasspath
args compileDebugAndroidTestJavaWithJavac.destinationDir
}
}
}
}
The enhancer transforms classes (not jar) at build time. Do not add cmobilecom-jpa-processor.jar
in runtime classpath. Added member (field or method) names have prefix "_jpa_". Keep those names
if code is obfuscated (e.g., proguard).
Kotlin DSL
val processor by configurations.creating
dependencies {
processor("com.cmobilecom:cmobilecom-jpa-processor:2.2.2")
// annotation processor: generate static metamodel
// Android does not have javax.annotation that is required to compile generated sources
// but it is not needed runtime.
compileOnly("javax.annotation:javax.annotation-api:1.3.2")
annotationProcessor(processor)
}
afterEvaluate {
// static metamodel generation and enhancement: classpath for loading META-INF/persistence.xml
// and referenced orm mapping xml files.
val persistenceXmlClasspath = files(android.sourceSets["main"].resources.srcDirs).asPath
val compileDebugJavaWithJavac = tasks.named<JavaCompile>("compileDebugJavaWithJavac");
compileDebugJavaWithJavac {
options.compilerArgs.add("-ApersistenceXmlClasspath=" + persistenceXmlClasspath)
doLast {
javaexec {
classpath = processor
main = "com.cmobilecom.jpa.processor.Enhancer"
args("-persistenceXmlClasspath", persistenceXmlClasspath)
args(compileDebugJavaWithJavac.get().destinationDir)
}
}
}
}
See build scripts that put together compile dependencies, runtime dependencies, static metamodel
generation and managed class enhancement.