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.text.MessageFormat;
19 import java.util.List;
20
21 import org.apache.ibatis.migration.Change;
22 import org.apache.ibatis.migration.commands.StatusCommand;
23 import org.apache.ibatis.migration.operations.StatusOperation;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28
29
30
31
32 @Mojo(name = "check", defaultPhase = LifecyclePhase.TEST)
33 public final class CheckCommandMojo extends StatusCommandMojo {
34
35 private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
36
37
38
39
40 @Override
41 public void execute() throws MojoExecutionException, MojoFailureException {
42 if (isSkip()) {
43 return;
44 }
45
46 init();
47
48 if (getCommand() instanceof StatusCommand) {
49 StatusCommand command = getCommand();
50 command.execute();
51 StatusOperation operation = command.getOperation();
52 List<Change> changes = operation.getCurrentStatus();
53 int pendings = operation.getPendingCount();
54 if (pendings > 0) {
55 Integer[] args = { pendings };
56 MessageFormat format = new MessageFormat(
57 getBundle(this.getLocale()).getString("migration.plugin.execution.check.failed"));
58 throw new MojoFailureException(this, LINE_SEPARATOR + format.format(args), createLongMessage(changes));
59 }
60 }
61 }
62
63
64
65
66
67
68
69
70
71 private String createLongMessage(List<Change> changes) {
72 StringBuilder builder = new StringBuilder();
73 builder.append("ID Applied At Description");
74 builder.append(LINE_SEPARATOR);
75 for (Change change : changes) {
76 builder.append(change);
77 builder.append(LINE_SEPARATOR);
78 }
79 return builder.toString();
80 }
81
82 @Override
83 public String toString() {
84 return getBundle(this.getLocale()).getString("migration.plugin.name") + " " + this.getClass().getSimpleName();
85 }
86
87 }