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

mybatis / generator / 1958

18 Jan 2026 03:42PM UTC coverage: 88.883% (-0.07%) from 88.948%
1958

push

github

web-flow
Merge pull request #1419 from jeffgbutler/api-refactoring

Modernize Some Public API Classes

2333 of 3154 branches covered (73.97%)

55 of 100 new or added lines in 10 files covered. (55.0%)

4 existing lines in 3 files now uncovered.

11585 of 13034 relevant lines covered (88.88%)

0.89 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
import org.mybatis.generator.logging.LogFactory;
37

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

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

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

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

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

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

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

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

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

86
        try {
NEW
87
            ConfigurationParser cp = new ConfigurationParser();
×
88
            Configuration config = cp.parseConfiguration(configurationFile.toFile());
×
NEW
89
            warnings.addAll(cp.getWarnings());
×
90
            DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
×
91

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

NEW
95
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator.Builder()
×
NEW
96
                    .withConfiguration(config)
×
NEW
97
                    .withShellCallback(shellCallback)
×
NEW
98
                    .withProgressCallback(progressCallback)
×
NEW
99
                    .withContextIds(contexts)
×
NEW
100
                    .withFullyQualifiedTableNames(fullyQualifiedTables)
×
NEW
101
                    .build();
×
102

NEW
103
            warnings.addAll(myBatisGenerator.generateAndWrite());
×
104
        } catch (XMLParserException e) {
×
105
            writeLine(getString("Progress.3")); //$NON-NLS-1$
×
106
            writeLine();
×
107
            for (String error : e.getErrors()) {
×
108
                writeLine(error);
×
109
            }
×
110

111
            return;
×
112
        } catch (SQLException | IOException e) {
×
113
            e.printStackTrace(System.out);
×
114
            return;
×
115
        } catch (InvalidConfigurationException e) {
×
116
            writeLine(getString("Progress.16")); //$NON-NLS-1$
×
117
            for (String error : e.getErrors()) {
×
118
                writeLine(error);
×
119
            }
×
120
            return;
×
121
        } catch (InterruptedException e) {
×
122
            Thread.currentThread().interrupt();
×
123
        }
×
124

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

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

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

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

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

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

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

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

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

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