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

mybatis / generator / 2135

02 Apr 2026 05:50PM UTC coverage: 91.765% (+1.4%) from 90.382%
2135

Pull #1485

github

web-flow
Merge a9306dd47 into 18f1f002d
Pull Request #1485: Code Cleanup and Coverage

2425 of 3126 branches covered (77.58%)

134 of 172 new or added lines in 28 files covered. (77.91%)

7 existing lines in 4 files now uncovered.

11879 of 12945 relevant lines covered (91.77%)

0.92 hits per line

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

0.0
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/ShellRunner.java
1
/*
2
 *    Copyright 2006-2026 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.generator.api;
17

18
import static org.mybatis.generator.internal.util.messages.Messages.getString;
19

20
import java.io.IOException;
21
import java.nio.file.Files;
22
import java.nio.file.Path;
23
import java.sql.SQLException;
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
28
import java.util.Set;
29

30
import org.mybatis.generator.config.Configuration;
31
import org.mybatis.generator.config.xml.ConfigurationParser;
32
import org.mybatis.generator.exception.InvalidConfigurationException;
33
import org.mybatis.generator.exception.XMLParserException;
34
import org.mybatis.generator.internal.DefaultShellCallback;
35
import org.mybatis.generator.internal.util.StringUtility;
36

37
/**
38
 * This class allows the code generator to be run from the command line.
39
 *
40
 * @author Jeff Butler
41
 */
