View Javadoc

1   // Version: $Id: $
2   // Copyright: Copyright(c) 2008 Trace Financial Limited
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   * Generates a diff between the specified database and the reference database.
20   * 
21   * @author Peter Murray
22   * @goal diff
23   */
24  public class LiquibaseDatabaseDiff extends AbstractLiquibaseChangeLogMojo {
25  
26      /**
27       * The fully qualified name of the driver class to use to connect to the reference database. If this is not
28       * specified, then the {@link #driver} will be used instead.
29       * 
30       * @parameter expression="${liquibase.referenceDriver}"
31       */
32      protected String referenceDriver;
33  
34      /**
35       * The reference database URL to connect to for executing Liquibase. If performing a diff against a Hibernate config
36       * xml file, then use <b>"hibernate:PATH_TO_CONFIG_XML"</b> as the URL. The path to the hibernate configuration file
37       * can be relative to the test classpath for the Maven project.
38       * 
39       * @parameter expression="${liquibase.referenceUrl}"
40       */
41      protected String referenceUrl;
42  
43      /**
44       * The reference database username to use to connect to the specified database.
45       * 
46       * @parameter expression="${liquibase.referenceUsername}"
47       */
48      protected String referenceUsername;
49  
50      /**
51       * The reference database password to use to connect to the specified database. If this is null then an empty
52       * password will be used.
53       * 
54       * @parameter expression="${liquibase.referencePassword}"
55       */
56      protected String referencePassword;
57  
58      /**
59       * The reference database password to use to connect to the specified database. If this is null then an empty
60       * password will be used.
61       * 
62       * @parameter expression="${liquibase.referenceDefaultSchemaName}"
63       */
64      protected String referenceDefaultSchemaName;
65  
66      /**
67       * The server id in settings.xml to use when authenticating with.
68       * 
69       * @parameter expression="${liquibase.referenceServer}"
70       */
71      private String referenceServer;
72  
73      /**
74       * The diff change log file to output the differences to. If this is null then the differences will be output to the
75       * screen.
76       * 
77       * @parameter expression="${liquibase.diffChangeLogFile}"
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 }