From c196ba8321e197ac43121da2b2593d53fb40b1a9 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Fri, 19 Jul 2024 08:23:36 +0300 Subject: [PATCH] HBX-2869: Create a GenerateHBM Mojo in the Maven plugin - Add the Mojo Signed-off-by: Koen Aers --- .../hibernate/tool/maven/GenerateHbmMojo.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 maven/src/main/java/org/hibernate/tool/maven/GenerateHbmMojo.java diff --git a/maven/src/main/java/org/hibernate/tool/maven/GenerateHbmMojo.java b/maven/src/main/java/org/hibernate/tool/maven/GenerateHbmMojo.java new file mode 100644 index 0000000000..13dc50aa5e --- /dev/null +++ b/maven/src/main/java/org/hibernate/tool/maven/GenerateHbmMojo.java @@ -0,0 +1,66 @@ +/* + * 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.tool.maven; + +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.hibernate.tool.api.export.Exporter; +import org.hibernate.tool.api.export.ExporterConstants; +import org.hibernate.tool.api.export.ExporterFactory; +import org.hibernate.tool.api.export.ExporterType; +import org.hibernate.tool.api.metadata.MetadataDescriptor; + +import java.io.File; + +import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES; + +/** + * 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 AbstractGenerationMojo { + + /** 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 { + Exporter hbmExporter = ExporterFactory.createExporter(ExporterType.HBM); + hbmExporter.getProperties().put(ExporterConstants.METADATA_DESCRIPTOR, metadataDescriptor); + hbmExporter.getProperties().put(ExporterConstants.DESTINATION_FOLDER, outputDirectory); + if (templatePath != null) { + getLog().info("Setting template path to: " + templatePath); + hbmExporter.getProperties().put(ExporterConstants.TEMPLATE_PATH, new String[] {templatePath}); + } + getLog().info("Starting HBM export to directory: " + outputDirectory + "..."); + hbmExporter.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + +}