Skip to content

Commit a0eb131

Browse files
committed
HHH-19879: Move Hibernate Tools' reveng module to Hibernate ORM and merge the relevant ant/gradle/maven plugins
- Add the reveng Ant tasks Signed-off-by: Koen Aers <koen.aers@gmail.com>
1 parent 2b8ab99 commit a0eb131

22 files changed

+2438
-1
lines changed

tooling/hibernate-ant/hibernate-ant.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ description = 'Annotation Processor to generate JPA 2 static metamodel classes'
1010
dependencies {
1111
compileOnly libs.ant
1212
implementation project( ':hibernate-core' )
13+
implementation project( ':hibernate-reveng')
1314
testImplementation libs.ant
1415
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.tool.ant;
6+
7+
import org.apache.tools.ant.BuildException;
8+
import org.apache.tools.ant.DirectoryScanner;
9+
import org.apache.tools.ant.Task;
10+
import org.apache.tools.ant.types.FileSet;
11+
import org.hibernate.tool.reveng.api.metadata.MetadataDescriptor;
12+
import org.hibernate.tool.reveng.api.metadata.MetadataDescriptorFactory;
13+
14+
import java.io.File;
15+
import java.io.FileInputStream;
16+
import java.io.FileNotFoundException;
17+
import java.io.IOException;
18+
import java.util.ArrayList;
19+
import java.util.LinkedList;
20+
import java.util.List;
21+
import java.util.Properties;
22+
23+
/**
24+
* @author max
25+
*
26+
*/
27+
public class ConfigurationTask extends Task {
28+
29+
List<FileSet> fileSets = new ArrayList<FileSet>();
30+
MetadataDescriptor metadataDescriptor;
31+
File configurationFile;
32+
File propertyFile;
33+
protected String entityResolver;
34+
35+
public ConfigurationTask() {
36+
setDescription( "Standard Configuration" );
37+
}
38+
39+
public void addConfiguredFileSet(FileSet fileSet) {
40+
fileSets.add( fileSet );
41+
}
42+
43+
public final MetadataDescriptor getMetadataDescriptor() {
44+
if ( metadataDescriptor == null ) {
45+
metadataDescriptor = createMetadataDescriptor();
46+
}
47+
return metadataDescriptor;
48+
}
49+
50+
protected MetadataDescriptor createMetadataDescriptor() {
51+
return MetadataDescriptorFactory
52+
.createNativeDescriptor(
53+
configurationFile,
54+
getFiles(),
55+
loadPropertiesFile() );
56+
}
57+
58+
protected Properties loadPropertiesFile() {
59+
if ( propertyFile != null ) {
60+
Properties properties = new Properties(); // TODO: should we "inherit" from the ant projects properties ?
61+
try (FileInputStream is = new FileInputStream( propertyFile )) {
62+
properties.load( is );
63+
return properties;
64+
}
65+
catch (FileNotFoundException e) {
66+
throw new BuildException( propertyFile + " not found.", e );
67+
}
68+
catch (IOException e) {
69+
throw new BuildException( "Problem while loading " + propertyFile, e );
70+
}
71+
}
72+
else {
73+
return null;
74+
}
75+
}
76+
77+
78+
protected File[] getFiles() {
79+
80+
List<File> files = new LinkedList<File>();
81+
for ( FileSet fs : fileSets ) {
82+
83+
DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
84+
85+
String[] dsFiles = ds.getIncludedFiles();
86+
for ( String dsFile : dsFiles ) {
87+
File f = new File( dsFile );
88+
if ( !f.isFile() ) {
89+
f = new File( ds.getBasedir(), dsFile );
90+
}
91+
92+
files.add( f );
93+
}
94+
}
95+
96+
return files.toArray( new File[0] );
97+
}
98+
99+
100+
public File getConfigurationFile() {
101+
return configurationFile;
102+
}
103+
104+
public void setConfigurationFile(File configurationFile) {
105+
this.configurationFile = configurationFile;
106+
}
107+
108+
public File getPropertyFile() {
109+
return propertyFile;
110+
}
111+
112+
public void setPropertyFile(File propertyFile) {
113+
this.propertyFile = propertyFile;
114+
}
115+
116+
public void setEntityResolver(String entityResolverName) {
117+
this.entityResolver = entityResolverName;
118+
}
119+
120+
public void setNamingStrategy(String namingStrategy) {
121+
}
122+
123+
@Override
124+
public Object clone() throws CloneNotSupportedException {
125+
ConfigurationTask ct = (ConfigurationTask) super.clone();
126+
ct.fileSets.addAll( this.fileSets );
127+
ct.metadataDescriptor = this.metadataDescriptor;
128+
ct.propertyFile = this.propertyFile;
129+
ct.entityResolver = this.entityResolver;
130+
return ct;
131+
}
132+
133+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.tool.ant;
6+
7+
import org.apache.tools.ant.BuildException;
8+
import org.apache.tools.ant.types.Environment;
9+
import org.apache.tools.ant.types.Path;
10+
import org.apache.tools.ant.types.PropertySet;
11+
import org.hibernate.tool.reveng.api.export.Exporter;
12+
import org.hibernate.tool.reveng.api.export.ExporterConstants;
13+
14+
import java.io.File;
15+
import java.util.Properties;
16+
17+
/**
18+
* @author max
19+
*
20+
* Is not actually a ant task, but simply just a task part of a HibernateToolTask
21+
*
22+
*/
23+
public abstract class ExporterTask {
24+
25+
// refactor out so not dependent on Ant ?
26+
protected HibernateToolTask parent;
27+
Properties properties;
28+
private Path templatePath;
29+
30+
public ExporterTask(HibernateToolTask parent) {
31+
this.parent = parent;
32+
this.properties = new Properties();
33+
}
34+
35+
36+
/*final*/ public void execute() {
37+
38+
Exporter exporter = configureExporter(createExporter() );
39+
exporter.start();
40+
41+
}
42+
43+
protected abstract Exporter createExporter();
44+
45+
public File getDestdir() {
46+
File destdir = (File)this.properties.get(ExporterConstants.DESTINATION_FOLDER);
47+
if(destdir==null) {
48+
return parent.getDestDir();
49+
}
50+
else {
51+
return destdir;
52+
}
53+
}
54+
public void setDestdir(File destdir) {
55+
this.properties.put(ExporterConstants.DESTINATION_FOLDER, destdir);
56+
}
57+
58+
public void setTemplatePath(Path path) {
59+
templatePath = path;
60+
}
61+
62+
public void setTemplatePrefix(String s) {
63+
}
64+
65+
public void validateParameters() {
66+
if(getDestdir()==null) {
67+
throw new BuildException("destdir must be set, either locally or on <hibernatetool>");
68+
}
69+
}
70+
71+
public void addConfiguredPropertySet(PropertySet ps) {
72+
properties.putAll(ps.getProperties());
73+
}
74+
75+
public void addConfiguredProperty(Environment.Variable property) {
76+
properties.put(property.getKey(), property.getValue());
77+
}
78+
79+
protected Path getTemplatePath() {
80+
if(templatePath==null) {
81+
return parent.getTemplatePath();
82+
}
83+
else {
84+
return templatePath;
85+
}
86+
}
87+
88+
89+
abstract String getName();
90+
91+
protected Exporter configureExporter(Exporter exporter) {
92+
Properties prop = new Properties();
93+
prop.putAll(parent.getProperties());
94+
prop.putAll(properties);
95+
exporter.getProperties().putAll(prop);
96+
exporter.getProperties().put(ExporterConstants.METADATA_DESCRIPTOR, parent.getMetadataDescriptor());
97+
exporter.getProperties().put(ExporterConstants.DESTINATION_FOLDER, getDestdir());
98+
exporter.getProperties().put(ExporterConstants.TEMPLATE_PATH, getTemplatePath().list());
99+
return exporter;
100+
}
101+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.tool.ant;
6+
7+
import org.hibernate.tool.reveng.api.export.Exporter;
8+
import org.hibernate.tool.reveng.api.export.ExporterConstants;
9+
import org.hibernate.tool.reveng.api.export.ExporterFactory;
10+
import org.hibernate.tool.reveng.api.export.ExporterType;
11+
12+
/**
13+
* @author max
14+
*
15+
*/
16+
public class GenericExporterTask extends ExporterTask {
17+
18+
public GenericExporterTask(HibernateToolTask parent) {
19+
super(parent);
20+
}
21+
22+
String templateName;
23+
String exporterClass;
24+
String filePattern;
25+
String forEach;
26+
27+
/**
28+
* The FilePattern defines the pattern used to generate files.
29+
* @param filePattern
30+
*/
31+
public void setFilePattern(String filePattern) {
32+
this.filePattern = filePattern;
33+
}
34+
35+
public void setForEach(String forEach) {
36+
this.forEach = forEach;
37+
}
38+
39+
public void setTemplate(String templateName) {
40+
this.templateName = templateName;
41+
}
42+
43+
public void setExporterClass(String exporterClass) {
44+
this.exporterClass = exporterClass;
45+
}
46+
47+
protected Exporter createExporter() {
48+
if (exporterClass == null) {
49+
return ExporterFactory.createExporter(ExporterType.GENERIC);
50+
}
51+
else {
52+
return ExporterFactory.createExporter(exporterClass);
53+
}
54+
}
55+
56+
protected Exporter configureExporter(Exporter exp) {
57+
super.configureExporter(exp);
58+
if (templateName != null) {
59+
exp.getProperties().put(ExporterConstants.TEMPLATE_NAME, templateName);
60+
}
61+
if (filePattern != null) {
62+
exp.getProperties().put(ExporterConstants.FILE_PATTERN, filePattern);
63+
}
64+
if (forEach != null) {
65+
exp.getProperties().put(ExporterConstants.FOR_EACH, forEach);
66+
}
67+
return exp;
68+
}
69+
70+
public String getName() {
71+
StringBuffer buf = new StringBuffer("generic exporter");
72+
if(exporterClass!=null) {
73+
buf.append( "class: " + exporterClass);
74+
}
75+
if(templateName!=null) {
76+
buf.append( "template: " + templateName);
77+
}
78+
return buf.toString();
79+
}
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.tool.ant;
6+
7+
import org.hibernate.tool.reveng.api.export.Exporter;
8+
import org.hibernate.tool.reveng.api.export.ExporterFactory;
9+
import org.hibernate.tool.reveng.api.export.ExporterType;
10+
11+
public class Hbm2CfgXmlExporterTask extends ExporterTask {
12+
13+
private boolean ejb3;
14+
15+
public Hbm2CfgXmlExporterTask(HibernateToolTask parent) {
16+
super(parent);
17+
}
18+
19+
public Exporter createExporter() {
20+
return ExporterFactory.createExporter(ExporterType.CFG);
21+
}
22+
23+
public void setEjb3(boolean ejb3) {
24+
this.ejb3 = ejb3;
25+
}
26+
27+
public String getName() {
28+
return "hbm2cfgxml (Generates hibernate.cfg.xml)";
29+
}
30+
31+
protected Exporter configureExporter(Exporter exporter) {
32+
super.configureExporter( exporter );
33+
exporter.getProperties().setProperty("ejb3", ""+ejb3);
34+
return exporter;
35+
}
36+
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.tool.ant;
6+
7+
import org.hibernate.tool.reveng.api.export.Exporter;
8+
import org.hibernate.tool.reveng.api.export.ExporterConstants;
9+
import org.hibernate.tool.reveng.api.export.ExporterFactory;
10+
import org.hibernate.tool.reveng.api.export.ExporterType;
11+
12+
/**
13+
* @author Dennis Byrne
14+
*/
15+
public class Hbm2DAOExporterTask extends Hbm2JavaExporterTask {
16+
17+
public Hbm2DAOExporterTask(HibernateToolTask parent) {
18+
super(parent);
19+
}
20+
21+
protected Exporter createExporter() {
22+
Exporter result = ExporterFactory.createExporter(ExporterType.DAO);
23+
result.getProperties().putAll(parent.getProperties());
24+
result.getProperties().put(ExporterConstants.METADATA_DESCRIPTOR, parent.getMetadataDescriptor());
25+
result.getProperties().put(ExporterConstants.DESTINATION_FOLDER, getDestdir());
26+
return result;
27+
}
28+
29+
public String getName() {
30+
return "hbm2dao (Generates a set of DAOs)";
31+
}
32+
33+
}

0 commit comments

Comments
 (0)