The plugin now scans for annotated classes in dependencies too
authorKai Moritz <kai@juplo.de>
Tue, 15 Oct 2013 23:18:53 +0000 (01:18 +0200)
committerKai Moritz <kai@juplo.de>
Thu, 17 Oct 2013 21:21:53 +0000 (23:21 +0200)
pom.xml
src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java
src/site/apt/pitfalls.apt

diff --git a/pom.xml b/pom.xml
index f4e0eef..bfd2c27 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <name>Erik-Berndt Scheper</name>
       <email>erik.berndt.scheper@gmail.com</email>
     </contributor>
+    <contributor>
+      <name>Guido Wimmel</name>
+      <email>Wimmel.Guido@swm.de</email>
+    </contributor>
   </contributors>
 
   <distributionManagement>
index 2e232bf..83f53d3 100644 (file)
@@ -47,9 +47,12 @@ import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import javax.persistence.Embeddable;
 import javax.persistence.Entity;
 import javax.persistence.MappedSuperclass;
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -85,7 +88,10 @@ public class Hbm2DdlMojo extends AbstractMojo
   public final static String NAMING_STRATEGY="hibernate.ejb.naming_strategy";
   public final static String ENVERS = "hibernate.export.envers";
 
-  private final static String MD5S = "schema.md5s";
+  public final static String MD5S = "schema.md5s";
+
+  private final static Pattern split = Pattern.compile("[^,\\s]+");
+
 
   /**
    * The maven project.
@@ -129,6 +135,21 @@ public class Hbm2DdlMojo extends AbstractMojo
    */
   private boolean scanTestClasses;
 
+  /**
+   * Dependency-Scopes, that should be scanned for annotated classes.
+   * <p>
+   * By default, only dependencies in the scope <code>compile</code> are
+   * scanned for annotated classes. Multiple scopes can be seperated by
+   * white space or commas.
+   * <p>
+   * The plugin does not scan for annotated classes in transitive
+   * dependencies. If some of your annotated classes are hidden in a
+   * transitive dependency, you can simply add that dependency explicitly.
+   *
+   * @parameter property="hibernate.export.scan_dependencies" default-value="compile"
+   */
+  private String scanDependencies;
+
   /**
    * Test-Classes-Directory to scan.
    * <p>
@@ -381,6 +402,34 @@ public class Hbm2DdlMojo extends AbstractMojo
           db.scanArchives(dirUrl);
         }
       }
+      if (scanDependencies != null)
+      {
+        Matcher matcher = split.matcher(scanDependencies);
+        while (matcher.find())
+        {
+          getLog().info("Scanning dependencies for scope " + matcher.group());
+          for (Artifact artifact : project.getDependencyArtifacts())
+          {
+            if (!artifact.getScope().equalsIgnoreCase(matcher.group()))
+              continue;
+            if (artifact.getFile() == null)
+            {
+              getLog().warn(
+                  "Cannot scan dependency " +
+                  artifact.getId() +
+                  ": no JAR-file available!"
+                  );
+              continue;
+            }
+            getLog().info(
+                "Scanning dependency " +
+                artifact.getId() +
+                " for annotated classes..."
+                );
+            db.scanArchives(artifact.getFile().toURI().toURL());
+          }
+        }
+      }
 
       Set<String> classNames = new HashSet<String>();
       if (db.getAnnotationIndex().containsKey(Entity.class.getName()))
index 33c40cf..a25abcd 100644 (file)
@@ -4,6 +4,12 @@
   Kai Moritz
   ---
 
+Annotated classes in dependencies are not found.
+
+  hibernate4-maven-plugin does not scan transitive dependencies for
+  annotated classes. If some of your annotated classes are hidden in a
+  transitive dependency, you can simply add that dependency explicitly.
+
 hibernate4-maven-plugin always needs a database-connection
 
   The default-configuration uses the EXPORT-target of the SchemaExport-Tool.