Is there a gradle plugin for running DataNucleus Enhancer? As I can see from documentation you can run it only from Maven or Ant: http://www.datanucleus.org/products/datanucleus/jpa/enhancer.html
2 Answers
I searched and found no plugin for running the DataNucleus Enhancer from Gradle. But there is a way of doing this by using the DataNucleus Enhancer Ant task.
I added the following in my build.gradle
.
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 the entityFiles
is where you configure your JPA entity annotated classes.
Unfortunately you cannot see the enhancer output, as this task is using Ant logging. Unless you're running gradle with -i
or -d
option.
Using: Java 8, org.eclipse.persistence:javax.persistence:2.1.0
, org.datanucleus:datanucleus-accessplatform-jpa-rdbms:4.1.1
.

- 20,474
- 12
- 67
- 117
-
With version 3.1.1 of the DataNucleus Enhancer, the class name for the enhancer task now appears to be `'org.datanucleus.enhancer.tools.EnhancerTask'` – Tad Aug 10 '17 at 12:12
-
How should this task be added to the project & called? – Tom Aug 11 '17 at 09:20
-
@Tom, this is a Gradle task, just add it in your build.gradle. If you're using an IDE like Eclipse you should use the Gradle integration. – Adrian Ber Aug 11 '17 at 09:31
-
@AdrianBer I figured it out in the end; it might be worth pointing out that the script must go _above_ `war.dependsOn datanucleusEnhance` if your build.gradle needs it. – Tom Aug 11 '17 at 10:05
-
@Tom, actually I think it is better to have `classes.dependsOn(datanucleusEnhance)`. I will edit my answer. – Adrian Ber Aug 11 '17 at 11:38
-
Hi Adrian, any way you can update this to show what versions of all of this you used? Clearly, you're not using 3.1.1, and if you don't get the versions all exactly right between all these things, it refuses to run... Thank you. – jamesmortensen Apr 13 '18 at 08:04
-
@jmort253, there's a commented line there for v3.1.1 – Adrian Ber Apr 13 '18 at 11:40
-
@AdrianBer Yes, I see that, but I can't get 3.1.1 to work, and at this point don't care what version I use as long as it works. The dependencies needed for each version aren't clearly documented. That's why I was hoping you could show what versions you used for the dependencies if you have a moment to spare. Clearly, whatever versions you used work for you, and I would like to know what versions that was. Why didn't you use 3.1.1? You couldn't get the latest to work either? Thank you. – jamesmortensen Apr 15 '18 at 06:05
-
Also, what version of Java were you using? Java 7 or Java 8? Thank you. – jamesmortensen Apr 16 '18 at 06:29
-
1I'm not working on that project anymore, but back then I was using Java 8 and org.eclipse.persistence:javax.persistence:2.1.0, org.datanucleus:datanucleus-accessplatform-jpa-rdbms:4.1.1. I hope this helps. I will state this in my answer too. – Adrian Ber Apr 16 '18 at 10:05
Here is slightly different approach. First of all I've created EntityEnhancer
class in my project. This class calls DataNucleus enhancer via its main method. Then I called this class from Gradle's JavaExec task.
This prints out all enhancer's log messages on Gradle console and also can be called from IDE.
EntityEnhancer class uses 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 just-compiled classes on the classpath and runs 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,070
- 13
- 16
-
1why not just set the "persistence-unit" to enhance rather than imposing a dependency on Spring just to get classes? https://github.com/datanucleus/datanucleus-core/blob/master/src/main/java/org/datanucleus/enhancer/DataNucleusEnhancer.java#L453 – Neil Stockton Sep 10 '16 at 09:11
-
@NeilStockton Good point. Unfortunately to do this you need to have persistence.xml file on your classpath (which I don't have) or you have to implement DataNucleus PersistenceUnitMetaData class. But if you have I will save some more lines of code. – Václav Kužel Sep 10 '16 at 10:48
-
-
@Stef That is correct! I've done this as a part of a demo application. My intention was not to create an extra plugin or do anything in Groovy. Just to make it as simple as possible (less code as possible). Source code of demo application is here: https://github.com/vkuzel/Orm-Frameworks-Demo/tree/master/datanucleus – Václav Kužel Jul 27 '17 at 12:32