Skip to content
Draft
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
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ project(':hibernate-maven-plugin').projectDir = new File(rootProject.projectDir,
include 'hibernate-ant'
project(':hibernate-ant').projectDir = new File(rootProject.projectDir, "tooling/hibernate-ant")

include 'hibernate-reveng'
project(':hibernate-reveng').projectDir = new File(rootProject.projectDir, "tooling/hibernate-reveng")

rootProject.children.each { project ->
project.buildFileName = "${project.name}.gradle"
assert project.projectDir.isDirectory()
Expand Down
1 change: 1 addition & 0 deletions tooling/hibernate-ant/hibernate-ant.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ description = 'Annotation Processor to generate JPA 2 static metamodel classes'
dependencies {
compileOnly libs.ant
implementation project( ':hibernate-core' )
implementation project( ':hibernate-reveng')
testImplementation libs.ant
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.tool.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.hibernate.tool.reveng.api.metadata.MetadataDescriptor;
import org.hibernate.tool.reveng.api.metadata.MetadataDescriptorFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

/**
* @author max
*
*/
public class ConfigurationTask extends Task {

List<FileSet> fileSets = new ArrayList<FileSet>();
MetadataDescriptor metadataDescriptor;
File configurationFile;
File propertyFile;
protected String entityResolver;

public ConfigurationTask() {
setDescription( "Standard Configuration" );
}

public void addConfiguredFileSet(FileSet fileSet) {
fileSets.add( fileSet );
}

public final MetadataDescriptor getMetadataDescriptor() {
if ( metadataDescriptor == null ) {
metadataDescriptor = createMetadataDescriptor();
}
return metadataDescriptor;
}

protected MetadataDescriptor createMetadataDescriptor() {
return MetadataDescriptorFactory
.createNativeDescriptor(
configurationFile,
getFiles(),
loadPropertiesFile() );
}

protected Properties loadPropertiesFile() {
if ( propertyFile != null ) {
Properties properties = new Properties(); // TODO: should we "inherit" from the ant projects properties ?
try (FileInputStream is = new FileInputStream( propertyFile )) {
properties.load( is );
return properties;
}
catch (FileNotFoundException e) {
throw new BuildException( propertyFile + " not found.", e );
}
catch (IOException e) {
throw new BuildException( "Problem while loading " + propertyFile, e );
}
}
else {
return null;
}
}


protected File[] getFiles() {

List<File> files = new LinkedList<File>();
for ( FileSet fs : fileSets ) {

DirectoryScanner ds = fs.getDirectoryScanner( getProject() );

String[] dsFiles = ds.getIncludedFiles();
for ( String dsFile : dsFiles ) {
File f = new File( dsFile );
if ( !f.isFile() ) {
f = new File( ds.getBasedir(), dsFile );
}

files.add( f );
}
}

return files.toArray( new File[0] );
}


public File getConfigurationFile() {
return configurationFile;
}

public void setConfigurationFile(File configurationFile) {
this.configurationFile = configurationFile;
}

public File getPropertyFile() {
return propertyFile;
}

public void setPropertyFile(File propertyFile) {
this.propertyFile = propertyFile;
}

public void setEntityResolver(String entityResolverName) {
this.entityResolver = entityResolverName;
}

public void setNamingStrategy(String namingStrategy) {
log("setting unused naming strategy: " + namingStrategy);
}

@Override
public Object clone() throws CloneNotSupportedException {
ConfigurationTask ct = (ConfigurationTask) super.clone();
ct.fileSets.addAll( this.fileSets );
ct.metadataDescriptor = this.metadataDescriptor;
ct.propertyFile = this.propertyFile;
ct.entityResolver = this.entityResolver;
return ct;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.tool.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.PropertySet;
import org.hibernate.tool.reveng.api.export.Exporter;
import org.hibernate.tool.reveng.api.export.ExporterConstants;

import java.io.File;
import java.util.Properties;

/**
* @author max
*
* Is not actually a ant task, but simply just a task part of a HibernateToolTask
*
*/
public abstract class ExporterTask {

// refactor out so not dependent on Ant ?
protected HibernateToolTask parent;
Properties properties;
private Path templatePath;

public ExporterTask(HibernateToolTask parent) {
this.parent = parent;
this.properties = new Properties();
}


public void execute() {

Exporter exporter = configureExporter(createExporter() );
exporter.start();

}

protected abstract Exporter createExporter();

public File getDestdir() {
File destdir = (File)this.properties.get(ExporterConstants.DESTINATION_FOLDER);
if(destdir==null) {
return parent.getDestDir();
}
else {
return destdir;
}
}
public void setDestdir(File destdir) {
this.properties.put(ExporterConstants.DESTINATION_FOLDER, destdir);
}

public void setTemplatePath(Path path) {
templatePath = path;
}

public void validateParameters() {
if(getDestdir()==null) {
throw new BuildException("destdir must be set, either locally or on <hibernatetool>");
}
}

public void addConfiguredPropertySet(PropertySet ps) {
properties.putAll(ps.getProperties());
}

public void addConfiguredProperty(Environment.Variable property) {
properties.put(property.getKey(), property.getValue());
}

protected Path getTemplatePath() {
if(templatePath==null) {
return parent.getTemplatePath();
}
else {
return templatePath;
}
}


abstract String getName();

protected Exporter configureExporter(Exporter exporter) {
Properties prop = new Properties();
prop.putAll(parent.getProperties());
prop.putAll(properties);
exporter.getProperties().putAll(prop);
exporter.getProperties().put(ExporterConstants.METADATA_DESCRIPTOR, parent.getMetadataDescriptor());
exporter.getProperties().put(ExporterConstants.DESTINATION_FOLDER, getDestdir());
exporter.getProperties().put(ExporterConstants.TEMPLATE_PATH, getTemplatePath().list());
return exporter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.tool.ant;

import org.hibernate.tool.reveng.api.export.Exporter;
import org.hibernate.tool.reveng.api.export.ExporterConstants;
import org.hibernate.tool.reveng.api.export.ExporterFactory;
import org.hibernate.tool.reveng.api.export.ExporterType;

/**
* @author max
*
*/
public class GenericExporterTask extends ExporterTask {

public GenericExporterTask(HibernateToolTask parent) {
super(parent);
}

String templateName;
String exporterClass;
String filePattern;
String forEach;

/**
* The FilePattern defines the pattern used to generate files.
* @param filePattern
*/
public void setFilePattern(String filePattern) {
this.filePattern = filePattern;
}

public void setForEach(String forEach) {
this.forEach = forEach;
}

public void setTemplate(String templateName) {
this.templateName = templateName;
}

public void setExporterClass(String exporterClass) {
this.exporterClass = exporterClass;
}

protected Exporter createExporter() {
if (exporterClass == null) {
return ExporterFactory.createExporter(ExporterType.GENERIC);
}
else {
return ExporterFactory.createExporter(exporterClass);
}
}

protected Exporter configureExporter(Exporter exp) {
super.configureExporter(exp);
if (templateName != null) {
exp.getProperties().put(ExporterConstants.TEMPLATE_NAME, templateName);
}
if (filePattern != null) {
exp.getProperties().put(ExporterConstants.FILE_PATTERN, filePattern);
}
if (forEach != null) {
exp.getProperties().put(ExporterConstants.FOR_EACH, forEach);
}
return exp;
}

public String getName() {
StringBuffer buf = new StringBuffer("generic exporter");
if(exporterClass!=null) {
buf.append( "class: " + exporterClass);
}
if(templateName!=null) {
buf.append( "template: " + templateName);
}
return buf.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.tool.ant;

import org.hibernate.tool.reveng.api.export.Exporter;
import org.hibernate.tool.reveng.api.export.ExporterFactory;
import org.hibernate.tool.reveng.api.export.ExporterType;

public class Hbm2CfgXmlExporterTask extends ExporterTask {

private boolean ejb3;

public Hbm2CfgXmlExporterTask(HibernateToolTask parent) {
super(parent);
}

public Exporter createExporter() {
return ExporterFactory.createExporter(ExporterType.CFG);
}

public void setEjb3(boolean ejb3) {
this.ejb3 = ejb3;
}

public String getName() {
return "hbm2cfgxml (Generates hibernate.cfg.xml)";
}

protected Exporter configureExporter(Exporter exporter) {
super.configureExporter( exporter );
exporter.getProperties().setProperty("ejb3", ""+ejb3);
return exporter;
}

}
Loading
Loading