1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.maven.mvnmigrate.report;
17
18 import java.io.File;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.ResourceBundle;
24
25 import org.apache.ibatis.migration.Change;
26 import org.apache.ibatis.migration.commands.StatusCommand;
27 import org.apache.ibatis.migration.operations.StatusOperation;
28 import org.apache.ibatis.migration.options.SelectedOptions;
29 import org.apache.maven.doxia.sink.Sink;
30 import org.apache.maven.doxia.siterenderer.Renderer;
31 import org.apache.maven.model.ReportPlugin;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.maven.reporting.AbstractMavenReport;
37 import org.apache.maven.reporting.MavenReportException;
38 import org.codehaus.plexus.util.xml.Xpp3Dom;
39
40
41
42
43
44 @Mojo(name = "status-report")
45 public final class StatusCommandReportMojo extends AbstractMavenReport {
46
47 private static final File DEFAULT_REPO = new File(".");
48
49 private static final String DEFAULT_ENVIRONMENT = "development";
50
51 private static final boolean DEFAULT_FORCE = false;
52
53
54
55
56 @Parameter(property = "project", required = true, readonly = true)
57 private MavenProject project;
58
59
60
61
62 @Parameter(property = "project.build.directory", readonly = true)
63 private File outputDirectory;
64
65
66
67
68 @Parameter(property = "reactorProjects", readonly = true)
69 protected List<MavenProject> reactorProjects;
70
71
72
73
74 @Component(role = Renderer.class)
75 private Renderer siteRenderer;
76
77
78
79
80 @Parameter(property = "migration.path", defaultValue = ".")
81 protected File repository;
82
83
84
85
86 @Parameter(property = "migration.env", defaultValue = "development")
87 protected String environment;
88
89
90
91
92 @Parameter(property = "migration.force", defaultValue = "false")
93 protected boolean force;
94
95
96
97
98 @Parameter(property = "migration.skip", defaultValue = "false")
99 protected boolean skip;
100
101
102
103
104 @Parameter(property = "migration.aggregate", defaultValue = "false")
105 protected boolean aggregate;
106
107 @Override
108 protected void executeReport(Locale locale) throws MavenReportException {
109 if (aggregate && !project.isExecutionRoot()) {
110 return;
111 }
112
113 if (skip && !aggregate) {
114 if (this.getLog().isInfoEnabled()) {
115 this.getLog().info(getBundle(locale).getString("migration.status.report.skipped"));
116 }
117 return;
118 }
119
120
121 if ("pom".equals(this.project.getPackaging()) && !aggregate) {
122 if (this.getLog().isInfoEnabled()) {
123 this.getLog().info("migration.status.report.skipped.pom");
124 }
125 return;
126 }
127
128 if (this.outputDirectory == null || !this.outputDirectory.exists()) {
129 if (this.getLog().isInfoEnabled()) {
130 this.getLog().info(
131 getBundle(locale).getString(getBundle(locale).getString("migration.status.report.skipped.no.target")));
132 }
133 return;
134 }
135
136 Map<MavenProject, List<Change>> aggregateReport = new HashMap<>();
137
138 for (MavenProject mavenProject : reactorProjects) {
139
140 Map<String, ReportPlugin> reportPluginMap = mavenProject.getModel().getReporting().getReportPluginsAsMap();
141 ReportPlugin plug = reportPluginMap.get(getBundle(locale).getString("migration.plugin.key"));
142
143 Xpp3Dom configurationDom = (Xpp3Dom) plug.getConfiguration();
144
145 File reactorRepo = DEFAULT_REPO;
146 String reactorEnv = DEFAULT_ENVIRONMENT;
147 boolean reactorForce = DEFAULT_FORCE;
148 boolean skipStatusCommand = false;
149
150 for (int i = 0; i < configurationDom.getChildCount(); i++) {
151 Xpp3Dom child = configurationDom.getChild(i);
152 if ("repository".equalsIgnoreCase(child.getName())) {
153 reactorRepo = new File(child.getValue());
154 } else if ("environment".equalsIgnoreCase(child.getName())) {
155 reactorEnv = child.getValue();
156 } else if ("force".equalsIgnoreCase(child.getName())) {
157 reactorForce = Boolean.valueOf(child.getValue());
158 } else if ("skip".equalsIgnoreCase(child.getName())) {
159 skipStatusCommand = Boolean.valueOf(child.getValue());
160 }
161 }
162
163 if (skipStatusCommand) {
164 continue;
165 }
166
167 final SelectedOptions options = new SelectedOptions();
168 options.getPaths().setBasePath(reactorRepo);
169 options.setEnvironment(reactorEnv);
170 options.setForce(reactorForce);
171
172 StatusCommand analyzer = new StatusCommand(options);
173 try {
174 analyzer.execute();
175 StatusOperation operation = analyzer.getOperation();
176 List<Change> analysis = operation.getCurrentStatus();
177
178 aggregateReport.put(mavenProject, analysis);
179 } catch (RuntimeException e) {
180 throw e;
181 } catch (Exception e) {
182 throw new MavenReportException(getBundle(locale).getString("migration.status.report.error"), e);
183 }
184 }
185
186
187 Sink sink = getSink();
188 ResourceBundle bundle = getBundle(locale);
189
190
191 MigrationStatusReportView view = new MigrationStatusReportView();
192 view.generateReport(aggregateReport, sink, bundle, aggregate);
193 }
194
195 @Override
196 protected String getOutputDirectory() {
197 if (this.getLog().isInfoEnabled()) {
198 this.getLog().info(outputDirectory.toString());
199 }
200 return this.outputDirectory.toString();
201 }
202
203 @Override
204 protected MavenProject getProject() {
205 return this.project;
206 }
207
208 @Override
209 protected Renderer getSiteRenderer() {
210 return this.siteRenderer;
211 }
212
213
214
215
216
217
218 @Override
219 public String getOutputName() {
220 return "migration-status-analysis";
221 }
222
223
224
225
226
227
228 @Override
229 public String getName(Locale locale) {
230 return getBundle(locale).getString("migration.status.report.name");
231 }
232
233
234
235
236
237
238 @Override
239 public String getDescription(Locale locale) {
240 return getBundle(locale).getString("migration.status.report.description");
241 }
242
243
244
245
246
247
248
249 protected ResourceBundle getBundle(Locale locale) {
250 return ResourceBundle.getBundle("migration-report", locale, this.getClass().getClassLoader());
251 }
252
253 }