Skip to content

Commit 1812bb2

Browse files
committed
HHH-9809 - Improve Hibernate Gradle plugin
1 parent acea523 commit 1812bb2

File tree

11 files changed

+463
-67
lines changed

11 files changed

+463
-67
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
= Bytecode Enhancement
2+
:toc:
3+
4+
This guide covers Hibernate's ability to enhance an applications domain model, the ways to perform that
5+
enhancement and the capabilities introduced into the domain model by the enhancement.
6+
7+
8+
== The capabilities
9+
10+
Hibernate will enhance the classes in an application's domain model in order to add one or more of the
11+
following capabilities:
12+
13+
. Lazy state initialization
14+
. Dirtiness tracking
15+
. Automatic bi-directional association management
16+
. Performance optimizations
17+
18+
todo : explain each in detail
19+
20+
21+
== Performing enhancement
22+
23+
Ultimately all enhancement is handled by the `org.hibernate.bytecode.enhance.spi.Enhancer` class. Custom means to
24+
enhancement can certainly be crafted on top of Enhancer, but that is beyond the scope of this guide. Here we
25+
will focus on the means Hibernate already exposes for performing these enhancements.
26+
27+
=== Run-time enhancement
28+
29+
Currently run-time enhancement of the domain model is only supported in managed JPA environments following the
30+
JPA defined SPI for performing class transformations. Even then, this support is disabled by default. In this
31+
scenario, run-time enhancement can be enabled by specifying `hibernate.ejb.use_class_enhancer=true` as a
32+
persistent unit property.
33+
34+
35+
=== Build-time enhancement
36+
37+
Hibernate also offers the ability to integrate the enhancement of the domain model as part of the
38+
normal build cycle of that domain model. Gradle, Ant and Maven are all supported. One possible benefit
39+
of this approach is that the enhanced classes are what gets added to the jar and can then be used on both
40+
sides of serialization.
41+
42+
43+
=== Gradle Plugin
44+
45+
Hibernate provides a Gradle plugin that is capable of providing build-time enhancement of the domain model as they are
46+
compiled as part of a Gradle build. To use the plugin a project would first need to apply it:
47+
48+
[[gradle-plugin-apply-example]]
49+
.Apply the plugin
50+
====
51+
[source, GROOVY]
52+
----
53+
ext {
54+
hibernateVersion = 'hibernate-version-you-want'
55+
}
56+
buildscript {
57+
dependencies {
58+
classpath "org.hibernate:hibernate-gradle-plugin:$hibernateVersion"
59+
}
60+
}
61+
----
62+
====
63+
64+
At the moment there is not much to configure with regard to the Enhancer, but what is configurable is exposed
65+
through a DSL extension registered. By default enhancement will not be done (in preparation for when this
66+
Gradle plugin offers additional capabilities). To enable it you must configure the following DSL extension:
67+
68+
[[gradle-plugin-apply-example]]
69+
.Apply the plugin
70+
====
71+
[source, GROOVY]
72+
----
73+
hibernate {
74+
enhance {
75+
// any configuration goes here
76+
}
77+
}
78+
----
79+
====
80+
81+
Currently the "enhance" extension supports 3 properties:
82+
83+
* `enableLazyInitialization`
84+
* `enableDirtyTracking`
85+
* `enableAssociationManagement`
86+
87+
Once enhancement overall is enabled, the default for these 3 properties is `true`.
88+
89+
90+
=== Ant Task
91+
92+
93+
=== Maven Plugin

documentation/src/main/asciidoc/topical/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ NOTE: This is still very much a work in progress. <<helping,Help>> is definitely
1919
== Tooling
2020

