From 3f58ea7565264b5fa5361cdf456617d7cb140e29 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Mon, 7 Jul 2025 12:21:26 +0300 Subject: [PATCH] HBX-3022: Create 5 Minute Tutorial for Gradle - Add a section with instructions to modify the 'app/build.gradle' file - Add a section for the specifying the 'hibernate.properties' file - Adapt the generated project in 'gradle/docs/examples/5-minute-tutorial' Signed-off-by: Koen Aers --- gradle/docs/5-minute-tutorial.md | 56 +++++++++++++++++++ .../5-minute-tutorial/app/build.gradle | 39 ++----------- .../src/main/resources/hibernate.properties | 23 ++++++++ 3 files changed, 83 insertions(+), 35 deletions(-) create mode 100644 gradle/docs/examples/5-minute-tutorial/app/src/main/resources/hibernate.properties diff --git a/gradle/docs/5-minute-tutorial.md b/gradle/docs/5-minute-tutorial.md index 39e4616f56..3fe8372f63 100644 --- a/gradle/docs/5-minute-tutorial.md +++ b/gradle/docs/5-minute-tutorial.md @@ -61,3 +61,59 @@ Learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.13 BUILD SUCCESSFUL in 19s 1 actionable task: 1 executed ``` + +Now you should see two folders along with a number of Gradle specific files that have +been created. It is beyond the scope of this short tutorial to explain all these artefacts. +However, we will focus on the `build.gradle` file in the `app` folder. + +## Modify the generated `app\build.gradle` file + +We have to specify the use of the Gradle plugin in the `plugin` section of the `build.gradle` file. +So we add `id('org.hibernate.tool.hibernate-tools-gradle') version '7.0.3.Final'` to that section. + +Also we need to depend on the java library containing the [H2 database]() drivers. +This is done in the `dependencies` section of the `gradle.build` file, +to which we add `implementation('com.h2database:h2:2.3.232')`. +To be able to look up this dependency, we add `mavenCentral()` to the `repositories` section +of the `gradle.build` file. + +The complete `gradle.build` file can look like the below. + +``` +plugins { + id('application') + id('org.hibernate.tool.hibernate-tools-gradle') version '7.0.3.Final' +} + +repositories { + mavenCentral() +} + +dependencies { + implementation('com.h2database:h2:2.3.232') +} +``` + +With this in place, we need to make sure that the Hibernate Tools Gradle plugin knows where +to find the database from which to generate the artefacts. This is done by spefifying the +Hibernate properties in the file `hibernate.properties`. + +## Specify the Hibernate Properties + +For the purpose of this tutorial introduction, let's assume that you have a database running, e.g. +[H2 Sakila database](https://github.com/hibernate/sakila-h2) reacheable at the following JDBC URL: +`jdbc:h2:tcp://localhost/./sakila`. + +With this set up, the `hibernate.properties` file should contain the properties as specified below. + +``` +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila +hibernate.connection.username=sa +hibernate.default_catalog=SAKILA +hibernate.default_schema=PUBLIC +``` + +For the file to be found by the plugin, add it as a resource to the project in the +`app/src/main/resources` subfolder. + diff --git a/gradle/docs/examples/5-minute-tutorial/app/build.gradle b/gradle/docs/examples/5-minute-tutorial/app/build.gradle index 95e085478c..e0f8f85890 100644 --- a/gradle/docs/examples/5-minute-tutorial/app/build.gradle +++ b/gradle/docs/examples/5-minute-tutorial/app/build.gradle @@ -1,43 +1,12 @@ -/* - * This file was generated by the Gradle 'init' task. - * - * This generated file contains a sample Java application project to get you started. - * 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. - */ - plugins { - // Apply the application plugin to add support for building a CLI application in Java. - id 'application' + id('application') + id('org.hibernate.tool.hibernate-tools-gradle') version '7.0.3.Final' } repositories { - // Use Maven Central for resolving dependencies. mavenCentral() } dependencies { - // Use JUnit Jupiter for testing. - testImplementation libs.junit.jupiter - - testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - - // This dependency is used by the application. - implementation libs.guava -} - -// Apply a specific Java toolchain to ease working on different environments. -java { - toolchain { - languageVersion = JavaLanguageVersion.of(21) - } -} - -application { - // Define the main class for the application. - mainClass = 'org.example.App' -} - -tasks.named('test') { - // Use JUnit Platform for unit tests. - useJUnitPlatform() -} + implementation('com.h2database:h2:2.3.232') +} \ No newline at end of file diff --git a/gradle/docs/examples/5-minute-tutorial/app/src/main/resources/hibernate.properties b/gradle/docs/examples/5-minute-tutorial/app/src/main/resources/hibernate.properties new file mode 100644 index 0000000000..60b2483a2f --- /dev/null +++ b/gradle/docs/examples/5-minute-tutorial/app/src/main/resources/hibernate.properties @@ -0,0 +1,23 @@ +############################################################################ +# Hibernate Tools, Tooling for your Hibernate Projects # +# # +# Copyright 2004-2025 Red Hat, Inc. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" basis, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +############################################################################ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila +hibernate.connection.username=sa +hibernate.default_catalog=SAKILA +hibernate.default_schema=PUBLIC +