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 package com.jcabi.mysql.maven.plugin;
31
32 import com.jcabi.aspects.Cacheable;
33 import com.jcabi.log.Logger;
34 import java.io.File;
35 import java.util.Collections;
36 import java.util.List;
37 import lombok.EqualsAndHashCode;
38 import lombok.ToString;
39 import org.apache.maven.plugin.AbstractMojo;
40 import org.apache.maven.plugin.MojoFailureException;
41 import org.apache.maven.plugins.annotations.Parameter;
42 import org.apache.maven.project.MavenProject;
43 import org.slf4j.impl.StaticLoggerBinder;
44
45
46
47
48
49 @ToString
50 @EqualsAndHashCode(callSuper = false)
51 abstract class AbstractMysqlMojo extends AbstractMojo {
52
53
54
55
56
57 private static final String PROPERTY_REUSED = "jcabi.reused.database";
58
59
60
61
62 @Parameter(
63 defaultValue = "${project}",
64 readonly = true
65 )
66 private transient MavenProject project;
67
68
69
70
71 @Parameter(
72 defaultValue = "false",
73 required = false
74 )
75 private transient boolean skip;
76
77
78
79
80 @Parameter(
81 defaultValue = "3306",
82 required = false
83 )
84 private transient int port;
85
86
87
88
89 @Parameter(
90 defaultValue = "${project.build.directory}/mysql-dist",
91 required = true
92 )
93 private transient File dist;
94
95
96
97
98 @Parameter(
99 defaultValue = "root",
100 required = false
101 )
102 private transient String user;
103
104
105
106
107 @Parameter(
108 defaultValue = "root",
109 required = false
110 )
111 private transient String password;
112
113
114
115
116 @Parameter(
117 defaultValue = "root",
118 required = false
119 )
120 private transient String dbname;
121
122
123
124
125 @Parameter(
126 defaultValue = "${project.build.directory}/mysql-data",
127 required = true
128 )
129 private transient File data;
130
131
132
133
134
135 @Parameter(required = false)
136 private transient File socket;
137
138
139
140
141 @Parameter(
142 defaultValue = "true",
143 required = false
144 )
145 private transient boolean erase;
146
147
148
149
150 @Parameter(
151 required = false
152 )
153 private transient List<String> options;
154
155
156
157
158
159 public void setSkip(final boolean skp) {
160 this.skip = skp;
161 }
162
163 @Override
164 public void execute() throws MojoFailureException {
165 StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
166 if (this.skip) {
167 Logger.info(this, "execution skipped because of 'skip' option");
168 return;
169 }
170 this.run(AbstractMysqlMojo.instances());
171 if (this.project == null) {
172 Logger.warn(
173 this,
174 "MavenProject not initialized, unable to set property %s",
175 AbstractMysqlMojo.PROPERTY_REUSED
176 );
177 } else {
178 Logger.info(
179 this,
180 "set Maven property %s = %s ",
181 AbstractMysqlMojo.PROPERTY_REUSED,
182 AbstractMysqlMojo.instances().reusedExistingDatabase()
183 );
184 this.project.getProperties().setProperty(
185 AbstractMysqlMojo.PROPERTY_REUSED,
186 Boolean.toString(
187 AbstractMysqlMojo.instances().reusedExistingDatabase()
188 )
189 );
190 }
191 }
192
193
194
195
196
197
198 public File distDir() throws MojoFailureException {
199 if (!this.dist.exists()) {
200 throw new MojoFailureException(
201 String.format(
202 "MySQL distribution directory doesn't exist: %s", this.dist
203 )
204 );
205 }
206 return this.dist;
207 }
208
209
210
211
212
213 public File dataDir() {
214 return this.data;
215 }
216
217
218
219
220
221 public File socketFile() {
222 return this.socket;
223 }
224
225
226
227
228
229
230 public boolean clear() {
231 return this.erase;
232 }
233
234
235
236
237
238 public Config config() {
239 if (this.options == null) {
240 this.options = Collections.emptyList();
241 }
242 return new Config(
243 this.port, this.user, this.password, this.dbname,
244 Collections.unmodifiableList(this.options)
245 );
246 }
247
248
249
250
251
252
253 protected abstract void run(Instances instances) throws MojoFailureException;
254
255
256
257
258
259 @Cacheable(forever = true)
260 private static Instances instances() {
261 return new Instances();
262 }
263
264 }