Skip to content

Commit aa59ea9

Browse files
committed
HBX-2983: Create 5 Minute Tutorial for Ant
Signed-off-by: Koen Aers <[email protected]>
1 parent 741cdda commit aa59ea9

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

ant/docs/5-minute-tutorial.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 Ant : 5 Minute Tutorial
20+
21+
The best way to get to know the Hibernate Tools Ant task 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 [Apache Ant](https://ant.apache.org) installed and available
24+
on your machine.
25+
26+
## Create a Java Project
27+
28+
Use a command line tool or your preferred IDE to create a basic Java project in a location
29+
of your choice and add the well known ```build.xml``` file to its root.
30+
31+
```xml
32+
<project xmlns:ivy="antlib:org.apache.ivy.ant">
33+
...
34+
</project>
35+
```
36+
37+
As you can see, we will be using [Apache Ivy](https://ant.apache.org/ivy/) to handle the
38+
library dependencies for us.
39+
40+
## Define the Project Class Path
41+
We add an ```ìvy:cachepath``` tag for the 'hibernate-tools-ant' library that will supply us with the
42+
reverse engineering functionality and another one for the 'h2' database that we will be using
43+
for the purpose of this short tutorial.
44+
45+
```xml
46+
<project xmlns:ivy="antlib:org.apache.ivy.ant">
47+
48+
<property name="hibernate.tools.version" value="the-hibernate-tools-version-to-use, e.g. 7.0.0.Final"/>
49+
<property name="h2.version" value="the-h2-version-to-use, e.g. 2.3.232"/>
50+
51+
<ivy:cachepath organisation="org.hibernate.tool" module="hibernate-tools-ant" revision="${hibernate.tools.version}"
52+
pathid="hibernate-tools" inline="true"/>
53+
<ivy:cachepath organisation="com.h2database" module="h2" revision="${h2.version}"
54+
pathid="h2" inline="true"/>
55+
56+
<path id="classpath">
57+
<path refid="hibernate-tools"/>
58+
<path refid="h2"/>
59+
</path>
60+
61+
...
62+
63+
</project>
64+
```
65+
Don't forget to substitute the property values for the two version properties with the actual
66+
versions that you want to use. When this is done, we combine the two ivy paths into the
67+
classpath that we will use to perform our reverse engineering.
68+
69+
## Add the Reverse Engineering Task
70+
Before we can add the `reveng` target that will perform the actual reverse engineering,
71+
we need to add the task definition. We define the task with the name `hibernatetool`
72+
but you can choose any name you like and use the Java class `org.hibernate.tool.ant.HibernateToolTask` that is to be found in the hibernate-tool-ant
73+
library on the class path created earlier.
74+
75+
```xml
76+
<project xmlns:ivy="antlib:org.apache.ivy.ant">
77+
78+
...
79+
80+
<taskdef name="hibernatetool"
81+
classname="org.hibernate.tool.ant.HibernateToolTask"
82+
classpathref="classpath" />
83+
84+
<target name="reveng">
85+
<hibernatetool destdir="generated-sources">
86+
<jdbcconfiguration propertyfile="hibernate.properties" />
87+
<hbm2java/>
88+
</hibernatetool>
89+
</target>
90+
91+
</project>
92+
```
93+
As a final step, at least with respect to the `build.xml` file, we create the `reveng` target
94+
and add the `hibernatetool` task. The `destdir` property on this task points to the folder
95+
where the artefacts will be generated. The nested `jdbcconfiguration` task uses a Hibernate
96+
properties file (i.e. `hibernate.properties`) for some specific settings to create the reverse
97+
engineering configuration. The nested `hbm2java` task will result in the generation of Java
98+
source files. Now as a last step before we can run ant, we need to add this
99+
`hibernate.properties` file.
100+
101+
## Specify the Hibernate Properties
102+
103+
For the purpose of this tutorial introduction, let's assume that you have a database running, e.g.
104+
[H2 Sakila database](https://github.com/maxandersen/sakila-h2) reacheable at the following JDBC URL:
105+
`jdbc:h2:tcp://localhost/./sakila`.
106+
107+
With this assumption, the `hibernate.properties` file, to be found in the root of our Java
108+
project should have the following content:
109+
110+
```properties
111+
hibernate.connection.driver_class=org.h2.Driver
112+
hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila
113+
hibernate.connection.username=sa
114+
hibernate.default_catalog=SAKILA
115+
hibernate.default_schema=PUBLIC
116+
```
117+
118+
## Run the Reverse Engineering
119+
120+
With all the previous elements in place, generating the Java classes from the Sakila database
121+
becomes as simple as issuing `ant reveng` in a command line window.
122+
123+
Congratulations! You have succesfully created Java classes for the Sakila database... Now it's
124+
probably time to dive somewhat deeper in the available functionality.

0 commit comments

Comments
 (0)