• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / migrations-maven-plugin / #631

13 Dec 2023 04:45PM CUT coverage: 22.137%. Remained the same
#631

Pull #290

github

web-flow
Merge c555088aa into b36811a2d
Pull Request #290: Update dependency org.apache.maven.reporting:maven-reporting-impl to v4.0.0-M13

28 of 226 branches covered (0.0%)

116 of 524 relevant lines covered (22.14%)

0.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.92
/src/main/java/org/mybatis/maven/mvnmigrate/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

18
import java.io.File;
19
import java.io.PrintStream;
20
import java.text.MessageFormat;
21
import java.util.Locale;
22
import java.util.ResourceBundle;
23

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

32
/**
33
 * Provides to an abstract class that extends {@link AbstractMojo}.
34
 */
35
abstract class AbstractCommandMojo<T extends BaseCommand> extends AbstractMojo {
1✔
36

37
  private final Locale locale = Locale.ENGLISH;
1✔
38

39
  /**
40
   * Location of migrate repository.
41
   */
42
  @Parameter(property = "migration.path", defaultValue = ".")
43
  private File repository;
44

45
  /**
46
   * Environment to configure. Default environment is 'development'.
47
   */
48
  @Parameter(property = "migration.env", defaultValue = "development")
49
  private String environment;
50

51
  /**
52
   * Forces script to continue even if SQL errors are encountered.
53
   */
54
  @Parameter(property = "migration.force", defaultValue = "false")
55
  private boolean force;
56

57
  /**
58
   * Skip migration actions.
59
   */
60
  @Parameter(property = "migration.skip", defaultValue = "false")
61
  private boolean skip;
62

63
  /**
64
   * The command to execute.
65
   */
66
  private T command;
67

68
  /**
69
   * execute the command.
70
   */
71
  @Override
72
  public void execute() throws MojoExecutionException, MojoFailureException {
73
    if (this.isSkip()) {
1!
74
      return;
×
75
    }
76
    this.init();
1✔
77
    this.command.execute();
1✔
78
  }
1✔
79

80
  /**
81
   * Initialize the MyBatis Migration command.
82
   */
83
  protected void init() throws MojoFailureException {
84
    try {
85
      final SelectedOptions options = new SelectedOptions();
1✔
86
      options.getPaths().setBasePath(this.getRepository());
1✔
87
      options.setEnvironment(this.getEnvironment());
1✔
88
      options.setForce(this.isForce());
1✔
89

90
      this.command = this.createCommandClass(options);
1✔
91
      final PrintStream out = new PrintStream(new MavenOutputStream(this.getLog()));
1✔
92
      this.command.setPrintStream(out);
1✔
93
      this.command.setDriverClassLoader(this.getClass().getClassLoader());
1✔
94

95
      if (this.getLog().isInfoEnabled()) {
1!
96
        final String[] args = { this.command.getClass().getSimpleName(),
1✔
97
            this.getBundle(this.locale).getString("migration.plugin.name") };
1✔
98
        final MessageFormat format = new MessageFormat(
1✔
99
            this.getBundle(this.locale).getString("migration.plugin.execution.command"));
1✔
100
        this.getLog().info(format.format(args));
1✔
101
      }
102
    } catch (final RuntimeException e) {
×
103
      throw e;
×
104
    } catch (final Exception e) {
×
105
      throw new MojoFailureException(this, e.getMessage(), e.getLocalizedMessage());
×
106
    }
1✔
107
  }
1✔
108

109
  protected Locale getLocale() {
110
    return this.locale;
1✔
111
  }
112

113
  protected File getRepository() {
114
    return this.repository;
1✔
115
  }
116

117
  protected String getEnvironment() {
118
    return this.environment;
1✔
119
  }
120

121
  protected boolean isForce() {
122
    return this.force;
1✔
123
  }
124

125
  /**
126
   * Return the command.
127
   *
128
   * @return {@link BaseCommand} the command created.
129
   */
130
  protected T getCommand() {
131
    return this.command;
1✔
132
  }
133

134
  /**
135
   * Test if the skip flag is setted.
136
   *
137
   * @return the skip flag.
138
   */
139
  protected boolean isSkip() {
140
    if (this.skip && this.getLog().isInfoEnabled()) {
1!
141
      final String[] args = { this.getBundle(this.locale).getString("migration.plugin.name") };
×
142
      final MessageFormat format = new MessageFormat(
×
143
          this.getBundle(this.locale).getString("migration.plugin.execution.command.skipped"));
×
144
      this.getLog().info(format.format(args));
×
145
    }
146
    return this.skip;
1✔
147
  }
148

149
  /**
150
   * The current locale.
151
   *
152
   * @param locale
153
   *          the locale
154
   *
155
   * @return the bundle
156
   */
157
  protected ResourceBundle getBundle(final Locale locale) {
158
    return ResourceBundle.getBundle("migration-plugin", locale, this.getClass().getClassLoader());
1✔
159
  }
160

161
  /**
162
   * Creates the specific mojo command.
163
   *
164
   * @param options
165
   *          the options
166
   *
167
   * @return The command created.
168
   */
169
  protected abstract T createCommandClass(final SelectedOptions options);
170

171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc