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
17
18
19
20
21 public class LiquibaseChangeLogSyncSQLMojo extends AbstractLiquibaseChangeLogMojo {
22
23
24
25
26
27
28
29 protected File migrationSqlOutputFile;
30
31
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
49
50
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
59 try {
60 if (!migrationSqlOutputFile.exists()) {
61
62 migrationSqlOutputFile.getParentFile().mkdirs();
63
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 }