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 | 0 | 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 | 0 | AuthenticationInfo referenceInfo = wagonManager.getAuthenticationInfo(referenceServer); |
84 | 0 | if (referenceInfo != null) { |
85 | 0 | referenceUsername = referenceInfo.getUserName(); |
86 | 0 | referencePassword = referenceInfo.getPassword(); |
87 | |
} |
88 | |
|
89 | 0 | super.execute(); |
90 | 0 | } |
91 | |
|
92 | |
@Override |
93 | |
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException { |
94 | 0 | ClassLoader cl = null; |
95 | |
try { |
96 | 0 | cl = getMavenArtifactClassLoader(); |
97 | 0 | Thread.currentThread().setContextClassLoader(cl); |
98 | 0 | } catch (MojoExecutionException e) { |
99 | 0 | throw new LiquibaseException("Could not create the class loader, " + e, e); |
100 | 0 | } |
101 | |
|
102 | 0 | Database db = liquibase.getDatabase(); |
103 | 0 | Database referenceDatabase = CommandLineUtils.createDatabaseObject(cl, referenceUrl, referenceUsername, |
104 | |
referencePassword, referenceDriver, referenceDefaultSchemaName, null, null); |
105 | |
|
106 | 0 | getLog().info("Performing Diff on database " + db.toString()); |
107 | 0 | if (diffChangeLogFile != null) { |
108 | |
try { |
109 | 0 | CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db); |
110 | 0 | getLog().info("Differences written to Change Log File, " + diffChangeLogFile); |
111 | 0 | } catch (IOException e) { |
112 | 0 | throw new LiquibaseException(e); |
113 | 0 | } catch (ParserConfigurationException e) { |
114 | 0 | throw new LiquibaseException(e); |
115 | 0 | } |
116 | |
} else { |
117 | 0 | CommandLineUtils.doDiff(referenceDatabase, db); |
118 | |
} |
119 | 0 | } |
120 | |
|
121 | |
@Override |
122 | |
protected void printSettings(String indent) { |
123 | 0 | super.printSettings(indent); |
124 | 0 | getLog().info(indent + "referenceDriver: " + referenceDriver); |
125 | 0 | getLog().info(indent + "referenceUrl: " + referenceUrl); |
126 | 0 | getLog().info(indent + "referenceUsername: " + referenceUsername); |
127 | 0 | getLog().info(indent + "referencePassword: " + referencePassword); |
128 | 0 | getLog().info(indent + "referenceDefaultSchema: " + referenceDefaultSchemaName); |
129 | 0 | getLog().info(indent + "diffChangeLogFile: " + diffChangeLogFile); |
130 | 0 | } |
131 | |
|
132 | |
@Override |
133 | |
protected void checkRequiredParametersAreSpecified() throws MojoFailureException { |
134 | 0 | super.checkRequiredParametersAreSpecified(); |
135 | |
|
136 | 0 | if (referenceUrl == null) { |
137 | 0 | throw new MojoFailureException("A reference database or hibernate configuration file " |
138 | |
+ "must be provided to perform a diff."); |
139 | |
} |
140 | |
|
141 | 0 | if (referencePassword == null) { |
142 | 0 | referencePassword = ""; |
143 | |
} |
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
protected boolean isPromptOnNonLocalDatabase() { |
148 | 0 | return false; |
149 | |
} |
150 | |
} |