Updating imports to cause less merge issues
[hibernate4-maven-plugin] / src / main / java / de / juplo / plugins / hibernate4 / Hbm2DdlMojo.java
1 package de.juplo.plugins.hibernate4;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import com.pyx4j.log4j.MavenLogAppender;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.math.BigInteger;
28 import java.net.URL;
29 import java.net.URLClassLoader;
30 import java.security.MessageDigest;
31 import java.sql.Connection;
32 import java.sql.Driver;
33 import java.sql.DriverManager;
34 import java.sql.DriverPropertyInfo;
35 import java.sql.SQLException;
36 import java.sql.SQLFeatureNotSupportedException;
37 import java.util.Comparator;
38 import java.util.Enumeration;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Map.Entry;
44 import java.util.Properties;
45 import java.util.Set;
46 import java.util.TreeSet;
47 import java.util.logging.Logger;
48 import javax.persistence.Embeddable;
49 import javax.persistence.Entity;
50 import javax.persistence.MappedSuperclass;
51 import org.apache.maven.plugin.AbstractMojo;
52 import org.apache.maven.plugin.MojoExecutionException;
53 import org.apache.maven.plugin.MojoFailureException;
54 import org.apache.maven.project.MavenProject;
55 import org.hibernate.cfg.Configuration;
56 import org.hibernate.envers.configuration.AuditConfiguration;
57 import org.hibernate.tool.hbm2ddl.SchemaExport;
58 import org.hibernate.tool.hbm2ddl.SchemaExport.Type;
59 import org.hibernate.tool.hbm2ddl.Target;
60 import org.scannotation.AnnotationDB;
61
62
63 /**
64  * Goal which extracts the hibernate-mapping-configuration and
65  * exports an according SQL-database-schema.
66  *
67  * @goal export
68  * @phase process-classes
69  * @threadSafe
70  * @requiresDependencyResolution runtime
71  */
72 public class Hbm2DdlMojo extends AbstractMojo
73 {
74   public final static String EXPORT_SKIPPED_PROPERTY = "hibernate.export.skipped";
75
76   public final static String DRIVER_CLASS = "hibernate.connection.driver_class";
77   public final static String URL = "hibernate.connection.url";
78   public final static String USERNAME = "hibernate.connection.username";
79   public final static String PASSWORD = "hibernate.connection.password";
80   public final static String DIALECT = "hibernate.dialect";
81   public final static String ENVERS = "hibernate.export.envers";
82
83   private final static String MD5S = "schema.md5s";
84
85   /**
86    * The maven project.
87    * <p>
88    * Only needed internally.
89    *
90    * @parameter expression="${project}"
91    * @required
92    * @readonly
93    */
94   private MavenProject project;
95
96   /**
97    * Build-directory.
98    * <p>
99    * Only needed internally.
100    *
101    * @parameter expression="${project.build.directory}"
102    * @required
103    * @readonly
104    */
105   private String buildDirectory;
106
107   /**
108    * Classes-Directory to scan.
109    * <p>
110    * This parameter defaults to the maven build-output-directory for classes.
111    * Additonally, all dependencies are scanned for annotated classes.
112    *
113    * @parameter expression="${project.build.outputDirectory}"
114    */
115   private String outputDirectory;
116
117   /**
118    * Wether to scan test-classes too, or not.
119    * <p>
120    * If this parameter is set to <code>true</code> the test-classes of the
121    * artifact will be scanned for hibernate-annotated classes additionally.
122    *
123    * @parameter expression="${hibernate.export.scann_testclasses}" default-value="false"
124    */
125   private boolean scanTestClasses;
126
127   /**
128    * Test-Classes-Directory to scan.
129    * <p>
130    * This parameter defaults to the maven build-output-directory for
131    * test-classes.
132    * <p>
133    * This parameter is only used, when <code>scanTestClasses</code> is set
134    * to <code>true</code>!
135    *
136    * @parameter expression="${project.build.testOutputDirectory}"
137    */
138   private String testOutputDirectory;
139
140   /**
141    * Skip execution
142    * <p>
143    * If set to <code>true</code>, the execution is skipped.
144    * <p>
145    * A skipped excecution is signaled via the maven-property
146    * <code>${hibernate.export.skipped}</code>.
147    * <p>
148    * The excecution is skipped automatically, if no modified or newly added
149    * annotated classes are found and the dialect was not changed.
150    *
151    * @parameter expression="${maven.test.skip}" default-value="false"
152    */
153   private boolean skip;
154
155   /**
156    * Force execution
157    * <p>
158    * Force execution, even if no modified or newly added annotated classes
159    * where found and the dialect was not changed.
160    * <p>
161    * <code>skip</code> takes precedence over <code>force</code>.
162    *
163    * @parameter expression="${hibernate.export.force}" default-value="false"
164    */
165   private boolean force;
166
167   /**
168    * SQL-Driver name.
169    *
170    * @parameter expression="${hibernate.connection.driver_class}
171    */
172   private String driverClassName;
173
174   /**
175    * Database URL.
176    *
177    * @parameter expression="${hibernate.connection.url}"
178    */
179   private String url;
180
181   /**
182    * Database username
183    *
184    * @parameter expression="${hibernate.connection.username}"
185    */
186   private String username;
187
188   /**
189    * Database password
190    *
191    * @parameter expression="${hibernate.connection.password}"
192    */
193   private String password;
194
195   /**
196    * Hibernate dialect.
197    *
198    * @parameter expression="${hibernate.dialect}"
199    */
200   private String hibernateDialect;
201
202   /**
203    * Path to Hibernate configuration file.
204    *
205    * @parameter default-value="${project.build.outputDirectory}/hibernate.properties"
206    */
207   private String hibernateProperties;
208
209   /**
210    * Target of execution:
211    * <ul>
212    *   <li><strong>NONE</strong> do nothing - just validate the configuration (forces excecution, signals skip)</li>
213    *   <li><strong>EXPORT</strong> create database (<strong>DEFAULT!</strong>. forces excecution, signals skip)</li>
214    *   <li><strong>SCRIPT</strong> export schema to SQL-script</li>
215    *   <li><strong>BOTH</strong></li>
216    * </ul>
217    *
218    * @parameter expression="${hibernate.export.target}" default-value="EXPORT"
219    */
220   private String target;
221
222   /**
223    * Type of execution.
224    * <ul>
225    *   <li><strong>NONE</strong> do nothing - just validate the configuration</li>
226    *   <li><strong>CREATE</strong> create database-schema</li>
227    *   <li><strong>DROP</strong> drop database-schema</li>
228    *   <li><strong>BOTH</strong> (<strong>DEFAULT!</strong>)</li>
229    * </ul>
230    *
231    * @parameter expression="${hibernate.export.type}" default-value="BOTH"
232    */
233   private String type;
234
235   /**
236    * Output file.
237    *
238    * @parameter expression="${hibernate.export.schema.filename}" default-value="${project.build.directory}/schema.sql"
239    */
240   private String outputFile;
241
242   /**
243    * Delimiter in output-file.
244    *
245    * @parameter expression="${hibernate.export.schema.delimiter}" default-value=";"
246    */
247   private String delimiter;
248
249   /**
250    * Format output-file.
251    *
252    * @parameter expression="${hibernate.export.schema.format}" default-value="true"
253    */
254   private boolean format;
255
256   /**
257    * Generate envers schema for auditing tables.
258    *
259    * @parameter expression="${hibernate.export.envers}" default-value="false"
260    */
261   private Boolean envers;
262
263   /**
264    * Transient attribute, used only for knowing if the schema has changed since it was last run.
265    */
266   private boolean modified;
267
268   @Override
269   public void execute()
270     throws
271       MojoFailureException,
272       MojoExecutionException
273   {
274     if (skip)
275     {
276       getLog().info("Exectuion of hibernate4-maven-plugin:export was skipped!");
277       project.getProperties().setProperty(EXPORT_SKIPPED_PROPERTY, "true");
278       return;
279     }
280
281     File dir = new File(outputDirectory);
282     if (!dir.exists())
283       throw new MojoExecutionException("Cannot scan for annotated classes in " + outputDirectory + ": directory does not exist!");
284
285     Map<String,String> md5s;
286     modified = false;
287     File saved = new File(buildDirectory + File.separator + MD5S);
288
289     if (saved.exists())
290     {
291       try
292       {
293         FileInputStream fis = new FileInputStream(saved);
294         ObjectInputStream ois = new ObjectInputStream(fis);
295         md5s = (HashMap<String,String>)ois.readObject();
296         ois.close();
297       }
298       catch (Exception e)
299       {
300         md5s = new HashMap<String,String>();
301         getLog().warn("Cannot read timestamps from saved: " + e);
302       }
303     }
304     else
305     {
306       md5s = new HashMap<String,String>();
307       try
308       {
309         saved.createNewFile();
310       }
311       catch (IOException e)
312       {
313         getLog().warn("Cannot create saved for timestamps: " + e);
314       }
315     }
316
317     ClassLoader classLoader;
318     try
319     {
320       getLog().debug("Creating ClassLoader for project-dependencies...");
321       List<String> classpathFiles = project.getCompileClasspathElements();
322       if (scanTestClasses)
323         classpathFiles.addAll(project.getTestClasspathElements());
324       URL[] urls = new URL[classpathFiles.size()];
325       for (int i = 0; i < classpathFiles.size(); ++i)
326       {
327         getLog().debug("Dependency: " + classpathFiles.get(i));
328         urls[i] = new File(classpathFiles.get(i)).toURI().toURL();
329       }
330       classLoader = new URLClassLoader(urls, getClass().getClassLoader());
331     }
332     catch (Exception e)
333     {
334       getLog().error("Error while creating ClassLoader!", e);
335       throw new MojoExecutionException(e.getMessage());
336     }
337
338     Properties properties = new Properties();
339
340     /** Try to read configuration from properties-file */
341     try
342     {
343       File file = new File(hibernateProperties);
344       if (file.exists())
345       {
346         getLog().info("Reading properties from file " + hibernateProperties + "...");
347         properties.load(new FileInputStream(file));
348       }
349       else
350         getLog().info("No hibernate-properties-file found! (Checked path: " + hibernateProperties + ")");
351     }
352     catch (IOException e)
353     {
354       getLog().error("Error while reading properties!", e);
355       throw new MojoExecutionException(e.getMessage());
356     }
357
358     /** Overwrite values from propertie-file or set, if given */
359     if (driverClassName != null)
360     {
361       if (properties.containsKey(DRIVER_CLASS))
362         getLog().debug(
363             "Overwriting property " +
364             DRIVER_CLASS + "=" + properties.getProperty(DRIVER_CLASS) +
365             " with the value " + driverClassName
366           );
367       else
368         getLog().debug("Using the value " + driverClassName);
369       properties.setProperty(DRIVER_CLASS, driverClassName);
370     }
371     if (url != null)
372     {
373       if (properties.containsKey(URL))
374         getLog().debug(
375             "Overwriting property " +
376             URL + "=" + properties.getProperty(URL) +
377             " with the value " + url
378           );
379       else
380         getLog().debug("Using the value " + url);
381       properties.setProperty(URL, url);
382     }
383     if (username != null)
384     {
385       if (properties.containsKey(USERNAME))
386         getLog().debug(
387             "Overwriting property " +
388             USERNAME + "=" + properties.getProperty(USERNAME) +
389             " with the value " + username
390           );
391       else
392         getLog().debug("Using the value " + username);
393       properties.setProperty(USERNAME, username);
394     }
395     if (password != null)
396     {
397       if (properties.containsKey(PASSWORD))
398         getLog().debug(
399             "Overwriting property " +
400             PASSWORD + "=" + properties.getProperty(PASSWORD) +
401             " with the value " + password 
402           );
403       else
404         getLog().debug("Using the value " + password);
405       properties.setProperty(PASSWORD, password);
406     }
407     if (hibernateDialect != null)
408     {
409       if (properties.containsKey(DIALECT))
410         getLog().debug(
411             "Overwriting property " +
412             DIALECT + "=" + properties.getProperty(DIALECT) +
413             " with the value " + hibernateDialect
414           );
415       else
416         getLog().debug("Using the value " + hibernateDialect);
417       properties.setProperty(DIALECT, hibernateDialect);
418     }
419
420     /** The generated SQL varies with the dialect! */
421     if (md5s.containsKey(DIALECT))
422     {
423       String dialect = properties.getProperty(DIALECT);
424       if (md5s.get(DIALECT).equals(dialect))
425         getLog().debug("SQL-dialect unchanged.");
426       else
427       {
428         getLog().debug("SQL-dialect changed: " + dialect);
429         modified = true;
430         md5s.put(DIALECT, dialect);
431       }
432     }
433     else
434     {
435       modified = true;
436       md5s.put(DIALECT, properties.getProperty(DIALECT));
437     }
438
439     if (md5s.containsKey(ENVERS))
440     {
441       String envers = properties.getProperty(ENVERS);
442       if (md5s.get(ENVERS).equals(envers))
443         getLog().debug("Envers unchanged.");
444       else
445       {
446         getLog().debug("Envers changed: " + envers);
447         modified = true;
448         md5s.put(ENVERS, envers.toString());
449       }
450     }
451     else
452     {
453       modified = true;
454       md5s.put(ENVERS, properties.getProperty(ENVERS));
455     }
456
457     if (envers != null) {
458       if (properties.containsKey(ENVERS))
459         getLog().debug(
460             "Overwriting property " +
461                 ENVERS + "=" + properties.getProperty(ENVERS) +
462                 " with the value " + envers
463         );
464       else
465         getLog().debug("Using the value " + envers);
466       properties.setProperty(ENVERS, envers.toString());
467     }
468
469     if (properties.isEmpty())
470     {
471       getLog().error("No properties set!");
472       throw new MojoFailureException("Hibernate-Configuration is missing!");
473     }
474
475     Target target;
476     try
477     {
478       target = Target.valueOf(this.target.toUpperCase());
479     }
480     catch (IllegalArgumentException e)
481     {
482       getLog().error("Invalid value for configuration-option \"target\": " + this.target);
483       getLog().error("Valid values are: NONE, SCRIPT, EXPORT, BOTH");
484       throw new MojoExecutionException("Invalid value for configuration-option \"target\"");
485     }
486     Type type;
487     try
488     {
489       type = Type.valueOf(this.type.toUpperCase());
490     }
491     catch (IllegalArgumentException e)
492     {
493       getLog().error("Invalid value for configuration-option \"type\": " + this.type);
494       getLog().error("Valid values are: NONE, CREATE, DROP, BOTH");
495       throw new MojoExecutionException("Invalid value for configuration-option \"type\"");
496     }
497
498     if (target.equals(Target.SCRIPT) || target.equals(Target.NONE))
499     {
500       project.getProperties().setProperty(EXPORT_SKIPPED_PROPERTY, "true");
501     }
502     if (
503         !modified
504         && !target.equals(Target.SCRIPT)
505         && !target.equals(Target.NONE)
506         && !force
507       )
508     {
509       getLog().info("No modified annotated classes found and dialect unchanged.");
510       getLog().info("Skipping schema generation!");
511       project.getProperties().setProperty(EXPORT_SKIPPED_PROPERTY, "true");
512       return;
513     }
514
515     getLog().info("Gathered hibernate-configuration (turn on debugging for details):");
516     for (Entry<Object,Object> entry : properties.entrySet())
517       getLog().info("  " + entry.getKey() + " = " + entry.getValue());
518
519     Connection connection = null;
520     try
521     {
522       /**
523        * The connection must be established outside of hibernate, because
524        * hibernate does not use the context-classloader of the current
525        * thread and, hence, would not be able to resolve the driver-class!
526        */
527       switch (target)
528       {
529         case EXPORT:
530         case BOTH:
531           switch (type)
532           {
533             case CREATE:
534             case DROP:
535             case BOTH:
536               Class driverClass = classLoader.loadClass(properties.getProperty(DRIVER_CLASS));
537               getLog().debug("Registering JDBC-driver " + driverClass.getName());
538               DriverManager.registerDriver(new DriverProxy((Driver)driverClass.newInstance()));
539               getLog().debug(
540                   "Opening JDBC-connection to "
541                   + properties.getProperty(URL)
542                   + " as "
543                   + properties.getProperty(USERNAME)
544                   + " with password "
545                   + properties.getProperty(PASSWORD)
546                   );
547               connection = DriverManager.getConnection(
548                   properties.getProperty(URL),
549                   properties.getProperty(USERNAME),
550                   properties.getProperty(PASSWORD)
551                   );
552           }
553       }
554     }
555     catch (ClassNotFoundException e)
556     {
557       getLog().error("Dependency for driver-class " + properties.getProperty(DRIVER_CLASS) + " is missing!");
558       throw new MojoExecutionException(e.getMessage());
559     }
560     catch (Exception e)
561     {
562       getLog().error("Cannot establish connection to database!");
563       Enumeration<Driver> drivers = DriverManager.getDrivers();
564       if (!drivers.hasMoreElements())
565         getLog().error("No drivers registered!");
566       while (drivers.hasMoreElements())
567         getLog().debug("Driver: " + drivers.nextElement());
568       throw new MojoExecutionException(e.getMessage());
569     }
570
571     ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
572     MavenLogAppender.startPluginLog(this);
573     try
574     {
575       /**
576        * Change class-loader of current thread, so that hibernate can
577        * see all dependencies!
578        */
579       Thread.currentThread().setContextClassLoader(classLoader);
580
581       Set<Class<?>> classes = scanForAnnotations(dir, classLoader, md5s);
582
583       Configuration config = new Configuration();
584       config.setProperties(properties);
585       getLog().debug("Adding annotated classes to hibernate-mapping-configuration...");
586       for (Class<?> annotatedClass : classes)
587       {
588         getLog().debug("Class " + annotatedClass);
589         config.addAnnotatedClass(annotatedClass);
590       }
591       config.buildMappings();
592       if ("true".equals(properties.getProperty(ENVERS)))
593       {
594         getLog().debug("Using envers");
595         AuditConfiguration.getFor(config);
596       }
597
598       SchemaExport export = new SchemaExport(config, connection);
599       export.setOutputFile(outputFile);
600       export.setDelimiter(delimiter);
601       export.setFormat(format);
602       export.execute(target, type);
603
604       for (Object exception : export.getExceptions())
605         getLog().debug(exception.toString());
606     }
607     finally
608     {
609       /** Stop Log-Capturing */
610       MavenLogAppender.endPluginLog(this);
611
612       /** Restore the old class-loader (TODO: is this really necessary?) */
613       Thread.currentThread().setContextClassLoader(contextClassLoader);
614
615       /** Close the connection */
616       try
617       {
618         if (connection != null)
619           connection.close();
620       }
621       catch (SQLException e)
622       {
623         getLog().error("Error while closing connection: " + e.getMessage());
624       }
625     }
626
627     /** Write md5-sums for annotated classes to file */
628     try
629     {
630       FileOutputStream fos = new FileOutputStream(saved);
631       ObjectOutputStream oos = new ObjectOutputStream(fos);
632       oos.writeObject(md5s);
633       oos.close();
634       fos.close();
635     }
636     catch (Exception e)
637     {
638       getLog().error("Cannot write md5-sums to file: " + e);
639     }
640   }
641
642   /**
643    * Needed, because DriverManager won't pick up drivers, that were not
644    * loaded by the system-classloader!
645    * See:
646    * http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-fromodifiedm-an-arbitrary-location
647    */
648   static final class DriverProxy implements Driver
649   {
650     private final Driver target;
651
652     DriverProxy(Driver target)
653     {
654       if (target == null)
655         throw new NullPointerException();
656       this.target = target;
657     }
658
659     public java.sql.Driver getTarget()
660     {
661       return target;
662     }
663
664     @Override
665     public boolean acceptsURL(String url) throws SQLException
666     {
667       return target.acceptsURL(url);
668     }
669
670     @Override
671     public java.sql.Connection connect(
672         String url,
673         java.util.Properties info
674       )
675       throws
676         SQLException
677     {
678       return target.connect(url, info);
679     }
680
681     @Override
682     public int getMajorVersion()
683     {
684       return target.getMajorVersion();
685     }
686
687     @Override
688     public int getMinorVersion()
689     {
690       return target.getMinorVersion();
691     }
692
693     @Override
694     public DriverPropertyInfo[] getPropertyInfo(
695         String url,
696         Properties info
697       )
698       throws
699         SQLException
700     {
701       return target.getPropertyInfo(url, info);
702     }
703
704     @Override
705     public boolean jdbcCompliant()
706     {
707       return target.jdbcCompliant();
708     }
709
710     /**
711      * This Method cannot be annotated with @Override, becaus the plugin
712      * will not compile then under Java 1.6!
713      */
714     public Logger getParentLogger() throws SQLFeatureNotSupportedException
715     {
716       throw new SQLFeatureNotSupportedException("Not supported, for backward-compatibility with Java 1.6");
717     }
718
719     @Override
720     public String toString()
721     {
722       return "Proxy: " + target;
723     }
724
725     @Override
726     public int hashCode()
727     {
728       return target.hashCode();
729     }
730
731     @Override
732     public boolean equals(Object obj)
733     {
734       if (!(obj instanceof DriverProxy))
735         return false;
736       DriverProxy other = (DriverProxy) obj;
737       return this.target.equals(other.target);
738     }
739   }
740
741   public Set<Class<?>> scanForAnnotations(File dir, ClassLoader classLoader, Map<String,String> md5s) throws MojoExecutionException, MojoFailureException {
742     Set<Class<?>> classes = new TreeSet<Class<?>>(
743         new Comparator<Class<?>>() {
744           @Override
745           public int compare(Class<?> a, Class<?> b) {
746             return a.getName().compareTo(b.getName());
747           }
748         }
749     );
750
751     try {
752       AnnotationDB db = new AnnotationDB();
753       getLog().info("Scanning directory " + outputDirectory + " for annotated classes...");
754       URL dirUrl = dir.toURI().toURL();
755       db.scanArchives(dirUrl);
756       if (scanTestClasses) {
757         dir = new File(testOutputDirectory);
758         if (!dir.exists())
759           throw new MojoExecutionException("Cannot scan for annotated test-classes in " + testOutputDirectory + ": directory does not exist!");
760         getLog().info("Scanning directory " + testOutputDirectory + " for annotated classes...");
761         dirUrl = dir.toURI().toURL();
762         db.scanArchives(dirUrl);
763       }
764
765       Set<String> classNames = new HashSet<String>();
766       if (db.getAnnotationIndex().containsKey(Entity.class.getName()))
767         classNames.addAll(db.getAnnotationIndex().get(Entity.class.getName()));
768       if (db.getAnnotationIndex().containsKey(MappedSuperclass.class.getName()))
769         classNames.addAll(db.getAnnotationIndex().get(MappedSuperclass.class.getName()));
770       if (db.getAnnotationIndex().containsKey(Embeddable.class.getName()))
771         classNames.addAll(db.getAnnotationIndex().get(Embeddable.class.getName()));
772
773       MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
774       for (String name : classNames) {
775         Class<?> annotatedClass = classLoader.loadClass(name);
776         classes.add(annotatedClass);
777         InputStream is =
778             annotatedClass
779                 .getResourceAsStream(annotatedClass.getSimpleName() + ".class");
780         byte[] buffer = new byte[1024*4]; // copy data in 4MB-chunks
781         int i;
782         while((i = is.read(buffer)) > -1)
783           digest.update(buffer, 0, i);
784         is.close();
785         byte[] bytes = digest.digest();
786         BigInteger bi = new BigInteger(1, bytes);
787         String newMd5 = String.format("%0" + (bytes.length << 1) + "x", bi);
788         String oldMd5 = !md5s.containsKey(name) ? "" : md5s.get(name);
789         if (!newMd5.equals(oldMd5))
790         {
791           getLog().debug("Found new or modified annotated class: " + name);
792           modified = true;
793           md5s.put(name, newMd5);
794         }
795         else
796         {
797           getLog().debug(oldMd5 + " -> class unchanged: " + name);
798         }
799       }
800     } catch (Exception e) {
801       getLog().error("Error while scanning!", e);
802       throw new MojoFailureException(e.getMessage());
803     }
804
805     if (classes.isEmpty())
806       throw new MojoFailureException("No annotated classes found in directory " + outputDirectory);
807
808     getLog().debug("Detected classes with mapping-annotations:");
809     for (Class<?> annotatedClass : classes)
810       getLog().debug("  " + annotatedClass.getName());
811     return classes;
812   }
813 }