1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.maven.mvnmigrate;
17
18 import java.io.File;
19 import java.io.PrintStream;
20 import java.text.MessageFormat;
21 import java.util.Locale;
22 import java.util.ResourceBundle;
23
24 import org.apache.ibatis.migration.commands.BaseCommand;
25 import org.apache.ibatis.migration.options.SelectedOptions;
26 import org.apache.maven.plugin.AbstractMojo;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.mybatis.maven.mvnmigrate.util.MavenOutputStream;
31
32
33
34
35 abstract class AbstractCommandMojo<T extends BaseCommand> extends AbstractMojo {
36
37 private final Locale locale = Locale.ENGLISH;
38
39
40
41
42 @Parameter(property = "migration.path", defaultValue = ".")
43 private File repository;
44
45
46
47
48 @Parameter(property = "migration.env", defaultValue = "development")
49 private String environment;
50
51
52
53
54 @Parameter(property = "migration.force", defaultValue = "false")
55 private boolean force;
56
57
58
59
60 @Parameter(property = "migration.skip", defaultValue = "false")
61 private boolean skip;
62
63
64
65
66 private T command;
67
68
69
70
71 @Override
72 public void execute() throws MojoExecutionException, MojoFailureException {
73 if (this.isSkip()) {
74 return;
75 }
76 this.init();
77 this.command.execute();
78 }
79
80
81
82
83 protected void init() throws MojoFailureException {
84 try {
85 final SelectedOptions options = new SelectedOptions();
86 options.getPaths().setBasePath(this.getRepository());
87 options.setEnvironment(this.getEnvironment());
88 options.setForce(this.isForce());
89
90 this.command = this.createCommandClass(options);
91 final PrintStream out = new PrintStream(new MavenOutputStream(this.getLog()));
92 this.command.setPrintStream(out);
93 this.command.setDriverClassLoader(this.getClass().getClassLoader());
94
95 if (this.getLog().isInfoEnabled()) {
96 final String[] args = { this.command.getClass().getSimpleName(),
97 this.getBundle(this.locale).getString("migration.plugin.name") };
98 final MessageFormat format = new MessageFormat(
99 this.getBundle(this.locale).getString("migration.plugin.execution.command"));
100 this.getLog().info(format.format(args));
101 }
102 } catch (final RuntimeException e) {
103 throw e;
104 } catch (final Exception e) {
105 throw new MojoFailureException(this, e.getMessage(), e.getLocalizedMessage());
106 }
107 }
108
109 protected Locale getLocale() {
110 return this.locale;
111 }
112
113 protected File getRepository() {
114 return this.repository;
115 }
116
117 protected String getEnvironment() {
118 return this.environment;
119 }
120
121 protected boolean isForce() {
122 return this.force;
123 }
124
125
126
127
128
129
130 protected T getCommand() {
131 return this.command;
132 }
133
134
135
136
137
138
139 protected boolean isSkip() {
140 if (this.skip && this.getLog().isInfoEnabled()) {
141 final String[] args = { this.getBundle(this.locale).getString("migration.plugin.name") };
142 final MessageFormat format = new MessageFormat(
143 this.getBundle(this.locale).getString("migration.plugin.execution.command.skipped"));
144 this.getLog().info(format.format(args));
145 }
146 return this.skip;
147 }
148
149
150
151
152
153
154
155
156
157 protected ResourceBundle getBundle(final Locale locale) {
158 return ResourceBundle.getBundle("migration-plugin", locale, this.getClass().getClassLoader());
159 }
160
161
162
163
164
165
166
167
168
169 protected abstract T createCommandClass(final SelectedOptions options);
170
171 }