- /** Overwrite values from propertie-file or set, if given */
- if (driverClassName != null)
- {
- if (properties.containsKey(DRIVER_CLASS))
- getLog().debug(
- "Overwriting property " +
- DRIVER_CLASS + "=" + properties.getProperty(DRIVER_CLASS) +
- " with the value " + driverClassName
- );
- else
- getLog().debug("Using the value " + driverClassName);
- properties.setProperty(DRIVER_CLASS, driverClassName);
- }
- if (url != null)
- {
- if (properties.containsKey(URL))
- getLog().debug(
- "Overwriting property " +
- URL + "=" + properties.getProperty(URL) +
- " with the value " + url
- );
- else
- getLog().debug("Using the value " + url);
- properties.setProperty(URL, url);
- }
- if (username != null)
- {
- if (properties.containsKey(USERNAME))
- getLog().debug(
- "Overwriting property " +
- USERNAME + "=" + properties.getProperty(USERNAME) +
- " with the value " + username
- );
- else
- getLog().debug("Using the value " + username);
- properties.setProperty(USERNAME, username);
- }
- if (password != null)
- {
- if (properties.containsKey(PASSWORD))
- getLog().debug(
- "Overwriting property " +
- PASSWORD + "=" + properties.getProperty(PASSWORD) +
- " with the value " + password
- );
- else
- getLog().debug("Using the value " + password);
- properties.setProperty(PASSWORD, password);
- }
- if (hibernateDialect != null)
- {
- if (properties.containsKey(DIALECT))
- getLog().debug(
- "Overwriting property " +
- DIALECT + "=" + properties.getProperty(DIALECT) +
- " with the value " + hibernateDialect
- );
+ /** 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);
+ }
+ }
+ }