jpa-schema-gradle-plugin

Gradle plugin for generate database schema or DDL scripts from JPA entities

License

License

Categories

Categories

Gradle Build Tools
GroupId

GroupId

io.github.divinespear
ArtifactId

ArtifactId

jpa-schema-gradle-plugin
Last Version

Last Version

0.3.5.1
Release Date

Release Date

Type

Type

jar
Description

Description

jpa-schema-gradle-plugin
Gradle plugin for generate database schema or DDL scripts from JPA entities
Project URL

Project URL

http://divinespear.github.io/jpa-schema-gradle-plugin
Source Code Management

Source Code Management

https://github.com/divinespear/jpa-schema-gradle-plugin

Download jpa-schema-gradle-plugin

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.divinespear/jpa-schema-gradle-plugin/ -->
<dependency>
    <groupId>io.github.divinespear</groupId>
    <artifactId>jpa-schema-gradle-plugin</artifactId>
    <version>0.3.5.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.divinespear/jpa-schema-gradle-plugin/
implementation 'io.github.divinespear:jpa-schema-gradle-plugin:0.3.5.1'
// https://jarcasting.com/artifacts/io.github.divinespear/jpa-schema-gradle-plugin/
implementation ("io.github.divinespear:jpa-schema-gradle-plugin:0.3.5.1")
'io.github.divinespear:jpa-schema-gradle-plugin:jar:0.3.5.1'
<dependency org="io.github.divinespear" name="jpa-schema-gradle-plugin" rev="0.3.5.1">
  <artifact name="jpa-schema-gradle-plugin" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.divinespear', module='jpa-schema-gradle-plugin', version='0.3.5.1')
)
libraryDependencies += "io.github.divinespear" % "jpa-schema-gradle-plugin" % "0.3.5.1"
[io.github.divinespear/jpa-schema-gradle-plugin "0.3.5.1"]

Dependencies

compile (3)

Group / Artifact Type Version
javax.xml.bind : jaxb-api jar 2.3.0
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.2.31
org.hibernate.javax.persistence : hibernate-jpa-2.1-api jar 1.0.0.Final

test (5)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : hamcrest-all jar 1.3
com.netflix.nebula : nebula-test jar 6.3.0
org.spockframework : spock-core jar 1.1-groovy-2.4
com.h2database : h2 jar 1.4.191

Project Modules

There are no modules declared in this project.

jpa-schema-gradle-plugin

Build Status FOSSA Status

Gradle plugin for generate schema or DDL scripts from JPA entities using JPA 2.1 schema generator. for Maven, see Maven Plugin.

Currently support EclipseLink (Reference Implementation) and Hibernate.

Before Announce...

READ MY LIP; JPA DDL GENERATOR IS NOT SILVER BULLET

Sometimes (most times exactly :P) JPA will generate weird scripts so you SHOULD modify them properly.

Version History

See Releases for more informations...

6.x (in progress...)

  • Required JDK 8 or above.
  • Required Gradle 6.0 or above. (for support Java 13 or above)

4.x

0.3.6

  • Required Gradle 4.10 or above. (for support spring-boot plugin version 2.0+)

0.3

  • Required Gradle 4.0 or above.
  • Java 9 support.
  • Required JDK 8 or above.
  • No more output.resourcesDir = output.classesDir needed.
  • No more buildscript dependencies needed.
  • Dropped support DataNucleus, it was my mistake.
  • Required Gradle 6.0 or above. (for support Java 13)

Reworking on 0.3

  • Minimized spring dependency, only include spring-orm, spring-context, spring-aspects and its dependencies. (based on spring 5.x)
  • Direct including any JPA implementation is removed, remains JUST API.
  • Will improve test with each major release version of each JPA providers.
  • Re-implemented with Kotlin, on my self-training.

If you have discussions, please make issue. discussions are always welcome.

How-to Use

see Gradle Plugins Registry.

plugins {
  id 'io.github.divinespear.jpa-schema-generate' version '0.3.6'
}

