X-Git-Url: https://juplo.de/gitweb/?p=hibernate4-maven-plugin;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fplugins%2Fhibernate4%2FHbm2DdlMojo.java;h=8db0fd66c72b8530900fff8250f28f86243adc98;hp=7afea6974f83700db0377a446c126600ed975f3b;hb=a4de03f352b21ce6abad570d2753467e3a972a10;hpb=6b6f25c70d0ad3424b087b2b6e9844c13518f3e3 diff --git a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java index 7afea697..8db0fd66 100644 --- a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java @@ -19,7 +19,10 @@ package de.juplo.plugins.hibernate4; 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; @@ -27,12 +30,18 @@ import java.sql.Driver; 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; @@ -58,6 +67,14 @@ import org.scannotation.AnnotationDB; */ 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. * @@ -176,35 +193,135 @@ public class Hbm2DdlMojo extends AbstractMojo 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 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)ois.readObject(); + ois.close(); + } + catch (Exception e) + { + timestamps = new HashMap(); + getLog().warn("Cannot read timestamps from saved: " + e); + } + } + else + { + timestamps = new HashMap(); + 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 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> classes = + new TreeSet>( + new Comparator>() { + @Override + public int compare(Class a, Class b) + { + return a.getName().compareTo(b.getName()); + } + } + ); - Set classes = new HashSet(); try { AnnotationDB db = new AnnotationDB(); getLog().info("Scanning directory " + outputDirectory + " for annotated classes..."); URL dirUrl = dir.toURI().toURL(); db.scanArchives(dirUrl); + + Set classNames = new HashSet(); 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 */ @@ -217,7 +334,7 @@ public class Hbm2DdlMojo extends AbstractMojo 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) { @@ -225,57 +342,104 @@ public class Hbm2DdlMojo extends AbstractMojo 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 entry : properties.entrySet()) - getLog().debug(entry.getKey() + " = " + entry.getValue()); - - ClassLoader classLoader = null; - try { - getLog().debug("Creating ClassLoader for project-dependencies..."); - List 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 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; @@ -379,6 +543,20 @@ public class Hbm2DdlMojo extends AbstractMojo 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); + } } /** @@ -449,6 +627,15 @@ public class Hbm2DdlMojo extends AbstractMojo 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() {