View Javadoc
1   /*
2    *    Copyright 2010-2025 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.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   * Goal which executes the ibatis migration script command.
33   */
34  @Mojo(name = "script")
35  public final class ScriptCommandMojo extends AbstractCommandMojo<ScriptCommand> {
36  
37    /**
38     * Initial version.
39     */
40    @Parameter(property = "migration.v1", required = true)
41    private String v1;
42  
43    /**
44     * Final version.
45     */
46    @Parameter(property = "migration.v2")
47    private String v2;
48  
49    /**
50     * The output file to be create.
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          // Set the default System.out PrintStream
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          // Print out all generated script. This is the standard migration tool behavior.
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 }