-
-
Notifications
You must be signed in to change notification settings - Fork 220
HBX-3022: Create 5 Minute Tutorial for Gradle #5234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
|
||||||
| ``` | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and I think it's |
||||||
| 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. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw.. I know that Steve is in favor of using these version catalogs (hibernate/hibernate-reactive#1989), but I'm not sure how applicable is that for a small tutorial 😃 🙈 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is if you decide to go with the highlights 😃