+
+ /** Try to read configuration from properties-file */
+ try
+ {
+ if (hibernateProperties == null)
+ {
+ 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
+ {
+ 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)
+ {
+ getLog().error("Error while reading properties!", e);
+ throw new MojoExecutionException(e.getMessage());
+ }
+
+ /** Try to read configuration from configuration-file */
+ try
+ {
+ if (hibernateConfig == null)
+ {
+ 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
+ {
+ 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)
+ {
+ getLog().error("Error while reading configuration!", e);
+ throw new MojoExecutionException(e.getMessage());
+ }
+
+ ParsedPersistenceXmlDescriptor persistenceUnitDescriptor =
+ getPersistenceUnitDescriptor(
+ persistenceUnit,
+ config.getProperties(),
+ new MavenProjectClassLoaderService(classLoader)
+ );
+ 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))
+ getLog().debug(
+ "Overwriting property " +
+ DRIVER_CLASS + "=" + config.getProperty(DRIVER_CLASS) +
+ " with the value " + driverClassName
+ );
+ else
+ 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))
+ getLog().debug(
+ "Overwriting property " +
+ URL + "=" + config.getProperty(URL) +
+ " with the value " + url
+ );
+ else
+ 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))
+ getLog().debug(
+ "Overwriting property " +
+ USERNAME + "=" + config.getProperty(USERNAME) +
+ " with the value " + username
+ );
+ else
+ 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))
+ getLog().debug(
+ "Overwriting property " +
+ PASSWORD + "=" + config.getProperty(PASSWORD) +
+ " with value " + password
+ );
+ else
+ 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))
+ getLog().debug(
+ "Overwriting property " +
+ DIALECT + "=" + config.getProperty(DIALECT) +
+ " with value " + hibernateDialect
+ );
+ else
+ getLog().debug(
+ "Using value " + hibernateDialect + " for property " + DIALECT
+ );
+ config.setProperty(DIALECT, hibernateDialect);
+ }
+
+ if ( hibernateNamingStrategy != null )
+ {
+ if ( config.getProperties().contains(NAMING_STRATEGY))
+ getLog().debug(
+ "Overwriting property " +
+ NAMING_STRATEGY + "=" + config.getProperty(NAMING_STRATEGY) +
+ " with value " + hibernateNamingStrategy
+ );
+ else
+ getLog().debug(
+ "Using value " + hibernateNamingStrategy + " for property " +
+ NAMING_STRATEGY
+ );
+ config.setProperty(NAMING_STRATEGY, hibernateNamingStrategy);
+ }
+
+ /** The generated SQL varies with the dialect! */
+ if (md5s.containsKey(DIALECT))
+ {
+ String dialect = config.getProperty(DIALECT);
+ if (md5s.get(DIALECT).equals(dialect))
+ getLog().debug("SQL-dialect unchanged.");
+ else
+ {
+ modified = true;
+ if (dialect == null)
+ {
+ getLog().debug("SQL-dialect was unset.");
+ md5s.remove(DIALECT);
+ }
+ else
+ {
+ getLog().debug("SQL-dialect changed: " + dialect);
+ md5s.put(DIALECT, dialect);
+ }
+ }
+ }
+ else
+ {
+ String dialect = config.getProperty(DIALECT);
+ if (dialect != null)
+ {
+ modified = true;
+ md5s.put(DIALECT, config.getProperty(DIALECT));
+ }
+ }
+
+ /** The generated SQL varies with the envers-configuration */
+ if (md5s.get(ENVERS) != null)
+ {
+ if (md5s.get(ENVERS).equals(Boolean.toString(envers)))
+ getLog().debug("Envers-Configuration unchanged. Enabled: " + envers);
+ else
+ {
+ getLog().debug("Envers-Configuration changed. Enabled: " + envers);
+ modified = true;
+ md5s.put(ENVERS, Boolean.toString(envers));
+ }
+ }
+ else
+ {
+ modified = true;
+ md5s.put(ENVERS, Boolean.toString(envers));
+ }
+
+ if (config.getProperties().isEmpty())
+ {
+ getLog().error("No properties set!");
+ throw new MojoFailureException("Hibernate configuration is missing!");
+ }
+
+ getLog().info("Gathered hibernate-configuration (turn on debugging for details):");
+ for (Entry<Object,Object> entry : config.getProperties().entrySet())
+ getLog().info(" " + entry.getKey() + " = " + entry.getValue());
+
+
+ getLog().debug("Adding explicitly configured mappings...");
+ if (hibernateMapping != null)
+ {
+ try
+ {
+ MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
+ for (String filename : hibernateMapping.split("[\\s,]+"))
+ {
+ // First try the filename as absolute/relative path
+ File file = new File(filename);
+ if (!file.exists())
+ {
+ // If the file was not found, search for it in the resource-directories
+ for (Resource resource : project.getResources())
+ {
+ file = new File(resource.getDirectory() + File.separator + filename);
+ if (file.exists())
+ break;
+ }
+ }
+ if (file != null && file.exists())
+ {
+ InputStream is = new FileInputStream(file);
+ byte[] buffer = new byte[1024*4]; // copy data in 4MB-chunks
+ int i;
+ while((i = is.read(buffer)) > -1)
+ digest.update(buffer, 0, i);
+ is.close();
+ byte[] bytes = digest.digest();
+ BigInteger bi = new BigInteger(1, bytes);
+ String newMd5 = String.format("%0" + (bytes.length << 1) + "x", bi);
+ String oldMd5 = !md5s.containsKey(filename) ? "" : md5s.get(filename);
+ if (!newMd5.equals(oldMd5))
+ {
+ getLog().debug("Found new or modified mapping-file: " + filename);
+ modified = true;
+ md5s.put(filename, newMd5);
+ }
+ else
+ {
+ getLog().debug(oldMd5 + " -> mapping-file unchanged: " + filename);
+ }
+ getLog().debug("Adding mappings from XML-configurationfile: " + file);
+ config.addFile(file);
+ }
+ else
+ throw new MojoFailureException("File " + filename + " could not be found in any of the configured resource-directories!");
+ }
+ }
+ catch (NoSuchAlgorithmException e)
+ {
+ throw new MojoFailureException("Cannot calculate MD5 sums!", e);
+ }
+ catch (FileNotFoundException e)
+ {
+ throw new MojoFailureException("Cannot calculate MD5 sums!", e);
+ }
+ catch (IOException e)
+ {
+ throw new MojoFailureException("Cannot calculate MD5 sums!", e);
+ }
+ }
+
+ getLog().debug("Adding annotated classes to hibernate-mapping-configuration...");
+ // build annotated packages
+ Set<String> packages = new HashSet<String>();
+ for (Class<?> annotatedClass : classes)
+ {
+ String packageName = annotatedClass.getPackage().getName();
+ if (!packages.contains(packageName))
+ {
+ getLog().debug("Add package " + packageName);
+ packages.add(packageName);
+ config.addPackage(packageName);
+ getLog().debug("type definintions" + config.getTypeDefs());
+ }
+ getLog().debug("Class " + annotatedClass);
+ config.addAnnotatedClass(annotatedClass);
+ }
+
+ Target target = null;
+ try
+ {
+ target = Target.valueOf(this.target.toUpperCase());
+ }
+ catch (IllegalArgumentException e)
+ {
+ getLog().error("Invalid value for configuration-option \"target\": " + this.target);
+ getLog().error("Valid values are: NONE, SCRIPT, EXPORT, BOTH");
+ throw new MojoExecutionException("Invalid value for configuration-option \"target\"");
+ }
+ Type type = null;
+ try
+ {
+ type = Type.valueOf(this.type.toUpperCase());
+ }
+ catch (IllegalArgumentException e)
+ {
+ getLog().error("Invalid value for configuration-option \"type\": " + this.type);
+ getLog().error("Valid values are: NONE, CREATE, DROP, BOTH");
+ throw new MojoExecutionException("Invalid value for configuration-option \"type\"");
+ }
+
+
+ if (config.getProperty(DIALECT) == null)
+ throw new MojoFailureException("hibernate-dialect must be set!");
+
+
+ if (target.equals(Target.SCRIPT) || target.equals(Target.NONE))
+ {
+ project.getProperties().setProperty(EXPORT_SKIPPED_PROPERTY, "true");
+ }
+ if (
+ !modified
+ && !target.equals(Target.SCRIPT)
+ && !target.equals(Target.NONE)
+ && !force
+ )
+ {
+ getLog().info("No modified annotated classes or mapping-files found and dialect unchanged.");
+ getLog().info("Skipping schema generation!");
+ project.getProperties().setProperty(EXPORT_SKIPPED_PROPERTY, "true");
+ return;
+ }
+
+
+ if ( config.getProperties().containsKey(NAMING_STRATEGY))
+ {
+ String namingStrategy = config.getProperty(NAMING_STRATEGY);
+ getLog().debug("Explicitly set NamingStrategy: " + namingStrategy);
+ try
+ {
+ @SuppressWarnings("unchecked")
+ Class<NamingStrategy> namingStrategyClass = (Class<NamingStrategy>) Class.forName(namingStrategy);
+ config.setNamingStrategy(namingStrategyClass.newInstance());
+ }
+ catch (Exception e)
+ {
+ getLog().error("Error setting NamingStrategy", e);
+ throw new MojoExecutionException(e.getMessage());
+ }
+ }
+
+
+ Environment.verifyProperties(config.getProperties());
+ ConfigurationHelper.resolvePlaceHolders(config.getProperties());
+ registry =
+ (StandardServiceRegistryImpl)
+ new StandardServiceRegistryBuilder()
+ .applySettings(config.getProperties())
+ .build();
+
+ config.buildMappings();
+
+ if (envers)
+ {
+ getLog().info("Automatic auditing via hibernate-envers enabled!");
+ AuditConfiguration.getFor(config);
+ }
+
+ SchemaExport export = new SchemaExport(registry, config);