Skip to content

Commit 90e64dd

Browse files
committed
HBX-3041: Backport the Gradle 5 minute tutorial to the 6.6 branch
Signed-off-by: Koen Aers <[email protected]>
1 parent 2e5eb09 commit 90e64dd

File tree

17 files changed

+851
-3
lines changed

17 files changed

+851
-3
lines changed

gradle/docs/5-minute-tutorial.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<!--
2+
~ Copyright 2004 - 2025 Red Hat, Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" basis,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
[![Hibernate](https://static.jboss.org/hibernate/images/hibernate_200x150.png)](https://tools.hibernate.org)
18+
19+
# Hibernate Tools Gradle : 5 Minute Tutorial
20+
21+
The best way to get to know the Hibernate Tools Gradle plugin is to start to use it.
22+
Hence we will provide a quick tutorial that gives you the first taste of it.
23+
Before tackling this tutorial, make sure you have the [Gradle](https://gradle.org) build tool
24+
[installed](https://gradle.org/install/) and available on your machine.
25+
26+
## Create a Gradle Java Project
27+
28+
Let’s assume in this case that we start off with a very simple default Gradle Java application
29+
that we create from a command-line window with the instruction below.
30+
31+
```shell
32+
gradle init --type java-application --dsl groovy
33+
```
34+
35+
Gradle will ask you some details about your application. The conversation is shown below
36+
for completenes but of course you can make your own choices.
37+
38+
```shell
39+
Enter target Java version (min: 7, default: 21):
40+
41+
Project name (default: 5-minute-tutorial):
42+
43+
Select application structure:
44+
1: Single application project
45+
2: Application and library project
46+
Enter selection (default: Single application project) [1..2] 1
47+
48+
Select test framework:
49+
1: JUnit 4
50+
2: TestNG
51+
3: Spock
52+
4: JUnit Jupiter
53+
Enter selection (default: JUnit Jupiter) [1..4] 4
54+
55+
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
56+
57+
58+
> Task :init
59+
Learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.13/samples/sample_building_java_applications.html
60+
61+
BUILD SUCCESSFUL in 19s
62+
1 actionable task: 1 executed
63+
```
64+
65+
Now you should see two folders along with a number of Gradle specific files that have
66+
been created. It is beyond the scope of this short tutorial to explain all these artefacts.
67+
We will focus mainly on the `build.gradle` file in the `app` folder. But to make the plugin work
68+
you will need to modify the file `gradle.properties` that was generated in the root folder.
69+
70+
## Disable the Configuration Cache
71+
72+
Currently the Hibernate Tools Gradle plugin does not support the configuration cache.
73+
As the Gradle init task generates a `gradle.properties` file in the root folder that
74+
explicitly enables the use of the configuration cache, you will need to comment out
75+
or delete the line where this happens.
76+
77+
```properties
78+
#org.gradle.configuration-cache=true
79+
```
80+
81+
You could also get rid of the entire `gradle.properties` file as we don't need it for the purpose of this
82+
tutorial. Now we can tackle the `build.gradle` file.
83+
84+
## Modify the generated `app\build.gradle` file
85+
86+
We have to specify the use of the Gradle plugin in the `plugin` section of the `build.gradle` file.
87+
So we add `id 'org.hibernate.tool.hibernate-tools-gradle' version '6.6.20.Final'` to that section.
88+
89+
```groovy
90+
...
91+
plugins {
92+
...
93+
id 'org.hibernate.tool.hibernate-tools-gradle' version '6.6.20.Final'
94+
}
95+
...
96+
```
97+
98+
Also we need to depend on the java library containing the [H2 database]() drivers.
99+
This is done in the `dependencies` section of the `gradle.build` file,
100+
to which we add `implementation 'com.h2database:h2:2.3.232'`.
101+
102+
```groovy
103+
...
104+
dependencies {
105+
...
106+
implementation 'com.h2database:h2:2.3.232'
107+
}
108+
...
109+
```
110+
111+
You could as an alternative also replace the entire `gradle.build` file
112+
with the contents as shown below.
113+
114+
```groovy
115+
plugins {
116+
id 'application'
117+
id 'org.hibernate.tool.hibernate-tools-gradle' version '6.6.20.Final'
118+
}
119+
120+
repositories {
121+
mavenCentral()
122+
}
123+
124+
dependencies {
125+
implementation('com.h2database:h2:2.3.232')
126+
}
127+
```
128+
129+
With this in place, we need to make sure that the Hibernate Tools Gradle plugin knows where
130+
to find the database from which to generate the artefacts. This is done by spefifying the
131+
Hibernate properties in the file `hibernate.properties`.
132+
133+
## Specify the Hibernate Properties
134+
135+
For the purpose of this tutorial introduction, let's assume that you have a database running, e.g.
136+
[H2 Sakila database](https://github.com/hibernate/sakila-h2) reacheable at the following JDBC URL:
137+
`jdbc:h2:tcp://localhost/./sakila`.
138+
139+
With this set up, the `hibernate.properties` file should contain the properties as specified below.
140+
141+
```properties
142+
hibernate.connection.driver_class=org.h2.Driver
143+
hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila
144+
hibernate.connection.username=sa
145+
hibernate.default_catalog=SAKILA
146+
hibernate.default_schema=PUBLIC
147+
```
148+
149+
For the file to be found by the plugin, add it as a resource to the project in the
150+
`app/src/main/resources` subfolder.
151+
152+
## Run the Reverse Engineering
153+
154+
With all the previous elements in place, generating the Java classes from the Sakila database
155+
becomes as simple as issuing `./gradlew generateJava` in your command line window.
156+
157+
```shell
158+
me@machine 5-minute-tutorial % ./gradlew generateJava
159+
160+
> Task :app:generateJava
161+
Starting Task 'generateJava'
162+
Creating Java exporter
163+
Loading the properties file : path/to/5-minute-tutorial/app/src/main/resources/hibernate.properties
164+
Properties file is loaded
165+
Starting Java export to directory: path/to/5-minute-tutorial/app/generated-sources...
166+
...
167+
Java export finished
168+
Ending Task 'generateJava'
169+
```
170+
171+
By default, you will find the files in the folder `app/generated-sources`.
172+
173+
```
174+
koen@Lateralus generated-sources % ls
175+
Actor.java City.java Film.java FilmCategory.java Inventory.java Rental.java
176+
Address.java Country.java FilmActor.java FilmCategoryId.java Language.java Staff.java
177+
Category.java Customer.java FilmActorId.java FilmText.java Payment.java Store.java
178+
```
179+
180+
Congratulations! You have succesfully created Java classes for the Sakila database... Now it's
181+
probably time to dive somewhat deeper in the available functionality.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+
10+
# Binary files should be left untouched
11+
*.jar binary
12+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
6+
7+
# Ignore the output generated by the Hibernate Tools plugin
8+
generated-sources
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.13/userguide/building_java_projects.html in the Gradle documentation.
6+
*/
7+
8+
plugins {
9+
// Apply the application plugin to add support for building a CLI application in Java.
10+
id 'application'
11+
id 'org.hibernate.tool.hibernate-tools-gradle' version '6.6.20.Final'
12+
}
13+
14+
repositories {
15+
// Use Maven Central for resolving dependencies.
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
// Use JUnit Jupiter for testing.
21+
testImplementation libs.junit.jupiter
22+
23+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
24+
25+
// This dependency is used by the application.
26+
implementation libs.guava
27+
implementation 'com.h2database:h2:2.3.232'
28+
}
29+
30+
// Apply a specific Java toolchain to ease working on different environments.
31+
java {
32+
toolchain {
33+
languageVersion = JavaLanguageVersion.of(21)
34+
}
35+
}
36+
37+
application {
38+
// Define the main class for the application.
39+
mainClass = 'org.example.App'
40+
}
41+
42+
tasks.named('test') {
43+
// Use JUnit Platform for unit tests.
44+
useJUnitPlatform()
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
package org.example;
5+
6+
public class App {
7+
public String getGreeting() {
8+
return "Hello World!";
9+
}
10+
11+
public static void main(String[] args) {
12+
System.out.println(new App().getGreeting());
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# Hibernate Tools, Tooling for your Hibernate Projects #
3+
# #
4+
# Copyright 2004-2025 Red Hat, Inc. #
5+
# #
6+
# Licensed under the Apache License, Version 2.0 (the "License"); #
7+
# you may not use this file except in compliance with the License. #
8+
# You may obtain a copy of the License at #
9+
# #
10+
# http://www.apache.org/licenses/LICENSE-2.0 #
11+
# #
12+
# Unless required by applicable law or agreed to in writing, software #
13+
# distributed under the License is distributed on an "AS IS" basis, #
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15+
# See the License for the specific language governing permissions and #
16+
# limitations under the License. #
17+
############################################################################
18+
hibernate.connection.driver_class=org.h2.Driver
19+
hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila
20+
hibernate.connection.username=sa
21+
hibernate.default_catalog=SAKILA
22+
hibernate.default_schema=PUBLIC
23+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
package org.example;
5+
6+
import org.junit.jupiter.api.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class AppTest {
10+
@Test void appHasAGreeting() {
11+
App classUnderTest = new App();
12+
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties
3+
4+
#org.gradle.configuration-cache=true
5+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
3+
4+
[versions]
5+
guava = "33.3.1-jre"
6+
junit-jupiter = "5.11.3"
7+
8+
[libraries]
9+
guava = { module = "com.google.guava:guava", version.ref = "guava" }
10+
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
Binary file not shown.

0 commit comments

Comments
 (0)