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