X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fplugins%2Fhibernate4%2FHbm2DdlMojo.java;h=f8c0634424772e8d374093fbd0f6a01bbc47ea74;hb=fdda82a6f76deefd10f83da89d7e82054e3c3ecd;hp=f1b6a2cdb64a0dca74297b09430aacc7819af662;hpb=2a85cb05542795f9cd2eed448f212f92842a85e8;p=hibernate-maven-plugin
diff --git a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java
index f1b6a2cd..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)
{
@@ -548,19 +578,44 @@ public class Hbm2DdlMojo extends AbstractMojo
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)
{
@@ -568,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)
{
@@ -601,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))
@@ -614,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))
@@ -626,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))
@@ -638,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))
@@ -650,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))
@@ -664,6 +791,7 @@ public class Hbm2DdlMojo extends AbstractMojo
);
config.setProperty(DIALECT, hibernateDialect);
}
+
if ( hibernateNamingStrategy != null )
{
if ( config.getProperties().contains(NAMING_STRATEGY))