import com.pyx4j.log4j.MavenLogAppender;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.Comparator;
import java.util.Enumeration;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
+import java.util.TreeSet;
+import java.util.logging.Logger;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
*/
public class Hbm2DdlMojo extends AbstractMojo
{
+ public final static String DRIVER_CLASS = "hibernate.connection.driver_class";
+ public final static String URL = "hibernate.connection.url";
+ public final static String USERNAME = "hibernate.connection.username";
+ public final static String PASSWORD = "hibernate.connection.password";
+ public final static String DIALECT = "hibernate.dialect";
+
+ private final static String TIMESTAMPS = "schema.timestamp";
+
/**
* The project whose project files to create.
*
MojoExecutionException
{
if (skip)
+ {
+ getLog().info("Exectuion of hibernate4-maven-plugin:export was skipped!");
return;
+ }
File dir = new File(outputDirectory);
if (!dir.exists())
throw new MojoExecutionException("Cannot scan for annotated classes in " + outputDirectory + ": directory does not exist!");
+ Map<String,Long> timestamps;
+ boolean modified = false;
+ File saved = new File(outputDirectory + File.separator + TIMESTAMPS);
+
+ if (saved.exists())
+ {
+ try
+ {
+ FileInputStream fis = new FileInputStream(saved);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+ timestamps = (HashMap<String,Long>)ois.readObject();
+ ois.close();
+ }
+ catch (Exception e)
+ {
+ timestamps = new HashMap<String,Long>();
+ getLog().warn("Cannot read timestamps from saved: " + e);
+ }
+ }
+ else
+ {
+ timestamps = new HashMap<String,Long>();
+ try
+ {
+ saved.createNewFile();
+ }
+ catch (IOException e)
+ {
+ getLog().warn("Cannot create saved for timestamps: " + e);
+ }
+ }
+
+ ClassLoader classLoader = null;
+ try
+ {
+ getLog().debug("Creating ClassLoader for project-dependencies...");
+ List<String> classpathFiles = project.getCompileClasspathElements();
+ URL[] urls = new URL[classpathFiles.size()];
+ for (int i = 0; i < classpathFiles.size(); ++i)
+ {
+ getLog().debug("Dependency: " + classpathFiles.get(i));
+ urls[i] = new File(classpathFiles.get(i)).toURI().toURL();
+ }
+ classLoader = new URLClassLoader(urls, getClass().getClassLoader());
+ }
+ catch (Exception e)
+ {
+ getLog().error("Error while creating ClassLoader!", e);
+ throw new MojoExecutionException(e.getMessage());
+ }
+
+ Set<Class<?>> classes =
+ new TreeSet<Class<?>>(
+ new Comparator<Class<?>>() {
+ @Override
+ public int compare(Class<?> a, Class<?> b)
+ {
+ return a.getName().compareTo(b.getName());
+ }
+ }
+ );
- Set<String> classes = new HashSet<String>();
try
{
AnnotationDB db = new AnnotationDB();
getLog().info("Scanning directory " + outputDirectory + " for annotated classes...");
URL dirUrl = dir.toURI().toURL();
db.scanArchives(dirUrl);
+
+ Set<String> classNames = new HashSet<String>();
if (db.getAnnotationIndex().containsKey(Entity.class.getName()))
- classes.addAll(db.getAnnotationIndex().get(Entity.class.getName()));
+ classNames.addAll(db.getAnnotationIndex().get(Entity.class.getName()));
if (db.getAnnotationIndex().containsKey(MappedSuperclass.class.getName()))
- classes.addAll(db.getAnnotationIndex().get(MappedSuperclass.class.getName()));
+ classNames.addAll(db.getAnnotationIndex().get(MappedSuperclass.class.getName()));
if (db.getAnnotationIndex().containsKey(Embeddable.class.getName()))
- classes.addAll(db.getAnnotationIndex().get(Embeddable.class.getName()));
+ classNames.addAll(db.getAnnotationIndex().get(Embeddable.class.getName()));
+
+ for (String name : classNames)
+ {
+ Class<?> annotatedClass = classLoader.loadClass(name);
+ classes.add(annotatedClass);
+ URL classUrl = annotatedClass.getResource(annotatedClass.getSimpleName() + ".class");
+ File classFile = new File(classUrl.toURI());
+ long lastModified = classFile.lastModified();
+ long timestamp = !timestamps.containsKey(name) ? 0 : timestamps.get(name);
+ if (lastModified > timestamp)
+ {
+ getLog().debug("Found new or modified annotated class: " + name);
+ modified = true;
+ timestamps.put(name, lastModified);
+ }
+ }
}
- catch (IOException e)
+ catch (ClassNotFoundException e)
+ {
+ getLog().error("Error while adding annotated classes!", e);
+ throw new MojoExecutionException(e.getMessage());
+ }
+ catch (Exception e)
{
getLog().error("Error while scanning!", e);
throw new MojoFailureException(e.getMessage());
}
+
if (classes.isEmpty())
throw new MojoFailureException("No annotated classes found in directory " + outputDirectory);
+ if (!modified)
+ {
+ getLog().info("No modified annotated classes found.");
+ getLog().info("Skipping schema generation!");
+ project.getProperties().setProperty("hibernate4.skipped", "true");
+ return;
+ }
+
+ getLog().debug("Detected classes with mapping-annotations:");
+ for (Class<?> annotatedClass : classes)
+ getLog().debug(" " + annotatedClass.getName());
+
+
Properties properties = new Properties();
/** Try to read configuration from properties-file */
properties.load(new FileInputStream(file));
}
else
- getLog().info("Ignoring nonexistent properties-file " + hibernateProperties + "!");
+ getLog().info("No hibernate-properties-file found! Checked path: " + hibernateProperties);
}
catch (IOException e)
{
throw new MojoExecutionException(e.getMessage());
}
- /** Overwrite values from propertie-file or set if given */
+ /** Overwrite values from propertie-file or set, if given */
if (driverClassName != null)
- properties.setProperty("hibernate.connection.driver_class", driverClassName);
+ {
+ if (properties.containsKey(DRIVER_CLASS))
+ getLog().debug(
+ "Overwriting property " +
+ DRIVER_CLASS + "=" + properties.getProperty(DRIVER_CLASS) +
+ " with the value " + driverClassName +
+ " from the plugin-configuration-parameter driverClassName!"
+ );
+ else
+ getLog().debug(
+ "Using the value " + driverClassName +
+ " from the plugin-configuration-parameter driverClassName!"
+ );
+ properties.setProperty(DRIVER_CLASS, driverClassName);
+ }
if (url != null)
- properties.setProperty("hibernate.connection.url", url);
+ {
+ if (properties.containsKey(URL))
+ getLog().debug(
+ "Overwriting property " +
+ URL + "=" + properties.getProperty(URL) +
+ " with the value " + url +
+ " from the plugin-configuration-parameter url!"
+ );
+ else
+ getLog().debug(
+ "Using the value " + url +
+ " from the plugin-configuration-parameter url!"
+ );
+ properties.setProperty(URL, url);
+ }
if (username != null)
- properties.setProperty("hibernate.connection.username", username);
+ {
+ if (properties.containsKey(USERNAME))
+ getLog().debug(
+ "Overwriting property " +
+ USERNAME + "=" + properties.getProperty(USERNAME) +
+ " with the value " + username +
+ " from the plugin-configuration-parameter username!"
+ );
+ else
+ getLog().debug(
+ "Using the value " + username +
+ " from the plugin-configuration-parameter username!"
+ );
+ properties.setProperty(USERNAME, username);
+ }
if (password != null)
- properties.setProperty("hibernate.connection.password", password);
+ {
+ if (properties.containsKey(PASSWORD))
+ getLog().debug(
+ "Overwriting property " +
+ PASSWORD + "=" + properties.getProperty(PASSWORD) +
+ " with the value " + password +
+ " from the plugin-configuration-parameter password!"
+ );
+ else
+ getLog().debug(
+ "Using the value " + password +
+ " from the plugin-configuration-parameter password!"
+ );
+ properties.setProperty(PASSWORD, password);
+ }
if (hibernateDialect != null)
- properties.setProperty("hibernate.dialect", hibernateDialect);
-
- if (properties.isEmpty())
- getLog().warn("No properties set!");
- for (Entry<Object,Object> entry : properties.entrySet())
- getLog().debug(entry.getKey() + " = " + entry.getValue());
-
- ClassLoader classLoader = null;
- try
{
- getLog().debug("Creating ClassLoader for project-dependencies...");
- List<String> classpathFiles = project.getCompileClasspathElements();
- URL[] urls = new URL[classpathFiles.size()];
- for (int i = 0; i < classpathFiles.size(); ++i)
- {
- getLog().debug("Dependency: " + classpathFiles.get(i));
- urls[i] = new File(classpathFiles.get(i)).toURI().toURL();
- }
- classLoader = new URLClassLoader(urls, getClass().getClassLoader());
+ if (properties.containsKey(DIALECT))
+ getLog().debug(
+ "Overwriting property " +
+ DIALECT + "=" + properties.getProperty(DIALECT) +
+ " with the value " + hibernateDialect +
+ " from the plugin-configuration-parameter hibernateDialect!"
+ );
+ else
+ getLog().debug(
+ "Using the value " + hibernateDialect +
+ " from the plugin-configuration-parameter hibernateDialect!"
+ );
+ properties.setProperty(DIALECT, hibernateDialect);
}
- catch (Exception e)
+
+ getLog().info("Gathered hibernate-configuration (turn on debugging for details):");
+ if (properties.isEmpty())
{
- getLog().error("Error while creating ClassLoader!", e);
- throw new MojoExecutionException(e.getMessage());
+ getLog().error("No properties set!");
+ throw new MojoFailureException("Hibernate-Configuration is missing!");
}
+ for (Entry<Object,Object> entry : properties.entrySet())
+ getLog().info(" " + entry.getKey() + " = " + entry.getValue());
Configuration config = new Configuration();
config.setProperties(properties);
- try
+ getLog().debug("Adding annotated classes to hibernate-mapping-configuration...");
+ for (Class<?> annotatedClass : classes)
{
- getLog().debug("Adding annotated classes to hibernate-mapping-configuration...");
- for (String annotatedClass : classes)
- {
- getLog().debug("Class " + annotatedClass);
- config.addAnnotatedClass(classLoader.loadClass(annotatedClass));
- }
- }
- catch (ClassNotFoundException e)
- {
- getLog().error("Error while adding annotated classes!", e);
- throw new MojoExecutionException(e.getMessage());
+ getLog().debug("Class " + annotatedClass);
+ config.addAnnotatedClass(annotatedClass);
}
Target target = null;
getLog().error("Error while closing connection: " + e.getMessage());
}
}
+
+ /** Write timestamps for annotated classes to file */
+ try
+ {
+ FileOutputStream fos = new FileOutputStream(saved);
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+ oos.writeObject(timestamps);
+ oos.close();
+ fos.close();
+ }
+ catch (Exception e)
+ {
+ getLog().error("Cannot write timestamps to file: " + e);
+ }
}
/**
return target.jdbcCompliant();
}
+ /**
+ * This Method cannot be annotated with @Override, becaus the plugin
+ * will not compile then under Java 1.6!
+ */
+ public Logger getParentLogger() throws SQLFeatureNotSupportedException
+ {
+ throw new SQLFeatureNotSupportedException("Not supported, for backward-compatibility with Java 1.6");
+ }
+
@Override
public String toString()
{