Skip to content
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

HBX-2869: Create a GenerateHBM Mojo in the Maven plugin #4886

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions maven-plugin/src/main/java/org/hibernate/mvn/GenerateHbmMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Hibernate Tools, Tooling for your Hibernate Projects
*
* Copyright 2016-2020 Red Hat, Inc.
*
* Licensed under the GNU Lesser General Public License (LGPL),
* version 2.1 or later (the "License").
* You may not use this file except in compliance with the License.
* You may read the licence in the 'lgpl.txt' file in the root folder of
* project or obtain a copy at
*
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* 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.
*/
package org.hibernate.mvn;

import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;

import java.io.File;

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.hibernate.tool.api.metadata.MetadataDescriptor;
import org.hibernate.tool.hbm2x.HibernateMappingExporter;

/**
* Mojo to generate hbm.xml files from an existing database.
* <p>
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
*/
@Mojo(name = "generateHbm", defaultPhase = GENERATE_SOURCES)
public class GenerateHbmMojo extends AbstractHbm2xMojo {

/** The directory into which the DAOs will be generated. */
@Parameter(defaultValue = "${project.basedir}/src/main/resources")
private File outputDirectory;

@Parameter
private String templatePath;

protected void executeExporter(MetadataDescriptor metadataDescriptor) {
try {
HibernateMappingExporter hbmExporter = new HibernateMappingExporter();
hbmExporter.setMetadataDescriptor(metadataDescriptor);
hbmExporter.setOutputDirectory(outputDirectory);
if (templatePath != null) {
getLog().info("Setting template path to: " + templatePath);
hbmExporter.setTemplatePath(new String[] {templatePath});
}
getLog().info("Starting HBM export to directory: " + outputDirectory + "...");
hbmExporter.start();
} catch (Exception e) {
e.printStackTrace();
}
}


}
2 changes: 2 additions & 0 deletions test/maven-plugin/src/it/generateHbm/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.java.version = 1.8+
invoker.goals = generate-resources
53 changes: 53 additions & 0 deletions test/maven-plugin/src/it/generateHbm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.hibernate.tool.test</groupId>
<artifactId>hbm2ddl</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>
<h2.version>1.4.195</h2.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>HBM generation</id>
<phase>generate-resources</phase>
<goals>
<goal>generateHbm</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<templatePath>${project.basedir}/src/main/resources/templates/</templatePath>
</configuration>
</execution>
</executions>
<configuration>
<revengFile>${project.basedir}/src/main/resources/hibernate.reveng.xml</revengFile>
</configuration>
<dependencies>
<dependency>
<!-- DB Driver of your choice -->
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:./test;DB_CLOSE_ON_EXIT=FALSE
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.pool_size=1
hibernate.show_sql=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>

<type-mapping>
<sql-type jdbc-type="DATE" hibernate-type="java.time.LocalDate"/>
<sql-type jdbc-type="TIMESTAMP" hibernate-type="java.time.LocalDateTime"/>
</type-mapping>

</hibernate-reverse-engineering>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}

<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/> {

<#if !pojo.isInterface()>
<#include "PojoFields.ftl"/>

<#include "PojoConstructors.ftl"/>

<#include "PojoPropertyAccessors.ftl"/>

<#include "PojoToString.ftl"/>

<#include "PojoEqualsHashcode.ftl"/>

<#else>
<#include "PojoInterfacePropertyAccessors.ftl"/>

</#if>
<#include "PojoExtraClassCode.ftl"/>

}
</#assign>

${pojo.generateImports()}
${classbody}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<#-- // Fields -->
<#list pojo.getAllPropertiesIterator() as field><#if pojo.getMetaAttribAsBool(field, "gen-property", true)><#if pojo.hasMetaAttribute(field, "field-description")> /**
${pojo.getFieldJavaDoc(field, 0)}
*/
</#if> ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${c2j.keyWordCheck(field.name)}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
</#list>
Binary file not shown.
6 changes: 6 additions & 0 deletions test/maven-plugin/src/it/generateHbm/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.io.*;

File file = new File(basedir, "target/generated-sources/Person.hbm.xml");
if (!file.isFile()) {
throw new FileNotFoundException("Could not find generated HBM file: " + file);
}
Loading