X-Git-Url: https://juplo.de/gitweb/?p=hibernate4-maven-plugin;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fplugins%2Fhibernate4%2FHbm2DdlMojo.java;h=f8c0634424772e8d374093fbd0f6a01bbc47ea74;hp=3efa1852fe9cbc47cbe0350d0c435079f157aff2;hb=7e3e9b90d61b077e48b59fc0eb63059886c68cf5;hpb=1e5cca792c49d60e20d7355eb97b13d591d80af6 diff --git a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java index 3efa1852..f8c06344 100644 --- a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java @@ -38,8 +38,8 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -70,7 +70,6 @@ import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor; import org.hibernate.jpa.boot.internal.PersistenceXmlParser; import org.hibernate.jpa.boot.spi.ProviderChecker; -import org.hibernate.mapping.PersistentClass; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaExport.Type; import org.hibernate.tool.hbm2ddl.Target; @@ -98,6 +97,11 @@ public class Hbm2DdlMojo extends AbstractMojo public final static String NAMING_STRATEGY="hibernate.ejb.naming_strategy"; public final static String ENVERS = "hibernate.export.envers"; + public final static String JPA_DRIVER = "javax.persistence.jdbc.driver"; + public final static String JPA_URL = "javax.persistence.jdbc.url"; + public final static String JPA_USERNAME = "javax.persistence.jdbc.user"; + public final static String JPA_PASSWORD = "javax.persistence.jdbc.password"; + public final static String MD5S = "hibernate4-generatedschema.md5s"; private final static Pattern split = Pattern.compile("[^,\\s]+"); @@ -259,8 +263,11 @@ public class Hbm2DdlMojo extends AbstractMojo /** * Path to Hibernate properties file. + * If this parameter is not set the plugin will try to load the configuration + * from a file hibernate.properties on the classpath. The + * test-classpath takes precedence. * - * @parameter default-value="${project.build.outputDirectory}/hibernate.properties" + * @parameter * @since 1.0 */ private String hibernateProperties; @@ -268,8 +275,11 @@ public class Hbm2DdlMojo extends AbstractMojo /** * Path to Hibernate configuration file (.cfg.xml). * Settings in this file will overwrite settings in the properties file. + * If this parameter is not set the plugin will try to load the configuration + * from a file hibernate.cfg.xml on the classpath. The + * test-classpath takes precedence. * - * @parameter default-value="${project.build.outputDirectory}/hibernate.cfg.xml" + * @parameter * @since 1.1.0 */ private String hibernateConfig; @@ -281,6 +291,7 @@ public class Hbm2DdlMojo extends AbstractMojo * Settings in this file will overwrite settings in the properties or the * configuration file. * + * @parameter * @since 1.1.0 */ private String persistenceUnit; @@ -405,20 +416,39 @@ public class Hbm2DdlMojo extends AbstractMojo } } - ClassLoader classLoader = null; + URLClassLoader classLoader = null; try { getLog().debug("Creating ClassLoader for project-dependencies..."); List classpathFiles = project.getCompileClasspathElements(); if (scanTestClasses) classpathFiles.addAll(project.getTestClasspathElements()); - URL[] urls = new URL[classpathFiles.size()]; - for (int i = 0; i < classpathFiles.size(); ++i) + List urls = new LinkedList(); + File file; + file = new File(testOutputDirectory); + if (!file.exists()) + { + getLog().info("creating test-output-directory: " + testOutputDirectory); + file.mkdirs(); + } + urls.add(file.toURI().toURL()); + file = new File(outputDirectory); + if (!file.exists()) + { + getLog().info("creating output-directory: " + outputDirectory); + file.mkdirs(); + } + urls.add(file.toURI().toURL()); + for (String pathElement : classpathFiles) { - getLog().debug("Dependency: " + classpathFiles.get(i)); - urls[i] = new File(classpathFiles.get(i)).toURI().toURL(); + getLog().debug("Dependency: " + pathElement); + urls.add(new File(pathElement).toURI().toURL()); } - classLoader = new URLClassLoader(urls, getClass().getClassLoader()); + classLoader = + new URLClassLoader( + urls.toArray(new URL[urls.size()]), + getClass().getClassLoader() + ); } catch (Exception e) { @@ -541,23 +571,51 @@ public class Hbm2DdlMojo extends AbstractMojo // Clear unused system-properties config.setProperties(new Properties()); + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + StandardServiceRegistryImpl registry = null; MavenLogAppender.startPluginLog(this); + try { + /** + * Change class-loader of current thread, so that hibernate can + * see all dependencies! + */ + Thread.currentThread().setContextClassLoader(classLoader); + + /** Try to read configuration from properties-file */ try { - File file = new File(hibernateProperties); - if (file.exists()) + if (hibernateProperties == null) { - getLog().info("Reading properties from file " + hibernateProperties + "..."); - Properties properties = new Properties(); - properties.load(new FileInputStream(file)); - config.setProperties(properties); + URL url = classLoader.findResource("hibernate.properties"); + if (url == null) + { + getLog().info("No hibernate.properties on the classpath!"); + } + else + { + getLog().info("Reading settings from hibernate.properties on the classpath."); + Properties properties = new Properties(); + properties.load(url.openStream()); + config.setProperties(properties); + } } else - getLog().info("No hibernate-properties-file found! (Checked path: " + hibernateProperties + ")"); + { + File file = new File(hibernateProperties); + if (file.exists()) + { + getLog().info("Reading settings from file " + hibernateProperties + "..."); + Properties properties = new Properties(); + properties.load(new FileInputStream(file)); + config.setProperties(properties); + } + else + getLog().info("No hibernate-properties-file found! (Checked path: " + hibernateProperties + ")"); + } } catch (IOException e) { @@ -565,23 +623,33 @@ public class Hbm2DdlMojo extends AbstractMojo throw new MojoExecutionException(e.getMessage()); } - /** - * Change class-loader of current thread, so that hibernate can - * see all dependencies! - */ - Thread.currentThread().setContextClassLoader(classLoader); - /** Try to read configuration from configuration-file */ try { - File file = new File(hibernateConfig); - if (file.exists()) + if (hibernateConfig == null) { - getLog().info("Reading configuration from file " + hibernateConfig + "..."); - config.configure(file); + URL url = classLoader.findResource("hibernate.cfg.xml"); + if (url == null) + { + getLog().info("No hibernate.cfg.xml on the classpath!"); + } + else + { + getLog().info("Reading settings from hibernate.cfg.xml on the classpath."); + config.configure(url); + } } else - getLog().info("No hibernate-configuration-file found! (Checked path: " + hibernateConfig + ")"); + { + File file = new File(hibernateConfig); + if (file.exists()) + { + getLog().info("Reading configuration from file " + hibernateConfig + "..."); + config.configure(file); + } + else + getLog().info("No hibernate-configuration-file found! (Checked path: " + hibernateConfig + ")"); + } } catch (Exception e) { @@ -598,7 +666,9 @@ public class Hbm2DdlMojo extends AbstractMojo if (persistenceUnitDescriptor != null) config.setProperties(persistenceUnitDescriptor.getProperties()); + /** Overwrite values from properties-file or set, if given */ + if (driverClassName != null) { if (config.getProperties().containsKey(DRIVER_CLASS)) @@ -611,6 +681,21 @@ public class Hbm2DdlMojo extends AbstractMojo getLog().debug("Using the value " + driverClassName); config.setProperty(DRIVER_CLASS, driverClassName); } + if (config.getProperty(DRIVER_CLASS) == null) + { + String driver = config.getProperty(JPA_DRIVER); + if (driver != null) + { + getLog().info( + DRIVER_CLASS + + " is not set. Borrow setting from " + + JPA_DRIVER + + ": " + + driver); + config.setProperty(DRIVER_CLASS, driver); + } + } + if (url != null) { if (config.getProperties().containsKey(URL)) @@ -623,6 +708,21 @@ public class Hbm2DdlMojo extends AbstractMojo getLog().debug("Using the value " + url); config.setProperty(URL, url); } + if (config.getProperty(URL) == null) + { + String url = config.getProperty(JPA_URL); + if (url != null) + { + getLog().info( + URL + + " is not set. Borrow setting from " + + JPA_URL + + ": " + + url); + config.setProperty(URL, url); + } + } + if (username != null) { if (config.getProperties().containsKey(USERNAME)) @@ -635,6 +735,21 @@ public class Hbm2DdlMojo extends AbstractMojo getLog().debug("Using the value " + username); config.setProperty(USERNAME, username); } + if (config.getProperty(USERNAME) == null) + { + String username = config.getProperty(JPA_USERNAME); + if (username != null) + { + getLog().info( + USERNAME + + " is not set. Borrow setting from " + + JPA_USERNAME + + ": " + + username); + config.setProperty(USERNAME, username); + } + } + if (password != null) { if (config.getProperties().containsKey(PASSWORD)) @@ -647,6 +762,21 @@ public class Hbm2DdlMojo extends AbstractMojo getLog().debug("Using value " + password + " for property " + PASSWORD); config.setProperty(PASSWORD, password); } + if (config.getProperty(PASSWORD) == null) + { + String password = config.getProperty(JPA_PASSWORD); + if (password != null) + { + getLog().info( + PASSWORD + + " is not set. Borrow setting from " + + JPA_PASSWORD + + ": " + + password); + config.setProperty(PASSWORD, password); + } + } + if (hibernateDialect != null) { if (config.getProperties().containsKey(DIALECT)) @@ -661,6 +791,7 @@ public class Hbm2DdlMojo extends AbstractMojo ); config.setProperty(DIALECT, hibernateDialect); } + if ( hibernateNamingStrategy != null ) { if ( config.getProperties().contains(NAMING_STRATEGY)) @@ -841,20 +972,6 @@ public class Hbm2DdlMojo extends AbstractMojo } - Iterator it = config.getClassMappings(); - if (!it.hasNext()) - { - throw new MojoFailureException("No mapped classes found!"); - } - else - { - getLog().info("Mapped classes:"); - while (it.hasNext()) - { - getLog().debug(" " + it.next().getClassName()); - } - } - if (config.getProperty(DIALECT) == null) throw new MojoFailureException("hibernate-dialect must be set!"); @@ -897,7 +1014,7 @@ public class Hbm2DdlMojo extends AbstractMojo Environment.verifyProperties(config.getProperties()); ConfigurationHelper.resolvePlaceHolders(config.getProperties()); - StandardServiceRegistryImpl registry = + registry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder() .applySettings(config.getProperties()) @@ -952,6 +1069,9 @@ public class Hbm2DdlMojo extends AbstractMojo /** Restore the old class-loader (TODO: is this really necessary?) */ Thread.currentThread().setContextClassLoader(contextClassLoader); + + if (registry != null) + registry.destroy(); } /** Write md5-sums for annotated classes to file */