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

mybatis / generator / 1990

28 Jan 2026 08:49PM UTC coverage: 89.789% (-0.2%) from 89.986%
1990

push

github

web-flow
Merge pull request #1344 from DanielLiu1123/feat-merge-mapper

Support Java file merge

2251 of 3031 branches covered (74.27%)

61 of 99 new or added lines in 4 files covered. (61.62%)

11519 of 12829 relevant lines covered (89.79%)

0.9 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.JavaMergingShellCallback;
36
import org.mybatis.generator.internal.util.StringUtility;
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 JAVA_MERGE_ENABLED = "-javaMergeEnabled";
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 {
87
            ConfigurationParser cp = new ConfigurationParser();
×
88
            Configuration config = cp.parseConfiguration(configurationFile.toFile());
×
89
            warnings.addAll(cp.getWarnings());
×
90
            ShellCallback shellCallback;
NEW
91
            if (arguments.containsKey(JAVA_MERGE_ENABLED)) {
×
NEW
92
                shellCallback = new JavaMergingShellCallback(arguments.containsKey(OVERWRITE));
×
93
            } else {
NEW
94
                shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
×
95
            }
96

97
            ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
×
98
                    : null;
×
99

100
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator.Builder()
×
101
                    .withConfiguration(config)
×
102
                    .withShellCallback(shellCallback)
×
103
                    .withProgressCallback(progressCallback)
×
104
                    .withContextIds(contexts)
×
105
                    .withFullyQualifiedTableNames(fullyQualifiedTables)
×
106
                    .build();
×
107

108
            warnings.addAll(myBatisGenerator.generateAndWrite());
×
109
        } catch (XMLParserException e) {
×
110
            writeLine(getString("Progress.3")); //$NON-NLS-1$
×
111
            writeLine();
×
112
            for (String error : e.getErrors()) {
×
113
                writeLine(error);
×
114
            }
×
115

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

130
        for (String warning : warnings) {
×
131
            writeLine(warning);
×
132
        }
×
133

134
        if (warnings.isEmpty()) {
×
135
            writeLine(getString("Progress.4")); //$NON-NLS-1$
×
136
        } else {
137
            writeLine();
×
138
            writeLine(getString("Progress.5")); //$NON-NLS-1$
×
139
        }
140
    }
×
141

142
    private static void usage() {
143
        writeLine(getString("Usage")); //$NON-NLS-1$
×
144
    }
×
145

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

150
    private static void writeLine() {
151
        System.out.println();
×
152
    }
×
153

154
    private static Map<String, String> parseCommandLine(String[] args) {
155
        List<String> errors = new ArrayList<>();
×
156
        Map<String, String> arguments = new HashMap<>();
×
157

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

199
        if (!errors.isEmpty()) {
×
200
            for (String error : errors) {
×
201
                writeLine(error);
×
202
            }
×
203

204
            System.exit(-1);
×
205
        }
206

207
        return arguments;
×
208
    }
209
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc