Skip to content

radcortez/openjpa-gradle-plugin

Repository files navigation

Version License

OpenJPA Gradle Plugin

The plugin is available in the Gradle Plugin Portal

This plugin requires Gradle 7.x and targets OpenJPA 3.2.x.

Usage

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")
    }
}

Tasks

The OpenJPA Gradle Plugin has three tasks:

  • enhance
  • metamodel
  • sql

Enhance (Enhances entity classes with the OpenJPA Enhancer tool)

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.

Metamodel (Generates JPA Metamodel classes)

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.

SQL (Generates the database schema to a file)

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'
    }
}

Configuration

The OpenJPA Gradle Plugin also allows you to configure global properties to all tasks:

openjpa {
    includes ''
    excludes ''
    persistenceXml 'META-INF/persistence.xml'
}

Configuration Details

includes

  • Description: Include files from the classpath into the OpenJPA Tools.
  • Type: String
  • Default: none
  • Required: No
  • Scope: global

excludes

  • Description: Exclude files from the classpath into the OpenJPA Tools.
  • Type: String
  • Default: none
  • Required: No
  • Scope: global

persistenceXml

  • Description: Location of the persistence.xml file.
  • Type: String
  • Default: META-INF/persistence.xml
  • Required: No
  • Scope: global

addDefaultConstructor

  • Description: Add a default constructors to the Entity Enhancement.
  • Type: boolean
  • Default: true
  • Required: No
  • Scope: enhance

enforcePropertyRestrictions

  • Description: Throw an exception when an entity property access is not obeying the restrictions placed on property access.
  • Type: boolean
  • Default: false
  • Required: No
  • Scope: enhance

tmpClassLoader

  • Description: Tell the PCEnhancer to use a temporary classloader for enhancement.
  • Type: boolean
  • Default: false
  • Required: No
  • Scope: enhance

metamodelOutputFolder

  • Description: The output folder to use to generate the metamodel source files.
  • Type: String
  • Default: build/generated
  • Required: No
  • Scope: metamodel

metamodelDependency

  • Description: The OpenJPA dependency to use to generate the metamodel source files.
  • Type: String
  • Default: org.apache.openjpa:openjpa:3.1.0
  • Required: No
  • Scope: metamodel

connectionDriverName

  • Description: The Connection Driver Name to determine the databsase to generate the Database Schema.
  • Type: String
  • Default: none
  • Required: Yes
  • Scope: sql

sqlFile

  • Description: The SQL file name to generate the Database Schema.
  • Type: String
  • Default: build/database.sql
  • Required: No
  • Scope: sql

Full Example:

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.

Build

To use it locally, checkout the project, build it and install it in your local Maven repository.

  • Build with ./gradlew build
  • Add it to the local Maven repository with ./gradlew publishToMavenLocal