Skip to content

Commit

Permalink
HBX-2869: Create a GenerateHBM Mojo in the Maven plugin - Add the Mojo
Browse files Browse the repository at this point in the history
Signed-off-by: Koen Aers <[email protected]>
  • Loading branch information
koentsje committed Jul 19, 2024
1 parent 6641f27 commit c196ba8
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions maven/src/main/java/org/hibernate/tool/maven/GenerateHbmMojo.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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();
}
}


}

0 comments on commit c196ba8

Please sign in to comment.