View Javadoc

1   package org.liquibase.maven.plugins;
2   
3   import java.io.File;
4   import java.io.FileWriter;
5   import java.io.IOException;
6   import java.io.Writer;
7   
8   import org.apache.maven.plugin.MojoExecutionException;
9   
10  import liquibase.Liquibase;
11  import liquibase.database.Database;
12  import liquibase.exception.LiquibaseException;
13  import liquibase.resource.ResourceAccessor;
14  
15  /**
16   * Generates SQL that marks all unapplied changes as applied.
17   * 
18   * @author JAmes Atwill
19   * @goal changelogSyncSQL
20   */
21  public class LiquibaseChangeLogSyncSQLMojo extends AbstractLiquibaseChangeLogMojo {
22  
23      /**
24       * The file to output the Migration SQL script to, if it exists it will be overwritten.
25       * 
26       * @parameter expression="${liquibase.migrationSqlOutputFile}" default-value=
27       *            "${project.build.directory}/liquibase/migrate.sql"
28       */
29      protected File migrationSqlOutputFile;
30  
31      /** The writer for writing the migration SQL. */
32      private Writer outputWriter;
33  
34      @Override
35      protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
36          liquibase.changeLogSync(contexts, outputWriter);
37      }
38  
39      @Override
40      protected void printSettings(String indent) {
41          super.printSettings(indent);
42          getLog().info(indent + "migrationSQLOutputFile: " + migrationSqlOutputFile);
43  
44      }
45  
46      @Override
47      protected boolean isPromptOnNonLocalDatabase() {
48          // Always run on an non-local database as we are not actually modifying
49          // the database
50          // when run on it.
51          return false;
52      }
53  
54      @Override
55      protected Liquibase createLiquibase(ResourceAccessor fo, Database db) throws MojoExecutionException {
56          Liquibase liquibase = super.createLiquibase(fo, db);
57  
58          // Setup the output file writer
59          try {
60              if (!migrationSqlOutputFile.exists()) {
61                  // Ensure the parent directories exist
62                  migrationSqlOutputFile.getParentFile().mkdirs();
63                  // Create the actual file
64                  if (!migrationSqlOutputFile.createNewFile()) {
65                      throw new MojoExecutionException("Cannot create the migration SQL file; "
66                              + migrationSqlOutputFile.getAbsolutePath());
67                  }
68              }
69              outputWriter = new FileWriter(migrationSqlOutputFile);
70          } catch (IOException e) {
71              getLog().error(e);
72              throw new MojoExecutionException("Failed to create SQL output writer", e);
73          }
74          getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath());
75          return liquibase;
76      }
77  
78      @Override
79      protected void cleanup(Database db) {
80          super.cleanup(db);
81          if (outputWriter != null) {
82              try {
83                  outputWriter.close();
84              } catch (IOException e) {
85                  getLog().error(e);
86              }
87          }
88      }
89  
90  }