ScriptCommandMojo.java

  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. import java.io.File;
  18. import java.io.PrintStream;
  19. import java.text.MessageFormat;

  20. import org.apache.ibatis.migration.commands.ScriptCommand;
  21. import org.apache.ibatis.migration.options.SelectedOptions;
  22. import org.apache.maven.plugin.MojoExecutionException;
  23. import org.apache.maven.plugin.MojoFailureException;
  24. import org.apache.maven.plugins.annotations.Mojo;
  25. import org.apache.maven.plugins.annotations.Parameter;

  26. /**
  27.  * Goal which executes the ibatis migration script command.
  28.  */
  29. @Mojo(name = "script")
  30. public final class ScriptCommandMojo extends AbstractCommandMojo<ScriptCommand> {

  31.   /**
  32.    * Initial version.
  33.    */
  34.   @Parameter(property = "migration.v1", required = true)
  35.   private String v1;

  36.   /**
  37.    * Final version.
  38.    */
  39.   @Parameter(property = "migration.v2")
  40.   private String v2;

  41.   /**
  42.    * The output file to be create.
  43.    */
  44.   @Parameter(property = "migration.output")
  45.   private File output;

  46.   @Override
  47.   protected ScriptCommand createCommandClass(SelectedOptions options) {
  48.     return new ScriptCommand(options);
  49.   }

  50.   @Override
  51.   public void execute() throws MojoExecutionException, MojoFailureException {
  52.     if (isSkip()) {
  53.       return;
  54.     }

  55.     try {
  56.       init();

  57.       if (this.output == null) {
  58.         // Set the default System.out PrintStream
  59.         this.getCommand().setPrintStream(System.out);
  60.         if (this.getLog().isInfoEnabled()) {
  61.           String[] args = { this.v1, this.v2 };
  62.           MessageFormat format = new MessageFormat(
  63.               getBundle(this.getLocale()).getString("migration.plugin.execution.command.script.sqlscript"));
  64.           this.getLog().info(format.format(args));
  65.         }
  66.         // Print out all generated script. This is the standard migration tool behavior.
  67.         System.out.println("  --- CUT HERE ---");
  68.       } else {
  69.         if (!this.output.exists()) {
  70.           new File(this.output.getParent()).mkdirs();
  71.         }
  72.         this.getCommand().setPrintStream(new PrintStream(this.output));
  73.       }

  74.       StringBuilder cmdParams = new StringBuilder(v1);
  75.       if (v2 != null) {
  76.         cmdParams.append(" ").append(v2);
  77.       }
  78.       this.getCommand().execute(cmdParams.toString());

  79.       if (this.getLog().isInfoEnabled()) {
  80.         if (this.output != null) {
  81.           File[] args = { this.output };
  82.           MessageFormat format = new MessageFormat(
  83.               getBundle(this.getLocale()).getString("migration.plugin.execution.command.create.file"));
  84.           this.getLog().info(format.format(args));
  85.         } else {
  86.           String[] args = { this.v1, this.v2 };
  87.           MessageFormat format = new MessageFormat(
  88.               getBundle(this.getLocale()).getString("migration.plugin.execution.command.script.sqlscript"));
  89.           this.getLog().info(format.format(args));
  90.         }
  91.       }
  92.     } catch (RuntimeException e) {
  93.       throw e;
  94.     } catch (Exception e) {
  95.       throw new MojoExecutionException(e.getMessage(), e);
  96.     }
  97.   }

  98. }