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() }
Václav Kužel
source share