View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * Goal which executes the ibatis migration script command.
31   */
32  @Mojo(name = "script")
33  public final class ScriptCommandMojo extends AbstractCommandMojo<ScriptCommand> {
34  
35    /**
36     * Initial version.
37     */
38    @Parameter(property = "migration.v1", required = true)
39    private String v1;
40  
41    /**
42     * Final version.
43     */
44    @Parameter(property = "migration.v2")
45    private String v2;
46  
47    /**
48     * The output file to be create.
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          // Set the default System.out PrintStream
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          // Print out all generated script. This is the standard migration tool behavior.
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 }