How to start DataNucleus Enhancer from Gradle?

Is there a gradle plugin to run the DataNucleus Enhancer? As I see from the documentation, you can only run it from Maven or Ant: http://www.datanucleus.org/products/datanucleus/jpa/enhancer.html

+7
gradle datanucleus
source share
2 answers

I searched and did not find a plugin to run the DataNucleus Enhancer from Gradle. But there is a way to do this using the DataNucleus Enhancer Ant task.

In my build.gradle I added the following.

 task datanucleusEnhance { description "Enhance JPA model classes using DataNucleus Enhancer" dependsOn compileJava doLast { // define the entity classes def entityFiles = fileTree(sourceSets.main.output.classesDir).matching { include 'com/mycom/*.class', 'org/myorg/*.class' } println "Enhancing with DataNucleus the following files" entityFiles.getFiles().each { println it } // define Ant task for DataNucleus Enhancer ant.taskdef( name : 'datanucleusenhancer', classpath : sourceSets.main.runtimeClasspath.asPath, classname : 'org.datanucleus.enhancer.EnhancerTask' // the below is for DataNucleus Enhancer 3.1.1 //classname : 'org.datanucleus.enhancer.tools.EnhancerTask' ) // run the DataNucleus Enhancer as an Ant task ant.datanucleusenhancer( classpath: sourceSets.main.runtimeClasspath.asPath, verbose: true, api: "JPA") { entityFiles.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet) } } } classes.dependsOn(datanucleusEnhance) 

In entityFiles you are setting up annotated JPA object classes.

Unfortunately, you do not see the enhancer output, since this task uses the Ant log. If you are not using gradle with the -i or -d .

+10
source share

Here is a slightly different approach. First of all, I created the EntityEnhancer class in my project. This class calls the DataNucleus enhancer through the main method. Then I called this class from the Gradle JavaExec task.

This displays all enhancer log messages on the Gradle console, and is also called from the IDE.

The EntityEnhancer class uses the Spring Boot 1.3.5 library.

 public class EntityEnhancer { private static final ClassPathScanningCandidateComponentProvider ENTITY_SCANNER; static { ENTITY_SCANNER = new ClassPathScanningCandidateComponentProvider(false); ENTITY_SCANNER.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); } public static void main(String[] args) throws IOException { Validate.isTrue(args.length == 1, "Expected single argument <package_to_scan>!"); String pathToScan = args[0]; String[] classesToEnhance = findEntityClasses(pathToScan); Validate.isTrue(classesToEnhance.length > 0, "No classes to enhance has been found!"); DataNucleusEnhancer enhancer = new DataNucleusEnhancer("JPA", null); enhancer.addClasses(classesToEnhance); enhancer.enhance(); } private static String[] findEntityClasses(String packageToScan) throws IOException { Set<BeanDefinition> entityBeanDefinitions = ENTITY_SCANNER.findCandidateComponents(packageToScan); List<String> entityClasses = entityBeanDefinitions.stream().map(BeanDefinition::getBeanClassName).collect(Collectors.toList()); return entityClasses.toArray(new String[]{}); } } 

Task definition for your build.gradle file. This actually puts your only compiled classes in the classpath and runs the EntityEnhancer class.

 // Call this task from your IDE after project compilation. task enhanceJpaEntities(type: JavaExec) { classpath = sourceSets.main.runtimeClasspath main = 'com.your.project.package.EntityEnhancer' args 'com.your.entities.package' } classes.doLast { enhanceJpaEntities.execute() } 
+1
source share

All Articles