1
2
3 package org.liquibase.maven.plugins;
4
5 import java.io.IOException;
6
7 import javax.xml.parsers.ParserConfigurationException;
8
9 import liquibase.Liquibase;
10 import liquibase.database.Database;
11 import liquibase.exception.LiquibaseException;
12 import liquibase.integration.commandline.CommandLineUtils;
13
14 import org.apache.maven.plugin.MojoExecutionException;
15 import org.apache.maven.plugin.MojoFailureException;
16 import org.apache.maven.wagon.authentication.AuthenticationInfo;
17
18
19
20
21
22
23
24 public class LiquibaseDatabaseDiff extends AbstractLiquibaseChangeLogMojo {
25
26
27
28
29
30
31
32 protected String referenceDriver;
33
34
35
36
37
38
39
40
41 protected String referenceUrl;
42
43
44
45
46
47
48 protected String referenceUsername;
49
50
51
52
53
54
55
56 protected String referencePassword;
57
58
59
60
61
62
63
64 protected String referenceDefaultSchemaName;
65
66
67
68
69
70
71 private String referenceServer;
72
73
74
75
76
77
78
79 protected String diffChangeLogFile;
80
81 @Override
82 public void execute() throws MojoExecutionException, MojoFailureException {
83 AuthenticationInfo referenceInfo = wagonManager.getAuthenticationInfo(referenceServer);
84 if (referenceInfo != null) {
85 referenceUsername = referenceInfo.getUserName();
86 referencePassword = referenceInfo.getPassword();
87 }
88
89 super.execute();
90 }
91
92 @Override
93 protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
94 ClassLoader cl = null;
95 try {
96 cl = getMavenArtifactClassLoader();
97 Thread.currentThread().setContextClassLoader(cl);
98 } catch (MojoExecutionException e) {
99 throw new LiquibaseException("Could not create the class loader, " + e, e);
100 }
101
102 Database db = liquibase.getDatabase();
103 Database referenceDatabase = CommandLineUtils.createDatabaseObject(cl, referenceUrl, referenceUsername,
104 referencePassword, referenceDriver, referenceDefaultSchemaName, null, null);
105
106 getLog().info("Performing Diff on database " + db.toString());
107 if (diffChangeLogFile != null) {
108 try {
109 CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db);
110 getLog().info("Differences written to Change Log File, " + diffChangeLogFile);
111 } catch (IOException e) {
112 throw new LiquibaseException(e);
113 } catch (ParserConfigurationException e) {
114 throw new LiquibaseException(e);
115 }
116 } else {
117 CommandLineUtils.doDiff(referenceDatabase, db);
118 }
119 }
120
121 @Override
122 protected void printSettings(String indent) {
123 super.printSettings(indent);
124 getLog().info(indent + "referenceDriver: " + referenceDriver);
125 getLog().info(indent + "referenceUrl: " + referenceUrl);
126 getLog().info(indent + "referenceUsername: " + referenceUsername);
127 getLog().info(indent + "referencePassword: " + referencePassword);
128 getLog().info(indent + "referenceDefaultSchema: " + referenceDefaultSchemaName);
129 getLog().info(indent + "diffChangeLogFile: " + diffChangeLogFile);
130 }
131
132 @Override
133 protected void checkRequiredParametersAreSpecified() throws MojoFailureException {
134 super.checkRequiredParametersAreSpecified();
135
136 if (referenceUrl == null) {
137 throw new MojoFailureException("A reference database or hibernate configuration file "
138 + "must be provided to perform a diff.");
139 }
140
141 if (referencePassword == null) {
142 referencePassword = "";
143 }
144 }
145
146 @Override
147 protected boolean isPromptOnNonLocalDatabase() {
148 return false;
149 }
150 }