generateSchema {
  // default options
  // see SchemaGenerationConfig to all options
  ...
  // if you want multiple output
  targets {
    targetName {
      // same as default options
      ...
    }
  }
}

To generate schema, run

gradle generateSchema

or

./gradlew generateSchema

see also test cases Generate*Spec.groovy, as examples.

without persistence.xml

You MUST specify two options: vendor and packageToScan.

generateSchema {
  vendor = 'hibernate' // 'eclipselink', 'hibernate', or 'hibernate+spring'.
                       // you can use class name too. (like 'org.hibernate.jpa.HibernatePersistenceProvider')
  packageToScan = [ 'your.package.to.scan', ... ]
  ...
}

Plugin only dependencies

Since 0.3.4, you can add dependencies for plugin with configuration generateSchema.

// no need to add 'generateSchema' into configurations block.

dependencies {
  ...
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  // only need to load java.time converter from spring-data-jpa on schema generation
  generateSchema 'org.threeten:threetenbp:1.3.6'
}

generateSchema {
  ...  
  packageToScan = [
    // load java.time converter from spring-data-jpa
    'org.springframework.data.jpa.convert.threeten',
    'your.package.to.scan',
    ...
  ]
  ...
}

Provider specific

EclipseLink

  • EclipseLink 2.5 on Java 9 without persistence.xml will not work.
  • EclipseLink 2.6 on Java 12 or higher will not work. embedded ASM library cannot read class files.
  • EclipseLink's Oracle{8,9,10,11}Platform uses some type classes from Oracle's JDBC driver. you should have it in your dependency.

Hibernate

  • After 5.2, just use hibernate-core instead hibernate-entitymanager, it is merged.

  • Naming strategy property is

    • 4.x: hibernate.ejb.naming_strategy
    • 5.x: hibernate.physical_naming_strategy / hibernate.implicit_naming_strategy
  • For select dialect, do one of

    • set hibernate.dialect on properties
    • set databaseProductName, databaseMajorVersion, and/or databaseMinorVersion for determine dialect.

Hibernate with Spring ORM

  • vendor should be hibernate+spring. (without persistence.xml)
  • use io.spring.dependency-management for version management.
    • You can change hibernate version with hibernate.version property.

SchemaGenerationConfig

Here is full list of parameters of generateSchema.

name type description
skip boolean skip schema generation

default value is false.

format boolean generate as formatted

default value is false.

scanTestClasses boolean scan test classes

default value is false.

persistenceXml string location of persistence.xml file

Note: Hibernate DOES NOT SUPPORT custom location. (SchemaExport support it, but JPA 2.1 schema generator does NOT.)

default value is META-INF/persistence.xml.

persistenceUnitName string unit name of persistence.xml

default value is default.

databaseAction string schema generation action for database

support value is one of

  • none
  • create
  • drop
  • drop-and-create
  • create-or-extend-tables (only for EclipseLink with database target)

default value is none.

scriptAction string schema generation action for script

support value is one of

  • none
  • create
  • drop
  • drop-and-create

default value is none.

outputDirectory file output directory for generated ddl scripts

REQUIRED for scriptAction is one of create, drop, or drop-and-create.

default value is ${project.buildDir}/generated-schema.

createOutputFileName string generated create script name

REQUIRED for scriptAction is one of create, or drop-and-create.

default value is {targetName}-create.sql if targetName presented, otherwise create.sql.

dropOutputFileName string generated drop script name

REQUIRED for scriptAction is one of drop, or drop-and-create.

default value is {targetName}-drop.sql if targetName presented, otherwise drop.sql.

createSourceMode string specifies whether the creation of database artifacts is to occur on the basis of the object/relational mappingmetadata, DDL script, or a combination of the two.

support value is one of

  • metadata
  • script
  • metadata-then-script
  • script-then-metadata

default value is metadata.

createSourceFile string create source file path.

REQUIRED for createSourceMode is one of script, metadata-then-script, orscript-then-metadata.

