Fixed NullPointerException in ModificationTracker
[hibernate4-maven-plugin] / src / main / java / de / juplo / plugins / hibernate / AbstractSchemaMojo.java
index 09bd83d..e121f2a 100644 (file)
@@ -136,7 +136,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
    * If set to <code>true</code>, the execution is skipped.
    * <p>
    * A skipped execution is signaled via the maven-property
    * If set to <code>true</code>, the execution is skipped.
    * <p>
    * A skipped execution is signaled via the maven-property
-   * <code>${hibernate.export.skipped}</code>.
+   * <code>${hibernate.schema.skipped}</code>.
    * <p>
    * The execution is skipped automatically, if no modified or newly added
    * annotated classes are found and the dialect was not changed.
    * <p>
    * The execution is skipped automatically, if no modified or newly added
    * annotated classes are found and the dialect was not changed.
@@ -542,6 +542,64 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
         classes = scanUrls(urls);
         for (String className : unit.getManagedClassNames())
           classes.add(className);
         classes = scanUrls(urls);
         for (String className : unit.getManagedClassNames())
           classes.add(className);
+        /**
+         * Add mappings from the default mapping-file
+         * <code>META-INF/orm.xml</code>, if present
+         */
+        boolean error = false;
+        InputStream is;
+        is = classLoader.getResourceAsStream("META-INF/orm.xml");
+        if (is != null)
+        {
+          getLog().info("Adding default JPA-XML-mapping from META-INF/orm.xml");
+          try
+          {
+            tracker.track("META-INF/orm.xml", is);
+            sources.addResource("META-INF/orm.xml");
+          }
+          catch (IOException e)
+          {
+            getLog().error("cannot read META-INF/orm.xml: " + e);
+            error = true;
+          }
+        }
+        else
+        {
+          getLog().debug("no META-INF/orm.xml found");
+        }
+        /**
+         * Add mappings from files, that are explicitly configured in the
+         * persistence unit
+         */
+        for (String mapping : unit.getMappingFileNames())
+        {
+          getLog().info("Adding explicitly configured mapping from " + mapping);
+          is = classLoader.getResourceAsStream(mapping);
+          if (is != null)
+          {
+            try
+            {
+              tracker.track(mapping, is);
+              sources.addResource(mapping);
+            }
+            catch (IOException e)
+            {
+              getLog().info("cannot read mapping-file " + mapping + ": " + e);
+              error = true;
+            }
+          }
+          else
+          {
+            getLog().error("cannot find mapping-file " + mapping);
+            error = true;
+          }
+        }
+        if (error)
+          throw new MojoFailureException(
+              "error, while reading mappings configured in persistence-unit \"" +
+              unit.getName() +
+              "\""
+              );
       }
 
       /** Add the configured/collected annotated classes */
       }
 
       /** Add the configured/collected annotated classes */
@@ -613,6 +671,21 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
         thread.setContextClassLoader(contextClassLoader);
       }
     }
         thread.setContextClassLoader(contextClassLoader);
       }
     }
+    catch (MojoExecutionException e)
+    {
+      tracker.failed();
+      throw e;
+    }
+    catch (MojoFailureException e)
+    {
+      tracker.failed();
+      throw e;
+    }
+    catch (RuntimeException e)
+    {
+      tracker.failed();
+      throw e;
+    }
     finally
     {
       /** Remember mappings and configuration */
     finally
     {
       /** Remember mappings and configuration */
@@ -1014,8 +1087,9 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
     try
     {
       getLog().info("Adding annotated resource: " + name);
     try
     {
       getLog().info("Adding annotated resource: " + name);
-      String packageName;
+      String packageName = null;
 
 
+      boolean error = false;
       try
       {
         Class<?> annotatedClass = classLoaderService.classForName(name);
       try
       {
         Class<?> annotatedClass = classLoaderService.classForName(name);
@@ -1026,20 +1100,34 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
                 resourceName.length()
                 ) + ".class";
         InputStream is = annotatedClass.getResourceAsStream(resourceName);
                 resourceName.length()
                 ) + ".class";
         InputStream is = annotatedClass.getResourceAsStream(resourceName);
-        if (tracker.track(name, is))
-          getLog().debug("New or modified class: " + name);
+        if (is != null)
+        {
+          if (tracker.track(name, is))
+            getLog().debug("New or modified class: " + name);
+          else
+            getLog().debug("Unchanged class: " + name);
+          sources.addAnnotatedClass(annotatedClass);
+          packageName = annotatedClass.getPackage().getName();
+        }
         else
         else
-          getLog().debug("Unchanged class: " + name);
-        sources.addAnnotatedClass(annotatedClass);
-        packageName = annotatedClass.getPackage().getName();
+        {
+          getLog().error("cannot find ressource " + resourceName + " for class " + name);
+          error = true;
+        }
       }
       catch(ClassLoadingException e)
       {
         packageName = name;
       }
       }
       catch(ClassLoadingException e)
       {
         packageName = name;
       }
+      if (error)
+      {
+        throw new MojoExecutionException("error while inspecting annotated class " + name);
+      }
 
 
-      if (!packages.contains(packageName))
+      while (packageName != null)
       {
       {
+        if (packages.contains(packageName))
+          return;
         String resource = packageName.replace('.', '/') + "/package-info.class";
         InputStream is = classLoaderService.locateResourceStream(resource);
         if (is == null)
         String resource = packageName.replace('.', '/') + "/package-info.class";
         InputStream is = classLoaderService.locateResourceStream(resource);
         if (is == null)
@@ -1053,10 +1141,15 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
             getLog().debug("New or modified package: " + packageName);
           else
            getLog().debug("Unchanged package: " + packageName);
             getLog().debug("New or modified package: " + packageName);
           else
            getLog().debug("Unchanged package: " + packageName);
-          getLog().info("Adding annotated package " + packageName);
+          getLog().info("Adding annotations from package " + packageName);
           sources.addPackage(packageName);
         }
         packages.add(packageName);
           sources.addPackage(packageName);
         }
         packages.add(packageName);
+        int i = packageName.lastIndexOf('.');
+        if (i < 0)
+          packageName = null;
+        else
+          packageName = packageName.substring(0,i);
       }
     }
     catch (Exception e)
       }
     }
     catch (Exception e)