The plugin is available in the Gradle Plugin Portal
This plugin requires Gradle 7.x and targets OpenJPA 3.2.x.
In a Gradle project where you want to use the OpenJPA Gradle Plugin, add the following snippet:
apply plugin: 'openjpa'
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("com.radcortez.gradle:openjpa-gradle-plugin:3.2.0")
}
}
The OpenJPA Gradle Plugin has three tasks:
The enhance
Task is performed automatically in your build after the classes
Task. You can also call it separately
with gradle enhance
.
You can pass additional configuration to the enhance
task with:
openjpa {
enhance {
addDefaultConstructor true
enforcePropertyRestrictions false
tmpClassLoader false
}
}
You can omit the configuration if you use the default values.
If you want to generate Metamodel classes for your entity classes, you have to explicitly activate it, by adding the following configuration:
openjpa {
metamodel {
}
}
You can pass additional configuration to the metamodel
task with:
openjpa {
metamodel {
metamodelOutputFolder 'build/generated'
metamodelDependency 'org.apache.openjpa:openjpa:3.1.0'
}
}
You can omit the configuration if you use the default values.
To run the sql
task properly, you need to setup the target database to generate the sql schema. You can do it with
the following configuration:
openjpa {
sql {
connectionDriverName 'oracle.jdbc.OracleDriver'
}
}
The connectionDriverName
is mandatory. You don't need the driver in the classpath, just the name of the driver that
you use to connect to the target database. OpenJPA will try to guess the correct database dictionary to use to generate
the correct SQL statements.
You can pass additional configuration to the sql
task with:
openjpa {
sql {
connectionDriverName 'oracle.jdbc.OracleDriver'
sqlFile 'build/database.sql'
}
}
The OpenJPA Gradle Plugin also allows you to configure global properties to all tasks:
openjpa {
includes ''
excludes ''
persistenceXml 'META-INF/persistence.xml'
}
includes
excludes
persistenceXml
addDefaultConstructor
enforcePropertyRestrictions
tmpClassLoader
metamodelOutputFolder
metamodelDependency
connectionDriverName
sqlFile
openjpa {
excludes '**/*.class'
metamodel {
metamodelOutputFolder 'target/generated-sources/metamodel'
}
sql {
connectionDriverName 'oracle.jdbc.OracleDriver'
}
}
This example will exclude all classes from the OpenJPA tools, so it will only use the entities referenced in the
persistence.xml
. The metamodel source files are generated at 'target/generated-sources/metamodel' and are
automatically part of the build. An Oracle database SQL schema can be generated with gradle sql
in the file
build/database.sql
.
To use it locally, checkout the project, build it and install it in your local Maven repository.
./gradlew build
./gradlew publishToMavenLocal