| 1 |  |  package org.liquibase.maven.plugins; | 
  | 2 |  |   | 
  | 3 |  |  import java.io.*; | 
  | 4 |  |  import liquibase.resource.ResourceAccessor; | 
  | 5 |  |  import liquibase.Liquibase; | 
  | 6 |  |  import liquibase.database.Database; | 
  | 7 |  |  import liquibase.exception.LiquibaseException; | 
  | 8 |  |  import org.apache.maven.plugin.MojoExecutionException; | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |   | 
  | 17 |  |   | 
  | 18 | 0 |  public class LiquibaseUpdateSQL extends AbstractLiquibaseUpdateMojo { | 
  | 19 |  |   | 
  | 20 |  |       | 
  | 21 |  |   | 
  | 22 |  |   | 
  | 23 |  |   | 
  | 24 |  |   | 
  | 25 |  |   | 
  | 26 |  |      protected File migrationSqlOutputFile; | 
  | 27 |  |   | 
  | 28 |  |       | 
  | 29 |  |      private Writer outputWriter; | 
  | 30 |  |   | 
  | 31 |  |      @Override | 
  | 32 |  |      protected boolean isPromptOnNonLocalDatabase() { | 
  | 33 |  |           | 
  | 34 |  |           | 
  | 35 |  |           | 
  | 36 | 0 |          return false; | 
  | 37 |  |      } | 
  | 38 |  |   | 
  | 39 |  |      @Override | 
  | 40 |  |      protected void doUpdate(Liquibase liquibase) throws LiquibaseException { | 
  | 41 | 0 |          if (changesToApply > 0) { | 
  | 42 | 0 |              liquibase.update(changesToApply, contexts, outputWriter); | 
  | 43 |  |          } else { | 
  | 44 | 0 |              liquibase.update(contexts, outputWriter); | 
  | 45 |  |          } | 
  | 46 | 0 |      } | 
  | 47 |  |   | 
  | 48 |  |      @Override | 
  | 49 |  |      protected Liquibase createLiquibase(ResourceAccessor fo, Database db) throws MojoExecutionException { | 
  | 50 | 0 |          Liquibase liquibase = super.createLiquibase(fo, db); | 
  | 51 |  |   | 
  | 52 |  |           | 
  | 53 |  |          try { | 
  | 54 | 0 |              if (!migrationSqlOutputFile.exists()) { | 
  | 55 |  |                   | 
  | 56 | 0 |                  migrationSqlOutputFile.getParentFile().mkdirs(); | 
  | 57 |  |                   | 
  | 58 | 0 |                  if (!migrationSqlOutputFile.createNewFile()) { | 
  | 59 | 0 |                      throw new MojoExecutionException("Cannot create the migration SQL file; " | 
  | 60 |  |                              + migrationSqlOutputFile.getAbsolutePath()); | 
  | 61 |  |                  } | 
  | 62 |  |              } | 
  | 63 | 0 |              outputWriter = new FileWriter(migrationSqlOutputFile); | 
  | 64 | 0 |          } catch (IOException e) { | 
  | 65 | 0 |              getLog().error(e); | 
  | 66 | 0 |              throw new MojoExecutionException("Failed to create SQL output writer", e); | 
  | 67 | 0 |          } | 
  | 68 | 0 |          getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath()); | 
  | 69 | 0 |          return liquibase; | 
  | 70 |  |      } | 
  | 71 |  |   | 
  | 72 |  |      @Override | 
  | 73 |  |      protected void printSettings(String indent) { | 
  | 74 | 0 |          super.printSettings(indent); | 
  | 75 | 0 |          getLog().info(indent + "migrationSQLOutputFile: " + migrationSqlOutputFile); | 
  | 76 | 0 |      } | 
  | 77 |  |   | 
  | 78 |  |      @Override | 
  | 79 |  |      protected void cleanup(Database db) { | 
  | 80 | 0 |          super.cleanup(db); | 
  | 81 | 0 |          if (outputWriter != null) { | 
  | 82 |  |              try { | 
  | 83 | 0 |                  outputWriter.close(); | 
  | 84 | 0 |              } catch (IOException e) { | 
  | 85 | 0 |                  getLog().error(e); | 
  | 86 | 0 |              } | 
  | 87 |  |          } | 
  | 88 | 0 |      } | 
  | 89 |  |  } |