Make output file handling more robust
authorJoel Johnson <mrjoel@lixil.net>
Thu, 26 Dec 2013 21:02:29 +0000 (14:02 -0700)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jan 2014 17:15:31 +0000 (18:15 +0100)
* Ensure output file directory path exists
* Anchor relative paths in build directory

src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java

index 9790181..18351f0 100644 (file)
@@ -875,9 +875,34 @@ public class Hbm2DdlMojo extends AbstractMojo
       }
 
       SchemaExport export = new SchemaExport(config, connection);
-      export.setOutputFile(outputFile);
       export.setDelimiter(delimiter);
       export.setFormat(format);
+
+      File outF = new File(outputFile);
+
+      if (!outF.isAbsolute())
+      {
+        // Interpret relative file path relative to build directory
+        outF = new File(buildDirectory, outputFile);
+        getLog().info("Adjusted relative path, resulting path is " + outF.getPath());
+      }
+
+      // Ensure that directory path for specified file exists
+      File outFileParentDir = outF.getParentFile();
+      if (null != outFileParentDir && !outFileParentDir.exists())
+      {
+        try
+        {
+          getLog().info("Creating directory path for output file:" + outFileParentDir.getPath());
+          outFileParentDir.mkdirs();
+        }
+        catch (Exception e)
+        {
+          getLog().error("Error creating directory path for output file: " + e.getLocalizedMessage());
+        }
+      }
+
+      export.setOutputFile(outF.getPath());
       export.execute(target, type);
 
       for (Object exception : export.getExceptions())