Skip to content

Commit 603bc06

Browse files
committed
HBX-3022: Create 5 Minute Tutorial for Gradle
- Add a section to show disablement of the configuration cache - Update to version 7.0.4.Final - Split out the changes in 'app/build.gradle' - Add 'app/src/main/resources/hibernate.properties' file - Add section to show running of the reverse engineering Signed-off-by: Koen Aers <[email protected]>
1 parent 207e37d commit 603bc06

File tree

5 files changed

+115
-12
lines changed

5 files changed

+115
-12
lines changed

gradle/docs/5-minute-tutorial.md

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,57 @@ BUILD SUCCESSFUL in 19s
6464

6565
Now you should see two folders along with a number of Gradle specific files that have
6666
been created. It is beyond the scope of this short tutorial to explain all these artefacts.
67-
However, we will focus on the `build.gradle` file in the `app` folder.
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.
6883

6984
## Modify the generated `app\build.gradle` file
7085

7186
We have to specify the use of the Gradle plugin in the `plugin` section of the `build.gradle` file.
72-
So we add `id('org.hibernate.tool.hibernate-tools-gradle') version '7.0.3.Final'` to that section.
87+
So we add `id 'org.hibernate.tool.hibernate-tools-gradle' version '7.0.4.Final'` to that section.
88+
89+
```groovy
90+
...
91+
plugins {
92+
...
93+
id 'org.hibernate.tool.hibernate-tools-gradle' version '7.0.4.Final'
94+
}
95+
...
96+
```
7397

7498
Also we need to depend on the java library containing the [H2 database]() drivers.
7599
This is done in the `dependencies` section of the `gradle.build` file,
76-
to which we add `implementation('com.h2database:h2:2.3.232')`.
77-
To be able to look up this dependency, we add `mavenCentral()` to the `repositories` section
78-
of the `gradle.build` file.
100+
to which we add `implementation 'com.h2database:h2:2.3.232'`.
79101

80-
The complete `gradle.build` file can look like the below.
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.
81113

82114
```groovy
83115
plugins {
84116
id('application')
85-
id('org.hibernate.tool.hibernate-tools-gradle') version '7.0.3.Final'
117+
id('org.hibernate.tool.hibernate-tools-gradle') version '7.0.4.Final'
86118
}
87119
88120
repositories {
@@ -117,3 +149,33 @@ hibernate.default_schema=PUBLIC
117149
For the file to be found by the plugin, add it as a resource to the project in the
118150
`app/src/main/resources` subfolder.
119151

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.

gradle/docs/examples/5-minute-tutorial/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
# Ignore Gradle build output directory
55
build
6+
7+
# Ignore the output generated by the Hibernate Tools plugin
8+
generated-sources
Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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+
18
plugins {
2-
id('application')
3-
id('org.hibernate.tool.hibernate-tools-gradle') version '7.0.3.Final'
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 '7.0.4.Final'
412
}
513

614
repositories {
15+
// Use Maven Central for resolving dependencies.
716
mavenCentral()
817
}
918

1019
dependencies {
11-
implementation('com.h2database:h2:2.3.232')
12-
}
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file was generated by the Gradle 'init' task.
22
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties
33

4-
org.gradle.configuration-cache=true
4+
#org.gradle.configuration-cache=true
55

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
hibernate.connection.driver_class=org.h2.Driver
2+
hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila
3+
hibernate.connection.username=sa
4+
hibernate.default_catalog=SAKILA
5+
hibernate.default_schema=PUBLIC

0 commit comments

Comments
 (0)