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.nio.file.Files;
21 import java.nio.file.Path;
22 import java.text.MessageFormat;
23
24 import org.apache.ibatis.migration.commands.ScriptCommand;
25 import org.apache.ibatis.migration.options.SelectedOptions;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30
31
32
33
34 @Mojo(name = "script")
35 public final class ScriptCommandMojo extends AbstractCommandMojo<ScriptCommand> {
36
37
38
39
40 @Parameter(property = "migration.v1", required = true)
41 private String v1;
42
43
44
45
46 @Parameter(property = "migration.v2")
47 private String v2;
48
49
50
51
52 @Parameter(property = "migration.output")
53 private File output;
54
55 @Override
56 protected ScriptCommand createCommandClass(SelectedOptions options) {
57 return new ScriptCommand(options);
58 }
59
60 @Override
61 public void execute() throws MojoExecutionException, MojoFailureException {
62 if (isSkip()) {
63 return;
64 }
65
66 try {
67 init();
68
69 if (this.output == null) {
70
71 this.getCommand().setPrintStream(System.out);
72 if (this.getLog().isInfoEnabled()) {
73 String[] args = { this.v1, this.v2 };
74 MessageFormat format = new MessageFormat(
75 getBundle(this.getLocale()).getString("migration.plugin.execution.command.script.sqlscript"));
76 this.getLog().info(format.format(args));
77 }
78
79 System.out.println(" --- CUT HERE ---");
80 } else {
81 if (!this.output.exists()) {
82 Files.createDirectories(Path.of(this.output.getParent()));
83 }
84 this.getCommand().setPrintStream(new PrintStream(this.output));
85 }
86
87 StringBuilder cmdParams = new StringBuilder(v1);
88 if (v2 != null) {
89 cmdParams.append(" ").append(v2);
90 }
91 this.getCommand().execute(cmdParams.toString());
92
93 if (this.getLog().isInfoEnabled()) {
94 if (this.output != null) {
95 File[] args = { this.output };
96 MessageFormat format = new MessageFormat(
97 getBundle(this.getLocale()).getString("migration.plugin.execution.command.create.file"));
98 this.getLog().info(format.format(args));
99 } else {
100 String[] args = { this.v1, this.v2 };
101 MessageFormat format = new MessageFormat(
102 getBundle(this.getLocale()).getString("migration.plugin.execution.command.script.sqlscript"));
103 this.getLog().info(format.format(args));
104 }
105 }
106 } catch (RuntimeException e) {
107 throw e;
108 } catch (Exception e) {
109 throw new MojoExecutionException(e.getMessage(), e);
110 }
111 }
112
113 }