42
public class ShellRunner {
×
43
    private static final String CONFIG_FILE = "-configfile"; //$NON-NLS-1$
44
    private static final String OVERWRITE = "-overwrite"; //$NON-NLS-1$
45
    private static final String CONTEXT_IDS = "-contextids"; //$NON-NLS-1$
46
    private static final String TABLES = "-tables"; //$NON-NLS-1$
47
    private static final String VERBOSE = "-verbose"; //$NON-NLS-1$
48
    private static final String JAVA_MERGE_ENABLED = "-javaMergeEnabled";
49
    private static final String HELP_1 = "-?"; //$NON-NLS-1$
50
    private static final String HELP_2 = "-h"; //$NON-NLS-1$
51

52
    public static void main(String[] args) {
53
        if (args.length == 0) {
×
54
            usage();
×
55
            System.exit(0);
×
NEW
56
            return; // only to satisfy the compiler, never returns
×
57
        }
58

59
        Map<String, String> arguments = parseCommandLine(args);
×
60

61
        if (arguments.containsKey(HELP_1)) {
×
62
            usage();
×
63
            System.exit(0);
×
NEW
64
            return; // only to satisfy the compiler, never returns
×
65
        }
66

67
        if (!arguments.containsKey(CONFIG_FILE)) {
×
68
            writeLine(getString("RuntimeError.0")); //$NON-NLS-1$
×
69
            return;
×
70
        }
71

72
        List<String> warnings = new ArrayList<>();
×
73

74
        String configfile = arguments.get(CONFIG_FILE);
×
75
        Path configurationFile = Path.of(configfile);
×
76
        if (Files.notExists(configurationFile)) {
×
77
            writeLine(getString("RuntimeError.1", configfile)); //$NON-NLS-1$
×
78
            return;
×
79
        }
80

81
        Set<String> fullyQualifiedTables = StringUtility.tokenize(arguments.get(TABLES));
×
82

83
        Set<String> contexts = StringUtility.tokenize(arguments.get(CONTEXT_IDS));
×
84

85
        try {
86
            ConfigurationParser cp = new ConfigurationParser();
×
87
            Configuration config = cp.parseConfiguration(configurationFile.toFile());
×
88
            warnings.addAll(cp.getWarnings());
×
89
            boolean overwriteEnabled = arguments.containsKey(OVERWRITE);
×
90
            boolean javaMergeEnabled = arguments.containsKey(JAVA_MERGE_ENABLED);
×
91

92
            ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
×
93
                    : null;
×
94

95
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator.Builder()
×
96
                    .withConfiguration(config)
×
97
                    .withShellCallback(new DefaultShellCallback())
×
98
                    .withProgressCallback(progressCallback)
×
99
                    .withContextIds(contexts)
×
100
                    .withFullyQualifiedTableNames(fullyQualifiedTables)
×
101
                    .withJavaFileMergeEnabled(javaMergeEnabled)
×
102
                    .withOverwriteEnabled(overwriteEnabled)
×
103
                    .build();
×
104

105
            warnings.addAll(myBatisGenerator.generateAndWrite());
×
106
        } catch (XMLParserException e) {
×
107
            writeLine(getString("Progress.3")); //$NON-NLS-1$
×
108
            writeLine();
×
109
            for (String error : e.getErrors()) {
×
110
                writeLine(error);
×
111
            }
×
UNCOV
112
            return;
×
113
        } catch (SQLException | IOException e) {
×
114
            e.printStackTrace(System.out);
×
115
            return;
×
116
        } catch (InvalidConfigurationException e) {
×
117
            writeLine(getString("Progress.16")); //$NON-NLS-1$
×
118
            for (String error : e.getErrors()) {
×
119
                writeLine(error);
×
120
            }
×
121
            return;
×
122
        } catch (InterruptedException e) {
×
123
            Thread.currentThread().interrupt();
×
124
        }
×
125

126
        for (String warning : warnings) {
×
127
            writeLine(warning);
×
128
        }
×
129

130
        if (warnings.isEmpty()) {
×
131
            writeLine(getString("Progress.4")); //$NON-NLS-1$
×
132
        } else {
133
            writeLine();
×
134
            writeLine(getString("Progress.5")); //$NON-NLS-1$
×
135
        }
136
    }
×
137

138
    private static void usage() {
139
        writeLine(getString("Usage")); //$NON-NLS-1$
×
140
    }
×
141

142
    private static void writeLine(String message) {
143
        System.out.println(message);
×
144
    }
×
145

146
    private static void writeLine() {
147
        System.out.println();
×
148
    }
×
149

150
    private static Map<String, String> parseCommandLine(String[] args) {
151
        List<String> errors = new ArrayList<>();
×
152
        Map<String, String> arguments = new HashMap<>();
×
153

154
        for (int i = 0; i < args.length; i++) {
×
155
            if (CONFIG_FILE.equalsIgnoreCase(args[i])) {
×
156
                if ((i + 1) < args.length) {
×
157
                    arguments.put(CONFIG_FILE, args[i + 1]);
×
158
                } else {
NEW
159
                    errors.add(getString("RuntimeError.19", CONFIG_FILE)); //$NON-NLS-1$
×
160
                }
161
                i++;
×
162
            } else if (OVERWRITE.equalsIgnoreCase(args[i])) {
×
163
                arguments.put(OVERWRITE, "Y"); //$NON-NLS-1$
×
164
            } else if (VERBOSE.equalsIgnoreCase(args[i])) {
×
165
                arguments.put(VERBOSE, "Y"); //$NON-NLS-1$
×
166
            } else if (JAVA_MERGE_ENABLED.equalsIgnoreCase(args[i])) {
×
167
                arguments.put(JAVA_MERGE_ENABLED, "Y"); //$NON-NLS-1$
×
168
            } else if (HELP_1.equalsIgnoreCase(args[i])) {
×
169
                arguments.put(HELP_1, "Y"); //$NON-NLS-1$
×
170
            } else if (HELP_2.equalsIgnoreCase(args[i])) {
×
171
                // put HELP_1 in the map here too - so we only
172
                // have to check for one entry in the mainline
173
                arguments.put(HELP_1, "Y"); //$NON-NLS-1$
×
174
            } else if (CONTEXT_IDS.equalsIgnoreCase(args[i])) {
×
175
                if ((i + 1) < args.length) {
×
176
                    arguments.put(CONTEXT_IDS, args[i + 1]);
×
177
                } else {
NEW
178
                    errors.add(getString("RuntimeError.19", CONTEXT_IDS)); //$NON-NLS-1$
×
179
                }
180
                i++;
×
181
            } else if (TABLES.equalsIgnoreCase(args[i])) {
×
182
                if ((i + 1) < args.length) {
×
183
                    arguments.put(TABLES, args[i + 1]);
×
184
                } else {
185
                    errors.add(getString("RuntimeError.19", TABLES)); //$NON-NLS-1$
×
186
                }
187
                i++;
×
188
            } else {
189
                errors.add(getString("RuntimeError.20", args[i])); //$NON-NLS-1$
×
190
            }
191
        }
192

193
        if (!errors.isEmpty()) {
×
194
            for (String error : errors) {
×
195
                writeLine(error);
×
196
            }
×
197

198
            System.exit(-1);
×
199
        }
200

201
        return arguments;
×
202
    }
203
}
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

© 2026 Coveralls, Inc