dropSourceMode string specifies whether the dropping of database artifacts is to occur on the basis of the object/relational mappingmetadata, DDL script, or a combination of the two.

support value is one of

  • metadata
  • script
  • metadata-then-script
  • script-then-metadata

default value is metadata.

dropSourceFile file drop source file path.

REQUIRED for dropSourceMode is one of script, metadata-then-script, orscript-then-metadata.

jdbcDriver string jdbc driver class name

default is declared class name in persistence xml.

and Remember, No Russian you MUST configure jdbc driver to dependencies.

jdbcUrl string jdbc connection url

default is declared connection url in persistence xml.

jdbcUser string jdbc connection username

default is declared username in persistence xml.

jdbcPassword string jdbc connection password

default is declared password in persistence xml.

If your account has no password (especially local file-base, like Apache Derby, H2, etc...), it can be omitted.

databaseProductName string database product name for emulate database connection. this should useful for script-only action.
  • specified if scripts are to be generated by the persistence provider and a connection to the target databaseis not supplied.
  • The value of this property should be the value returned for the target database by DatabaseMetaData#getDatabaseProductName()
databaseMajorVersion int database major version for emulate database connection. this should useful for script-only action.
  • specified if sufficient database version information is not included from DatabaseMetaData#getDatabaseProductName()
  • The value of this property should be the value returned for the target database by DatabaseMetaData#getDatabaseMajorVersion()
databaseMinorVersion int database minor version for emulate database connection. this should useful for script-only action.
  • specified if sufficient database version information is not included from DatabaseMetaData#getDatabaseProductName()
  • The value of this property should be the value returned for the target database by DatabaseMetaData#getDatabaseMinorVersion()
lineSeparator string line separator for generated schema file.

support value is one of CRLF (windows default), LF (*nix, max osx), and CR (classic mac), in case-insensitive.

default value is system property line.separator. if JVM cannot detect line.separator, then use LF by git core.autocrlf handling.

properties java.util.Map JPA vendor specific properties.
vendor string JPA vendor name or class name of vendor's PersistenceProvider implemention.

vendor name is one of

  • eclipselink(or org.eclipse.persistence.jpa.PersistenceProvider)
  • hibernate (or org.hibernate.jpa.HibernatePersistenceProvider)
  • hibernate+spring (or org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider)

REQUIRED for project without persistence.xml

packageToScan java.util.List list of package name for scan entity classes

REQUIRED for project without persistence.xml

How-to config library specific properties

It's just groovy map, so you can config like this:

generateSchema {
  ...
  // global properties
  properties = [
    'hibernate.dialect': 'org.hibernate.dialect.MySQL5InnoDBDialect',
    ...
  ]
  // you can set target-specific too.
  ...
}

Database Product Names

It's about databaseProductName property. If not listed below, will work as basic standard SQL.

for EclipseLink

databaseMajorVersion and databaseMinorVersion is not required.

  • Oracle12 = Oracle 12c
  • Oracle11 = Oracle 11g
  • Oracle10 = Oracle 10g
  • Oracle9 = Oracle 9i
  • Oracle = Oracle with default compatibility
  • Microsoft SQL Server
  • DB2
  • MySQL
  • PostgreSQL
  • SQL Anywhere
  • Sybase SQL Server
  • Adaptive Server Enterprise = Sybase
  • Pointbase
  • Informix Dynamic Server
  • Firebird
  • ingres
  • Apache Derby
  • H2
  • HSQL Database Engine

for Hibernate

For other versions, select tag to your version.

License

Source Copyright © 2013-2018 Sin Young "Divinespear" Kang (divinespear at gmail dot com). Distributed under the Apache License, Version 2.0.

Versions

Version
0.3.5.1
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1-1
0.2.1
0.2.0-1
0.2.0
0.1.12-1
0.1.12
0.1.11
0.1.10
0.1.9-1
0.1.9
0.1.8
0.1.7-1
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0