Skip to content

Commit 207e37d

Browse files
committed
HBX-3022: Create 5 Minute Tutorial for Gradle"
This reverts commit ad87091. Signed-off-by: Koen Aers <[email protected]>
1 parent ad87091 commit 207e37d

File tree

14 files changed

+460
-44
lines changed

14 files changed

+460
-44
lines changed

gradle/docs/5-minute-tutorial.md

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,61 @@ Hence we will provide a quick tutorial that gives you the first taste of it.
2323
Before tackling this tutorial, make sure you have the [Gradle](https://gradle.org) build tool
2424
[installed](https://gradle.org/install/) and available on your machine.
2525

26-
## Create a Simple Initial Gradle Java Project
26+
## Create a Gradle Java Project
2727

28-
Let’s assume in this case that we start off with a very simple Gradle Java application
29-
that we create from a command-line window with the instructions below.
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.
3030

3131
```shell
32-
mkdir 5-minute-tutorial
33-
cd 5-minute-tutorial
34-
echo "" > settings.gradle
32+
gradle init --type java-application --dsl groovy
3533
```
3634

37-
The last line above created an empty `settings.gradle` file. To use Gradle, we also need
38-
a `build.gradle` file.
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.
3937

40-
## Create the `build.gradle` file
38+
```shell
39+
Enter target Java version (min: 7, default: 21):
4140

42-
Add a `build.gradle` file to the `5-minute-tutorial` folder.
41+
Project name (default: 5-minute-tutorial):
4342

44-
```shell
45-
echo "" > build.gradle
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
4663
```
4764

48-
Now edit the created emtpy file and add the following contents:
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+
However, we will focus on the `build.gradle` file in the `app` folder.
68+
69+
## Modify the generated `app\build.gradle` file
70+
71+
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.
73+
74+
Also we need to depend on the java library containing the [H2 database]() drivers.
75+
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.
79+
80+
The complete `gradle.build` file can look like the below.
4981

5082
```groovy
5183
plugins {
@@ -72,15 +104,7 @@ For the purpose of this tutorial introduction, let's assume that you have a data
72104
[H2 Sakila database](https://github.com/hibernate/sakila-h2) reacheable at the following JDBC URL:
73105
`jdbc:h2:tcp://localhost/./sakila`.
74106

75-
With this set up, we create a `hibernate.properties` file in the `src/main/resources` folder
76-
as this is the default location where the Hibernate Gradle plugin will look for this file.
77-
78-
```shell
79-
mkdir -p src/main/resources
80-
echo "" > src/main/resources/hibernate.properties
81-
```
82-
83-
Now edit the `hibernate.properties` file so that it contains the properties as specified below.
107+
With this set up, the `hibernate.properties` file should contain the properties as specified below.
84108

85109
```properties
86110
hibernate.connection.driver_class=org.h2.Driver
@@ -90,25 +114,6 @@ hibernate.default_catalog=SAKILA
90114
hibernate.default_schema=PUBLIC
91115
```
92116

93-
Now we are ready to generate the entities.
94-
95-
## Run the Reverse Engineering
96-
97-
With all the previous elements in place, generating the Java classes from the Sakila database
98-
becomes as simple as issuing `gradle generateJava` in your command line window.
99-
100-
```shell
101-
> Task :generateJava
102-
Starting Task 'generateJava'
103-
Creating Java exporter
104-
Loading the properties file : /Users/koen/temp/5-minute-tutorial/src/main/resources/hibernate.properties
105-
Properties file is loaded
106-
Starting Java export to directory: /Users/koen/temp/5-minute-tutorial/generated-sources...
107-
...
108-
Java export finished
109-
Ending Task 'generateJava'
110-
```
111-
117+
For the file to be found by the plugin, add it as a resource to the project in the
118+
`app/src/main/resources` subfolder.
112119

113-
Congratulations! You have succesfully created Java classes for the Sakila database... Now it's
114-
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
generated-sources
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
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: 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)