CheckCommandMojo.java

  1. /*
  2.  *    Copyright 2010-2024 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.text.MessageFormat;
  18. import java.util.List;

  19. import org.apache.ibatis.migration.Change;
  20. import org.apache.ibatis.migration.commands.StatusCommand;
  21. import org.apache.ibatis.migration.operations.StatusOperation;
  22. import org.apache.maven.plugin.MojoExecutionException;
  23. import org.apache.maven.plugin.MojoFailureException;
  24. import org.apache.maven.plugins.annotations.LifecyclePhase;
  25. import org.apache.maven.plugins.annotations.Mojo;

  26. /**
  27.  * Goal which check the presence of pending migration.
  28.  */
  29. @Mojo(name = "check", defaultPhase = LifecyclePhase.TEST)
  30. public final class CheckCommandMojo extends StatusCommandMojo {

  31.   private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");

  32.   /**
  33.    * {@inheritDoc}
  34.    */
  35.   @Override
  36.   public void execute() throws MojoExecutionException, MojoFailureException {
  37.     if (isSkip()) {
  38.       return;
  39.     }

  40.     init();

  41.     if (getCommand() instanceof StatusCommand) {
  42.       StatusCommand command = getCommand();
  43.       command.execute();
  44.       StatusOperation operation = command.getOperation();
  45.       List<Change> changes = operation.getCurrentStatus();
  46.       int pendings = operation.getPendingCount();
  47.       if (pendings > 0) {
  48.         Integer[] args = { pendings };
  49.         MessageFormat format = new MessageFormat(
  50.             getBundle(this.getLocale()).getString("migration.plugin.execution.check.failed"));
  51.         throw new MojoFailureException(this, LINE_SEPARATOR + format.format(args), createLongMessage(changes));
  52.       }
  53.     }
  54.   }

  55.   /**
  56.    * Creates a user information message about all migration pending changes
  57.    *
  58.    * @param changes
  59.    *          List of migration changes
  60.    *
  61.    * @return User information message about all migration pending changes.
  62.    */
  63.   private String createLongMessage(List<Change> changes) {
  64.     StringBuilder builder = new StringBuilder();
  65.     builder.append("ID             Applied At          Description");
  66.     builder.append(LINE_SEPARATOR);
  67.     for (Change change : changes) {
  68.       builder.append(change);
  69.       builder.append(LINE_SEPARATOR);
  70.     }
  71.     return builder.toString();
  72.   }

  73.   @Override
  74.   public String toString() {
  75.     return getBundle(this.getLocale()).getString("migration.plugin.name") + " " + this.getClass().getSimpleName();
  76.   }

  77. }