AbstractCommandMojo.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 java.util.Locale;
  21. import java.util.ResourceBundle;

  22. import org.apache.ibatis.migration.commands.BaseCommand;
  23. import org.apache.ibatis.migration.options.SelectedOptions;
  24. import org.apache.maven.plugin.AbstractMojo;
  25. import org.apache.maven.plugin.MojoExecutionException;
  26. import org.apache.maven.plugin.MojoFailureException;
  27. import org.apache.maven.plugins.annotations.Parameter;
  28. import org.mybatis.maven.mvnmigrate.util.MavenOutputStream;

  29. /**
  30.  * Provides to an abstract class that extends {@link AbstractMojo}.
  31.  */
  32. abstract class AbstractCommandMojo<T extends BaseCommand> extends AbstractMojo {

  33.   private final Locale locale = Locale.ENGLISH;

  34.   /**
  35.    * Location of migrate repository.
  36.    */
  37.   @Parameter(property = "migration.path", defaultValue = ".")
  38.   private File repository;

  39.   /**
  40.    * Environment to configure. Default environment is 'development'.
  41.    */
  42.   @Parameter(property = "migration.env", defaultValue = "development")
  43.   private String environment;

  44.   /**
  45.    * Forces script to continue even if SQL errors are encountered.
  46.    */
  47.   @Parameter(property = "migration.force", defaultValue = "false")
  48.   private boolean force;

  49.   /**
  50.    * Skip migration actions.
  51.    */
  52.   @Parameter(property = "migration.skip", defaultValue = "false")
  53.   private boolean skip;

  54.   /**
  55.    * The command to execute.
  56.    */
  57.   private T command;

  58.   /**
  59.    * execute the command.
  60.    */
  61.   @Override
  62.   public void execute() throws MojoExecutionException, MojoFailureException {
  63.     if (this.isSkip()) {
  64.       return;
  65.     }
  66.     this.init();
  67.     this.command.execute();
  68.   }

  69.   /**
  70.    * Initialize the MyBatis Migration command.
  71.    */
  72.   protected void init() throws MojoFailureException {
  73.     try {
  74.       final SelectedOptions options = new SelectedOptions();
  75.       options.getPaths().setBasePath(this.getRepository());
  76.       options.setEnvironment(this.getEnvironment());
  77.       options.setForce(this.isForce());

  78.       this.command = this.createCommandClass(options);
  79.       final PrintStream out = new PrintStream(new MavenOutputStream(this.getLog()));
  80.       this.command.setPrintStream(out);
  81.       this.command.setDriverClassLoader(this.getClass().getClassLoader());

  82.       if (this.getLog().isInfoEnabled()) {
  83.         final String[] args = { this.command.getClass().getSimpleName(),
  84.             this.getBundle(this.locale).getString("migration.plugin.name") };
  85.         final MessageFormat format = new MessageFormat(
  86.             this.getBundle(this.locale).getString("migration.plugin.execution.command"));
  87.         this.getLog().info(format.format(args));
  88.       }
  89.     } catch (final RuntimeException e) {
  90.       throw e;
  91.     } catch (final Exception e) {
  92.       throw new MojoFailureException(this, e.getMessage(), e.getLocalizedMessage());
  93.     }
  94.   }

  95.   protected Locale getLocale() {
  96.     return this.locale;
  97.   }

  98.   protected File getRepository() {
  99.     return this.repository;
  100.   }

  101.   protected String getEnvironment() {
  102.     return this.environment;
  103.   }

  104.   protected boolean isForce() {
  105.     return this.force;
  106.   }

  107.   /**
  108.    * Return the command.
  109.    *
  110.    * @return {@link BaseCommand} the command created.
  111.    */
  112.   protected T getCommand() {
  113.     return this.command;
  114.   }

  115.   /**
  116.    * Test if the skip flag is setted.
  117.    *
  118.    * @return the skip flag.
  119.    */
  120.   protected boolean isSkip() {
  121.     if (this.skip && this.getLog().isInfoEnabled()) {
  122.       final String[] args = { this.getBundle(this.locale).getString("migration.plugin.name") };
  123.       final MessageFormat format = new MessageFormat(
  124.           this.getBundle(this.locale).getString("migration.plugin.execution.command.skipped"));
  125.       this.getLog().info(format.format(args));
  126.     }
  127.     return this.skip;
  128.   }

  129.   /**
  130.    * The current locale.
  131.    *
  132.    * @param locale
  133.    *          the locale
  134.    *
  135.    * @return the bundle
  136.    */
  137.   protected ResourceBundle getBundle(final Locale locale) {
  138.     return ResourceBundle.getBundle("migration-plugin", locale, this.getClass().getClassLoader());
  139.   }

  140.   /**
  141.    * Creates the specific mojo command.
  142.    *
  143.    * @param options
  144.    *          the options
  145.    *
  146.    * @return The command created.
  147.    */
  148.   protected abstract T createCommandClass(final SelectedOptions options);

  149. }