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(getBundle(locale).getString("migration.status.report.skipped.no.target"));
133 }
134 return;
135 }
136
137 Map<MavenProject, List<Change>> aggregateReport = new HashMap<>();
138
139 for (MavenProject mavenProject : reactorProjects) {
140
141 Map<String, ReportPlugin> reportPluginMap = mavenProject.getModel().getReporting().getReportPluginsAsMap();
142 ReportPlugin plug = reportPluginMap.get(getBundle(locale).getString("migration.plugin.key"));
143
144 Xpp3Dom configurationDom = (Xpp3Dom) plug.getConfiguration();
145
146 Path reactorRepo = DEFAULT_REPO;
147 String reactorEnv = DEFAULT_ENVIRONMENT;
148 boolean reactorForce = DEFAULT_FORCE;
149 boolean skipStatusCommand = false;
150
151 for (int i = 0; i < configurationDom.getChildCount(); i++) {
152 Xpp3Dom child = configurationDom.getChild(i);
153 if ("repository".equalsIgnoreCase(child.getName())) {
154 reactorRepo = Path.of(child.getValue());
155 } else if ("environment".equalsIgnoreCase(child.getName())) {
156 reactorEnv = child.getValue();
157 } else if ("force".equalsIgnoreCase(child.getName())) {
158 reactorForce = Boolean.valueOf(child.getValue());
159 } else if ("skip".equalsIgnoreCase(child.getName())) {
160 skipStatusCommand = Boolean.valueOf(child.getValue());
161 }
162 }
163
164 if (skipStatusCommand) {
165 continue;
166 }
167
168 final SelectedOptions options = new SelectedOptions();
169 options.getPaths().setBasePath(reactorRepo.toFile());
170 options.setEnvironment(reactorEnv);
171 options.setForce(reactorForce);
172
173 StatusCommand analyzer = new StatusCommand(options);
174 try {
175 analyzer.execute();
176 StatusOperation operation = analyzer.getOperation();
177 List<Change> analysis = operation.getCurrentStatus();
178
179 aggregateReport.put(mavenProject, analysis);
180 } catch (RuntimeException e) {
181 throw e;
182 } catch (Exception e) {
183 throw new MavenReportException(getBundle(locale).getString("migration.status.report.error"), e);
184 }
185 }
186
187
188 Sink sink = getSink();
189 ResourceBundle bundle = getBundle(locale);
190
191
192 MigrationStatusReportView view = new MigrationStatusReportView();
193 view.generateReport(aggregateReport, sink, bundle, aggregate);
194 }
195
196 @Override
197 protected String getOutputDirectory() {
198 if (this.getLog().isInfoEnabled()) {
199 this.getLog().info(outputDirectory.toString());
200 }
201 return this.outputDirectory.toString();
202 }
203
204 @Override
205 protected MavenProject getProject() {
206 return this.project;
207 }
208
209 @Override
210 protected Renderer getSiteRenderer() {
211 return this.siteRenderer;
212 }
213
214
215
216
217
218
219 @Override
220 public String getOutputName() {
221 return "migration-status-analysis";
222 }
223
224
225
226
227
228
229 @Override
230 public String getName(Locale locale) {
231 return getBundle(locale).getString("migration.status.report.name");
232 }
233
234
235
236
237
238
239 @Override
240 public String getDescription(Locale locale) {
241 return getBundle(locale).getString("migration.status.report.description");
242 }
243
244
245
246
247
248
249
250 protected ResourceBundle getBundle(Locale locale) {
251 return ResourceBundle.getBundle("migration-report", locale, this.getClass().getClassLoader());
252 }
253
254 }