diff --git a/ant/docs/examples/cfgxml/build.xml b/ant/docs/examples/cfgxml/build.xml new file mode 100644 index 0000000000..0c56dd8ed6 --- /dev/null +++ b/ant/docs/examples/cfgxml/build.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + diff --git a/ant/docs/examples/cfgxml/hibernate.cfg.xml b/ant/docs/examples/cfgxml/hibernate.cfg.xml new file mode 100644 index 0000000000..50e16665b1 --- /dev/null +++ b/ant/docs/examples/cfgxml/hibernate.cfg.xml @@ -0,0 +1,11 @@ + + + + + org.h2.Driver + jdbc:h2:tcp://localhost/./sakila + sa + SAKILA + PUBLIC + + diff --git a/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java b/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java index d6e3f49375..33195dd85f 100644 --- a/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java +++ b/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java @@ -45,6 +45,23 @@ public void test5MinuteTutorial() throws Exception { assertTrue(personFile.exists()); } + @Test + public void testCfgXml() throws Exception { + File buildFile = new File(baseFolder, "cfgxml/build.xml"); + File cfgXmlFile = new File(baseFolder, "cfgxml/hibernate.cfg.xml"); + String cfgXmlFileContents = Files.readString(cfgXmlFile.toPath()) + .replace("jdbc:h2:tcp://localhost/./sakila", constructJdbcConnectionString()) + .replace(">sa<", "><") + .replace(">SAKILA<", ">TEST<"); + Files.writeString(cfgXmlFile.toPath(), cfgXmlFileContents); + Project project = createProject(buildFile); + assertNotNull(project); + File personFile = new File(baseFolder, "cfgxml/generated/Person.java"); + assertFalse(personFile.exists()); + project.executeTarget("reveng"); + assertTrue(personFile.exists()); + } + @Test public void testClasspath() throws Exception { PrintStream savedOut = System.out; diff --git a/ant/src/main/java/org/hibernate/tool/ant/JDBCConfigurationTask.java b/ant/src/main/java/org/hibernate/tool/ant/JDBCConfigurationTask.java index ca952ea03e..b4e44018f2 100644 --- a/ant/src/main/java/org/hibernate/tool/ant/JDBCConfigurationTask.java +++ b/ant/src/main/java/org/hibernate/tool/ant/JDBCConfigurationTask.java @@ -19,11 +19,15 @@ import java.io.File; import java.lang.reflect.Constructor; +import java.util.Map; import java.util.Properties; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Path; +import org.hibernate.boot.cfgxml.internal.ConfigLoader; +import org.hibernate.boot.cfgxml.spi.LoadedConfig; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.tool.api.metadata.MetadataDescriptor; import org.hibernate.tool.api.metadata.MetadataDescriptorFactory; import org.hibernate.tool.api.metadata.MetadataConstants; @@ -53,7 +57,7 @@ public JDBCConfigurationTask() { setDescription("JDBC Configuration (for reverse engineering)"); } protected MetadataDescriptor createMetadataDescriptor() { - Properties properties = loadPropertiesFile(); + Properties properties = loadProperties(); RevengStrategy res = createReverseEngineeringStrategy(); properties.put(MetadataConstants.PREFER_BASIC_COMPOSITE_IDS, preferBasicCompositeIds); return MetadataDescriptorFactory @@ -119,8 +123,8 @@ public void setDetectManyToMany(boolean b) { public void setDetectOptimisticLock(boolean b) { detectOptimisticLock = b; } - - private RevengStrategy loadreverseEngineeringStrategy(final String className, RevengStrategy delegate) + + private RevengStrategy loadreverseEngineeringStrategy(final String className, RevengStrategy delegate) throws BuildException { try { Class clazz = ReflectionUtil.classForName(className); @@ -144,4 +148,21 @@ private RevengStrategy loadreverseEngineeringStrategy(final String className, Re throw new BuildException("Could not create or find " + className + " with one argument delegate constructor", e); } } + + private Map loadCfgXmlFile() { + return new ConfigLoader(new BootstrapServiceRegistryBuilder().build()) + .loadConfigXmlFile(getConfigurationFile()) + .getConfigurationValues(); + } + + private Properties loadProperties() { + Properties result = new Properties(); + if (getPropertyFile() != null) { + result.putAll(loadPropertiesFile()); + } + if (getConfigurationFile() != null) { + result.putAll(loadCfgXmlFile()); + } + return result; + } }