diff --git a/maven-plugin/src/main/java/org/hibernate/mvn/GenerateHbmMojo.java b/maven-plugin/src/main/java/org/hibernate/mvn/GenerateHbmMojo.java new file mode 100644 index 0000000000..6a8581cac2 --- /dev/null +++ b/maven-plugin/src/main/java/org/hibernate/mvn/GenerateHbmMojo.java @@ -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. + *

+ * 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(); + } + } + + +} diff --git a/test/maven-plugin/src/it/generateHbm/invoker.properties b/test/maven-plugin/src/it/generateHbm/invoker.properties new file mode 100644 index 0000000000..13598db400 --- /dev/null +++ b/test/maven-plugin/src/it/generateHbm/invoker.properties @@ -0,0 +1,2 @@ +invoker.java.version = 1.8+ +invoker.goals = generate-resources \ No newline at end of file diff --git a/test/maven-plugin/src/it/generateHbm/pom.xml b/test/maven-plugin/src/it/generateHbm/pom.xml new file mode 100644 index 0000000000..944b92a6ca --- /dev/null +++ b/test/maven-plugin/src/it/generateHbm/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.hibernate.tool.test + hbm2ddl + 0.0.1-SNAPSHOT + + + UTF-8 + UTF-8 + + 1.8 + 1.4.195 + + + + + + org.hibernate + hibernate-tools-maven-plugin + @project.version@ + + + HBM generation + generate-resources + + generateHbm + + + ${project.build.directory}/generated-sources + ${project.basedir}/src/main/resources/templates/ + + + + + ${project.basedir}/src/main/resources/hibernate.reveng.xml + + + + + com.h2database + h2 + ${h2.version} + + + + + + + \ No newline at end of file diff --git a/test/maven-plugin/src/it/generateHbm/src/main/resources/hibernate.properties b/test/maven-plugin/src/it/generateHbm/src/main/resources/hibernate.properties new file mode 100644 index 0000000000..71496b3adc --- /dev/null +++ b/test/maven-plugin/src/it/generateHbm/src/main/resources/hibernate.properties @@ -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 \ No newline at end of file diff --git a/test/maven-plugin/src/it/generateHbm/src/main/resources/hibernate.reveng.xml b/test/maven-plugin/src/it/generateHbm/src/main/resources/hibernate.reveng.xml new file mode 100644 index 0000000000..7ac52fd2cf --- /dev/null +++ b/test/maven-plugin/src/it/generateHbm/src/main/resources/hibernate.reveng.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/test/maven-plugin/src/it/generateHbm/src/main/resources/templates/pojo/Pojo.ftl b/test/maven-plugin/src/it/generateHbm/src/main/resources/templates/pojo/Pojo.ftl new file mode 100644 index 0000000000..84ff76a5b2 --- /dev/null +++ b/test/maven-plugin/src/it/generateHbm/src/main/resources/templates/pojo/Pojo.ftl @@ -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"/> + + +<#include "PojoExtraClassCode.ftl"/> + +} + + +${pojo.generateImports()} +${classbody} + diff --git a/test/maven-plugin/src/it/generateHbm/src/main/resources/templates/pojo/PojoFields.ftl b/test/maven-plugin/src/it/generateHbm/src/main/resources/templates/pojo/PojoFields.ftl new file mode 100644 index 0000000000..18cf396cc6 --- /dev/null +++ b/test/maven-plugin/src/it/generateHbm/src/main/resources/templates/pojo/PojoFields.ftl @@ -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)} + */ + ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${c2j.keyWordCheck(field.name)}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}; + + diff --git a/test/maven-plugin/src/it/generateHbm/test.mv.db b/test/maven-plugin/src/it/generateHbm/test.mv.db new file mode 100644 index 0000000000..72d79270f8 Binary files /dev/null and b/test/maven-plugin/src/it/generateHbm/test.mv.db differ diff --git a/test/maven-plugin/src/it/generateHbm/verify.groovy b/test/maven-plugin/src/it/generateHbm/verify.groovy new file mode 100644 index 0000000000..586fe51dc5 --- /dev/null +++ b/test/maven-plugin/src/it/generateHbm/verify.groovy @@ -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); +}