2121
* See the <<metamodelgen/MetamodelGenerator.adoc#,Metamodel Generator Guide>> for details on generating a JPA "Static Metamodel"
22+
* see the <<bytecode/BytecodeEnhancement.adoc#,Bytecode Enhancement Guide>> for information on bytecode enhancement
2223
* Guide on the Gradle plugin coming soon
2324
* Guide on the Ant tasks coming soon
2425
* Guide on the Maven plugin coming soon
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Defines a Gradle plugin for introducing Hibernate specific tasks and capabilities into and end-user build.
2+
3+
Currently the only capability added is for bytecode enhancement of the user domain model, although other capabilities are
4+
planned.
5+
6+
todo : usage

tooling/hibernate-gradle-plugin/hibernate-gradle-plugin.gradle

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7+
apply plugin: 'java-gradle-plugin'
8+
79
apply plugin: 'groovy'
810
apply plugin: 'maven'
911
apply plugin: 'java'
1012

1113
dependencies {
12-
compile( project(':hibernate-core') )
13-
compile( libraries.jpa )
14-
compile( libraries.javassist )
15-
compile gradleApi()
16-
compile localGroovy()
14+
compile( project( ':hibernate-core' ) )
15+
compile( libraries.jpa )
16+
compile( libraries.javassist )
17+
compile gradleApi()
18+
compile localGroovy()
1719
}
1820

1921
mavenPom {
20-
name = 'Hibernate Gradle plugin'
21-
description = "Gradle plugin for integrating Hibernate functionality into your build"
22+
name = 'Hibernate Gradle plugin'
23+
description = "Gradle plugin for integrating Hibernate functionality into your build"
2224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.tooling.gradle
8+
9+
/**
10+
* Gradle DSL extension for configuring various Hibernate bytecode enhancement. Registered
11+
* under "hibernate.enhance".
12+
*
13+
* @author Steve Ebersole
14+
*/
15+
class EnhanceExtension {
16+
def boolean enableLazyInitialization = true
17+
def boolean enableDirtyTracking = true
18+
def boolean enableAssociationManagement = true
19+
20+
boolean shouldApply() {
21+
return enableLazyInitialization || enableDirtyTracking || enableAssociationManagement;
22+
}
23+
}

tooling/hibernate-gradle-plugin/src/main/groovy/org/hibernate/tooling/gradle/EnhancerTask.groovy renamed to tooling/hibernate-gradle-plugin/src/main/groovy/org/hibernate/orm/tooling/gradle/EnhancerTask.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7-
package org.hibernate.tooling.gradle
7+
package org.hibernate.orm.tooling.gradle
88

99
import javax.persistence.ElementCollection
1010
import javax.persistence.Entity
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.tooling.gradle
8+
9+
import org.gradle.api.Project
10+
import org.gradle.api.plugins.JavaPluginConvention
11+
import org.gradle.api.tasks.SourceSet
12+
import org.gradle.util.ConfigureUtil
13+
14+
/**
15+
* Gradle DSL extension for configuring various Hibernate built-time tasks. Registered
16+
* under "hibernate".
17+
*
18+
* @author Steve Ebersole
19+
*/
20+
class HibernateExtension {
21+
private final Project project
22+
23+
/**
24+
* The source sets that hold persistent model. Default is project.sourceSets.main
25+
*/
26+
def SourceSet[] sourceSets
27+
/**
28+
* Configuration for bytecode enhancement. Private; see instead {@link #enhance(groovy.lang.Closure)}
29+
*/
30+
protected EnhanceExtension enhance
31+
32+
HibernateExtension(Project project) {
33+
this.project = project
34+
this.sourceSet( project.getConvention().getPlugin( JavaPluginConvention ).sourceSets.main )
35+
}
36+
37+
/**
38+
* Add a single SourceSet.
39+
*
40+
* @param sourceSet The SourceSet to add
41+
*/
42+
void sourceSet(SourceSet sourceSet) {
43+
if ( sourceSets == null ) {
44+
sourceSets = []
45+
}
46+
sourceSets += sourceSets
47+
}
48+
49+
void enhance(Closure closure) {
50+
enhance = new EnhanceExtension()
51+
ConfigureUtil.configure( closure, enhance )
52+
}
53+
}

0 commit comments

Comments
 (0)