Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractMysqlMojo |
|
| 1.6666666666666667;1.667 | ||||
AbstractMysqlMojo$AjcClosure1 |
|
| 1.6666666666666667;1.667 |
1 | 0 | /** |
2 | * Copyright (c) 2012-2014, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
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 | * Abstract MOJO. | |
47 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
48 | * @version $Id$ | |
49 | * @since 0.1 | |
50 | */ | |
51 | 0 | @ToString |
52 | 0 | @EqualsAndHashCode(callSuper = false) |
53 | 2 | abstract class AbstractMysqlMojo extends AbstractMojo { |
54 | ||
55 | /** | |
56 | * Property that will be exported by the plugin indicating if an existing | |
57 | * database could be reused. | |
58 | */ | |
59 | private static final String PROPERTY_REUSED = "jcabi.reused.database"; | |
60 | ||
61 | /** | |
62 | * The Maven project. | |
63 | */ | |
64 | @Parameter( | |
65 | defaultValue = "${project}", | |
66 | readonly = true | |
67 | ) | |
68 | private transient MavenProject project; | |
69 | ||
70 | /** | |
71 | * Shall we skip execution? | |
72 | */ | |
73 | @Parameter( | |
74 | defaultValue = "false", | |
75 | required = false | |
76 | ) | |
77 | private transient boolean skip; | |
78 | ||
79 | /** | |
80 | * Port to use. | |
81 | */ | |
82 | @Parameter( | |
83 | defaultValue = "3306", | |
84 | required = false | |
85 | ) | |
86 | private transient int port; | |
87 | ||
88 | /** | |
89 | * Location of MySQL distribution. | |
90 | */ | |
91 | @Parameter( | |
92 | defaultValue = "${project.build.directory}/mysql-dist", | |
93 | required = true | |
94 | ) | |
95 | private transient File dist; | |
96 | ||
97 | /** | |
98 | * Username to use. | |
99 | */ | |
100 | @Parameter( | |
101 | defaultValue = "root", | |
102 | required = false | |
103 | ) | |
104 | private transient String user; | |
105 | ||
106 | /** | |
107 | * Password to use. | |
108 | */ | |
109 | @Parameter( | |
110 | defaultValue = "root", | |
111 | required = false | |
112 | ) | |
113 | private transient String password; | |
114 | ||
115 | /** | |
116 | * Database name to use. | |
117 | */ | |
118 | @Parameter( | |
119 | defaultValue = "root", | |
120 | required = false | |
121 | ) | |
122 | private transient String dbname; | |
123 | ||
124 | /** | |
125 | * Location of MySQL data. | |
126 | */ | |
127 | @Parameter( | |
128 | defaultValue = "${project.build.directory}/mysql-data", | |
129 | required = true | |
130 | ) | |
131 | private transient File data; | |
132 | ||
133 | /** | |
134 | * Override location of MySQL socket file. Defaults to | |
135 | * (data dir)/mysql.socket. | |
136 | */ | |
137 | @Parameter( | |
138 | required = false | |
139 | ) | |
140 | private transient File socket; | |
141 | ||
142 | /** | |
143 | * Shall we always delete an existing database or reuse it? | |
144 | */ | |
145 | @Parameter( | |
146 | defaultValue = "true", | |
147 | required = false | |
148 | ) | |
149 | private transient boolean clearexistingdata; | |
150 | ||
151 | /** | |
152 | * Configuration options. | |
153 | */ | |
154 | @Parameter( | |
155 | required = false | |
156 | ) | |
157 | private transient List<String> options; | |
158 | ||
159 | /** | |
160 | * Set skip option. | |
161 | * @param skp Shall we skip execution? | |
162 | */ | |
163 | public void setSkip(final boolean skp) { | |
164 | 2 | this.skip = skp; |
165 | 2 | } |
166 | ||
167 | @Override | |
168 | public void execute() throws MojoFailureException { | |
169 | 2 | StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); |
170 | 2 | if (this.skip) { |
171 | 2 | Logger.info(this, "execution skipped because of 'skip' option"); |
172 | 2 | return; |
173 | } | |
174 | 0 | this.run(AbstractMysqlMojo.instances()); |
175 | 0 | if (this.project == null) { |
176 | 0 | Logger.warn( |
177 | this, | |
178 | "MavenProject not initialized, unable to set property %s", | |
179 | PROPERTY_REUSED | |
180 | ); | |
181 | } else { | |
182 | 0 | Logger.info( |
183 | this, | |
184 | "set Maven property %s = %s ", | |
185 | PROPERTY_REUSED, | |
186 | AbstractMysqlMojo.instances().reusedExistingDatabase() | |
187 | ); | |
188 | 0 | this.project.getProperties().setProperty( |
189 | PROPERTY_REUSED, | |
190 | Boolean.toString( | |
191 | AbstractMysqlMojo.instances().reusedExistingDatabase() | |
192 | ) | |
193 | ); | |
194 | } | |
195 | 0 | } |
196 | ||
197 | /** | |
198 | * Get directory with MySQL dist. | |
199 | * @return Directory | |
200 | * @throws MojoFailureException If fails | |
201 | */ | |
202 | public File distDir() throws MojoFailureException { | |
203 | 0 | if (!this.dist.exists()) { |
204 | 0 | throw new MojoFailureException( |
205 | String.format( | |
206 | "MySQL distribution directory doesn't exist: %s", this.dist | |
207 | ) | |
208 | ); | |
209 | } | |
210 | 0 | return this.dist; |
211 | } | |
212 | ||
213 | /** | |
214 | * Get directory with MySQL data. | |
215 | * @return Directory | |
216 | */ | |
217 | public File dataDir() { | |
218 | 0 | return this.data; |
219 | } | |
220 | ||
221 | /** | |
222 | * Get MySQL socket location. | |
223 | * @return Overridden socket location (null for default) | |
224 | */ | |
225 | public File socketFile() { | |
226 | 0 | return this.socket; |
227 | } | |
228 | ||
229 | /** | |
230 | * If true, always delete existing database files and create a new instance | |
231 | * from scratch. If false, try to reuse existing files. | |
232 | * @return If existing database files should be deleted. | |
233 | */ | |
234 | public boolean clear() { | |
235 | 0 | return this.clearexistingdata; |
236 | } | |
237 | ||
238 | /** | |
239 | * Get configuration. | |
240 | * @return Configuration | |
241 | */ | |
242 | public Config config() { | |
243 | 0 | if (this.options == null) { |
244 | 0 | this.options = Collections.emptyList(); |
245 | } | |
246 | 0 | return new Config( |
247 | this.port, this.user, this.password, this.dbname, | |
248 | Collections.unmodifiableList(this.options) | |
249 | ); | |
250 | } | |
251 | ||
252 | /** | |
253 | * Run custom functionality. | |
254 | * @param instances Instances to work with | |
255 | * @throws MojoFailureException If fails | |
256 | */ | |
257 | protected abstract void run(final Instances instances) | |
258 | throws MojoFailureException; | |
259 | ||
260 | /** | |
261 | * Get instances. | |
262 | * @return Instances | |
263 | */ | |
264 | @Cacheable(forever = true) | |
265 | private static Instances instances() { | |
266 | 0 | return new Instances(); |
267 | } | |
268 | ||
269 | } |