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 | 0 | 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 | 0 | liquibase.changeLogSync(contexts, outputWriter); |
37 | 0 | } |
38 | |
|
39 | |
@Override |
40 | |
protected void printSettings(String indent) { |
41 | 0 | super.printSettings(indent); |
42 | 0 | getLog().info(indent + "migrationSQLOutputFile: " + migrationSqlOutputFile); |
43 | |
|
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
protected boolean isPromptOnNonLocalDatabase() { |
48 | |
|
49 | |
|
50 | |
|
51 | 0 | return false; |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
protected Liquibase createLiquibase(ResourceAccessor fo, Database db) throws MojoExecutionException { |
56 | 0 | Liquibase liquibase = super.createLiquibase(fo, db); |
57 | |
|
58 | |
|
59 | |
try { |
60 | 0 | if (!migrationSqlOutputFile.exists()) { |
61 | |
|
62 | 0 | migrationSqlOutputFile.getParentFile().mkdirs(); |
63 | |
|
64 | 0 | if (!migrationSqlOutputFile.createNewFile()) { |
65 | 0 | throw new MojoExecutionException("Cannot create the migration SQL file; " |
66 | |
+ migrationSqlOutputFile.getAbsolutePath()); |
67 | |
} |
68 | |
} |
69 | 0 | outputWriter = new FileWriter(migrationSqlOutputFile); |
70 | 0 | } catch (IOException e) { |
71 | 0 | getLog().error(e); |
72 | 0 | throw new MojoExecutionException("Failed to create SQL output writer", e); |
73 | 0 | } |
74 | 0 | getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath()); |
75 | 0 | return liquibase; |
76 | |
} |
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 | |
|
90 | |
} |