StatusCommandReportMojo.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.report;

  17. import java.io.File;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import java.util.ResourceBundle;

  23. import org.apache.ibatis.migration.Change;
  24. import org.apache.ibatis.migration.commands.StatusCommand;
  25. import org.apache.ibatis.migration.operations.StatusOperation;
  26. import org.apache.ibatis.migration.options.SelectedOptions;
  27. import org.apache.maven.doxia.sink.Sink;
  28. import org.apache.maven.doxia.siterenderer.Renderer;
  29. import org.apache.maven.model.ReportPlugin;
  30. import org.apache.maven.plugins.annotations.Component;
  31. import org.apache.maven.plugins.annotations.Mojo;
  32. import org.apache.maven.plugins.annotations.Parameter;
  33. import org.apache.maven.project.MavenProject;
  34. import org.apache.maven.reporting.AbstractMavenReport;
  35. import org.apache.maven.reporting.MavenReportException;
  36. import org.codehaus.plexus.util.xml.Xpp3Dom;

  37. /**
  38.  * Extends {@link AbstractMavenReport}. <br>
  39.  * Class to generate a Maven report.
  40.  */
  41. @Mojo(name = "status-report")
  42. public final class StatusCommandReportMojo extends AbstractMavenReport {

  43.   private static final File DEFAULT_REPO = new File(".");

  44.   private static final String DEFAULT_ENVIRONMENT = "development";

  45.   private static final boolean DEFAULT_FORCE = false;

  46.   /**
  47.    * The Maven project to analyze.
  48.    */
  49.   @Parameter(property = "project", required = true, readonly = true)
  50.   private MavenProject project;

  51.   /**
  52.    * Target folder.
  53.    */
  54.   @Parameter(property = "project.build.directory", readonly = true)
  55.   private File outputDirectory;

  56.   /**
  57.    * The projects in the reactor for aggregation report.
  58.    */
  59.   @Parameter(property = "reactorProjects", readonly = true)
  60.   protected List<MavenProject> reactorProjects;

  61.   /**
  62.    * The project site renderer.
  63.    */
  64.   @Component(role = Renderer.class)
  65.   private Renderer siteRenderer;

  66.   /**
  67.    * Location of migrate repository.
  68.    */
  69.   @Parameter(property = "migration.path", defaultValue = ".")
  70.   protected File repository;

  71.   /**
  72.    * Environment to configure. Default environment is 'development'.
  73.    */
  74.   @Parameter(property = "migration.env", defaultValue = "development")
  75.   protected String environment;

  76.   /**
  77.    * Forces script to continue even if SQL errors are encountered.
  78.    */
  79.   @Parameter(property = "migration.force", defaultValue = "false")
  80.   protected boolean force;

  81.   /**
  82.    * Skip migration actions.
  83.    */
  84.   @Parameter(property = "migration.skip", defaultValue = "false")
  85.   protected boolean skip;

  86.   /**
  87.    * Aggregate report results.
  88.    */
  89.   @Parameter(property = "migration.aggregate", defaultValue = "false")
  90.   protected boolean aggregate;

  91.   @Override
  92.   protected void executeReport(Locale locale) throws MavenReportException {
  93.     if (aggregate && !project.isExecutionRoot()) {
  94.       return;
  95.     }

  96.     if (skip && !aggregate) {
  97.       if (this.getLog().isInfoEnabled()) {
  98.         this.getLog().info(getBundle(locale).getString("migration.status.report.skipped"));
  99.       }
  100.       return;
  101.     }

  102.     // Step 0: Checking pom availability
  103.     if ("pom".equals(this.project.getPackaging()) && !aggregate) {
  104.       if (this.getLog().isInfoEnabled()) {
  105.         this.getLog().info("migration.status.report.skipped.pom");
  106.       }
  107.       return;
  108.     }

  109.     if (this.outputDirectory == null || !this.outputDirectory.exists()) {
  110.       if (this.getLog().isInfoEnabled()) {
  111.         this.getLog().info(
  112.             getBundle(locale).getString(getBundle(locale).getString("migration.status.report.skipped.no.target")));
  113.       }
  114.       return;
  115.     }

  116.     Map<MavenProject, List<Change>> aggregateReport = new HashMap<>();

  117.     for (MavenProject mavenProject : reactorProjects) {

  118.       Map<String, ReportPlugin> reportPluginMap = mavenProject.getModel().getReporting().getReportPluginsAsMap();
  119.       ReportPlugin plug = reportPluginMap.get(getBundle(locale).getString("migration.plugin.key"));

  120.       Xpp3Dom configurationDom = (Xpp3Dom) plug.getConfiguration();

  121.       File reactorRepo = DEFAULT_REPO;
  122.       String reactorEnv = DEFAULT_ENVIRONMENT;
  123.       boolean reactorForce = DEFAULT_FORCE;
  124.       boolean skipStatusCommand = false;

  125.       for (int i = 0; i < configurationDom.getChildCount(); i++) {
  126.         Xpp3Dom child = configurationDom.getChild(i);
  127.         if ("repository".equalsIgnoreCase(child.getName())) {
  128.           reactorRepo = new File(child.getValue());
  129.         } else if ("environment".equalsIgnoreCase(child.getName())) {
  130.           reactorEnv = child.getValue();
  131.         } else if ("force".equalsIgnoreCase(child.getName())) {
  132.           reactorForce = Boolean.valueOf(child.getValue());
  133.         } else if ("skip".equalsIgnoreCase(child.getName())) {
  134.           skipStatusCommand = Boolean.valueOf(child.getValue());
  135.         }
  136.       }

  137.       if (skipStatusCommand) {
  138.         continue;
  139.       }

  140.       final SelectedOptions options = new SelectedOptions();
  141.       options.getPaths().setBasePath(reactorRepo);
  142.       options.setEnvironment(reactorEnv);
  143.       options.setForce(reactorForce);

  144.       StatusCommand analyzer = new StatusCommand(options);
  145.       try {
  146.         analyzer.execute();
  147.         StatusOperation operation = analyzer.getOperation();
  148.         List<Change> analysis = operation.getCurrentStatus();

  149.         aggregateReport.put(mavenProject, analysis);
  150.       } catch (RuntimeException e) {
  151.         throw e;
  152.       } catch (Exception e) {
  153.         throw new MavenReportException(getBundle(locale).getString("migration.status.report.error"), e);
  154.       }
  155.     }

  156.     // Step 2: Create sink and bundle
  157.     Sink sink = getSink();
  158.     ResourceBundle bundle = getBundle(locale);

  159.     // Step 3: Generate the report
  160.     MigrationStatusReportView view = new MigrationStatusReportView();
  161.     view.generateReport(aggregateReport, sink, bundle, aggregate);
  162.   }

  163.   @Override
  164.   protected String getOutputDirectory() {
  165.     if (this.getLog().isInfoEnabled()) {
  166.       this.getLog().info(outputDirectory.toString());
  167.     }
  168.     return this.outputDirectory.toString();
  169.   }

  170.   @Override
  171.   protected MavenProject getProject() {
  172.     return this.project;
  173.   }

  174.   @Override
  175.   protected Renderer getSiteRenderer() {
  176.     return this.siteRenderer;
  177.   }

  178.   /**
  179.    * Return the output name of the report.
  180.    *
  181.    * @return the output name.
  182.    */
  183.   @Override
  184.   public String getOutputName() {
  185.     return "migration-status-analysis";
  186.   }

  187.   /**
  188.    * Return the name of the report.
  189.    *
  190.    * @return the name of the report.
  191.    */
  192.   @Override
  193.   public String getName(Locale locale) {
  194.     return getBundle(locale).getString("migration.status.report.name");
  195.   }

  196.   /**
  197.    * Return the description of the report.
  198.    *
  199.    * @return the description of the report.
  200.    */
  201.   @Override
  202.   public String getDescription(Locale locale) {
  203.     return getBundle(locale).getString("migration.status.report.description");
  204.   }

  205.   /**
  206.    * Return the {@link ResourceBundle} given the current locale.
  207.    *
  208.    * @param locale
  209.    *          the current locale.
  210.    */
  211.   protected ResourceBundle getBundle(Locale locale) {
  212.     return ResourceBundle.getBundle("migration-report", locale, this.getClass().getClassLoader());
  213.   }

  214. }