-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
AbstractLiquibaseChangeLogMojo.java
186 lines (162 loc) · 6.66 KB
/
AbstractLiquibaseChangeLogMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Version: $Id: $
// Copyright: Copyright(c) 2007 Trace Financial Limited
package org.liquibase.maven.plugins;
import liquibase.GlobalConfiguration;
import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.configuration.core.DeprecatedConfigurationValueProvider;
import liquibase.database.Database;
import liquibase.exception.LiquibaseException;
import liquibase.hub.HubConfiguration;
import liquibase.resource.*;
import liquibase.util.StringUtil;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.liquibase.maven.property.PropertyElement;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* A Liquibase MOJO that requires the user to provide a DatabaseChangeLogFile to be able
* to perform any actions on the database.
*
* @author Peter Murray
*/
public abstract class AbstractLiquibaseChangeLogMojo extends AbstractLiquibaseMojo {
/**
* Specifies the directory where Liquibase can find your <i>changelog</i> file.
*
* @parameter property="liquibase.changeLogDirectory"
*/
@PropertyElement
protected String changeLogDirectory;
/**
* Specifies the <i>changelog</i> file for Liquibase to use.
*
* @parameter property="liquibase.changeLogFile"
*/
@PropertyElement
protected String changeLogFile;
/**
* Specifies which contexts Liquibase will execute, which can be separated by a commaif multiple contexts
are required.
* If a context is not specified, then ALL contexts will be executed.
*
* @parameter property="liquibase.contexts" default-value=""
*/
@PropertyElement
protected String contexts;
/**
* Specifies which Liquibase labels Liquibase will execute, which can be separated by a commaif multiple labels
are required or you need to designate a more complex expression.
* If a label is not specified, then ALL labels will be executed.
*
* @parameter property="liquibase.labels" default-value=""
*/
@PropertyElement
protected String labels;
/**
*
* Specifies the <i>Liquibase Hub API key</i> for Liquibase to use.
*
* @parameter property="liquibase.hub.apiKey"
*
*/
@PropertyElement(key = "liquibase.hub.apiKey")
protected String hubApiKey;
/**
*
* Specifies the <i>Liquibase Hub URL</i> for Liquibase to use.
*
* @parameter property="liquibase.hub.url"
*
*/
@PropertyElement(key = "liquibase.hub.url")
protected String hubUrl;
/**
* Specifies the <i>Liquibase Hub URL</i> for Liquibase to use.
*
* @parameter property="liquibase.hub.mode"
*
*/
@PropertyElement(key = "liquibase.hub.mode")
protected String hubMode;
/**
* How to handle multiple files being found in the search path that have duplicate paths.
* Options are WARN (log warning and choose one at random) or ERROR (fail current operation)
*
* @parameter property="liquibase.duplicateFileMode" default-value="ERROR"
*/
@PropertyElement
protected String duplicateFileMode;
@Override
protected void checkRequiredParametersAreSpecified() throws MojoFailureException {
super.checkRequiredParametersAreSpecified();
if (changeLogFile == null) {
throw new MojoFailureException("The changeLogFile must be specified.");
}
}
/**
* Performs the actual Liquibase task on the database using the fully configured {@link
* liquibase.Liquibase}.
*
* @param liquibase The {@link liquibase.Liquibase} that has been fully
* configured to run the desired database task.
*/
@Override
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
//
// Store the Hub API key and URL for later use
//
if (StringUtil.isNotEmpty(hubApiKey)) {
DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_API_KEY, hubApiKey);
}
if (StringUtil.isNotEmpty(hubUrl)) {
DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_URL.getKey(), hubUrl);
}
if (StringUtil.isNotEmpty(hubMode)) {
DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_MODE.getKey(), hubMode);
}
if (StringUtil.isNotEmpty(duplicateFileMode)) {
DeprecatedConfigurationValueProvider.setData(GlobalConfiguration.DUPLICATE_FILE_MODE.getKey(), GlobalConfiguration.DuplicateFileMode.valueOf(duplicateFileMode.toUpperCase(Locale.ROOT)));
}
}
@Override
protected void printSettings(String indent) {
super.printSettings(indent);
getLog().info(indent + "changeLogDirectory: " + changeLogDirectory);
getLog().info(indent + "changeLogFile: " + changeLogFile);
getLog().info(indent + "context(s): " + contexts);
getLog().info(indent + "label(s): " + labels);
}
@Override
protected ResourceAccessor getResourceAccessor(ClassLoader cl) {
List<ResourceAccessor> resourceAccessors = new ArrayList<ResourceAccessor>();
resourceAccessors.add(new MavenResourceAccessor(cl));
resourceAccessors.add(new FileSystemResourceAccessor(project.getBasedir()));
resourceAccessors.add(new ClassLoaderResourceAccessor(getClass().getClassLoader()));
if (changeLogDirectory != null) {
calculateChangeLogDirectoryAbsolutePath();
resourceAccessors.add(new FileSystemResourceAccessor(new File(changeLogDirectory)));
}
return new SearchPathResourceAccessor(searchPath, resourceAccessors.toArray(new ResourceAccessor[0]));
}
@Override
protected Liquibase createLiquibase(Database db) throws MojoExecutionException {
String changeLog = (changeLogFile == null) ? "" : changeLogFile.trim();
return new Liquibase(changeLog, Scope.getCurrentScope().getResourceAccessor(), db);
}
private void calculateChangeLogDirectoryAbsolutePath() {
if (changeLogDirectory != null) {
// convert to standard / if using absolute path on windows
changeLogDirectory = changeLogDirectory.trim().replace('\\', '/');
// try to know if it's an absolute or relative path : the absolute path case is simpler and don't need more actions
File changeLogDirectoryFile = new File(changeLogDirectory);
if (!changeLogDirectoryFile.isAbsolute()) {
// we are in the relative path case
changeLogDirectory = project.getBasedir().getAbsolutePath().replace('\\', '/') + "/" + changeLogDirectory;
}
}
}
}