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;
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;
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]+");
/**
* Path to Hibernate properties file.
+ * If this parameter is not set the plugin will try to load the configuration
+ * from a file <code>hibernate.properties</code> on the classpath. The
+ * test-classpath takes precedence.
*
- * @parameter default-value="${project.build.outputDirectory}/hibernate.properties"
+ * @parameter
* @since 1.0
*/
private String hibernateProperties;
/**
* 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 <code>hibernate.cfg.xml</code> 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;
* Settings in this file will overwrite settings in the properties or the
* configuration file.
*
+ * @parameter
* @since 1.1.0
*/
private String persistenceUnit;
}
}
- ClassLoader classLoader = null;
+ URLClassLoader classLoader = null;
try
{
getLog().debug("Creating ClassLoader for project-dependencies...");
List<String> classpathFiles = project.getCompileClasspathElements();
if (scanTestClasses)
classpathFiles.addAll(project.getTestClasspathElements());
- URL[] urls = new URL[classpathFiles.size()];
- for (int i = 0; i < classpathFiles.size(); ++i)
+ List<URL> urls = new LinkedList<URL>();
+ 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)
{
// 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)
{
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)
{
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("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("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("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("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))
);
config.setProperty(DIALECT, hibernateDialect);
}
+
if ( hibernateNamingStrategy != null )
{
if ( config.getProperties().contains(NAMING_STRATEGY))
}
- Iterator<PersistentClass> 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!");
Environment.verifyProperties(config.getProperties());
ConfigurationHelper.resolvePlaceHolders(config.getProperties());
- StandardServiceRegistryImpl registry =
+ registry =
(StandardServiceRegistryImpl)
new StandardServiceRegistryBuilder()
.applySettings(config.getProperties())
/** 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 */