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