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

mybatis / generator / 1722

06 Jul 2025 06:38PM UTC coverage: 88.195% (-0.1%) from 88.307%
1722

push

github

hazendaz
Setup subproject for new Java File Merger

2518 of 3420 branches covered (73.63%)

0 of 16 new or added lines in 3 files covered. (0.0%)

73 existing lines in 3 files now uncovered.

11192 of 12690 relevant lines covered (88.2%)

0.88 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-2025 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.ObjectFactory;
36
import org.mybatis.generator.internal.util.StringUtility;
37
import org.mybatis.generator.logging.LogFactory;
38

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

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

62
        Map<String, String> arguments = parseCommandLine(args);
×
63

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

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

75
        List<String> warnings = new ArrayList<>();
×
76

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

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

UNCOV
86
        Set<String> contexts = StringUtility.tokenize(arguments.get(CONTEXT_IDS));
×
87

88
        try {
UNCOV
89
            ConfigurationParser cp = new ConfigurationParser(warnings);
×
90
            Configuration config = cp.parseConfiguration(configurationFile.toFile());
×
91

92
            DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
×
NEW
UNCOV
93
            if (arguments.containsKey(JAVA_FILE_MERGER)) {
×
NEW
94
                JavaFileMerger javaFileMerger = ObjectFactory.createJavaFileMerger(arguments.get(JAVA_FILE_MERGER));
×
NEW
95
                shellCallback.setJavaFileMerger(javaFileMerger);
×
96
            }
97

UNCOV
98
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
×
99

100
            ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
×
101
                    : null;
×
102

103
            myBatisGenerator.generate(progressCallback, contexts, fullyQualifiedTables);
×
104

UNCOV
105
        } catch (XMLParserException e) {
×
106
            writeLine(getString("Progress.3")); //$NON-NLS-1$
×
107
            writeLine();
×
108
            for (String error : e.getErrors()) {
×
109
                writeLine(error);
×
110
            }
×
111

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()) {
×
UNCOV
119
                writeLine(error);
×
120
            }
×
121
            return;
×
122
        } catch (InterruptedException e) {
×
UNCOV
123
            Thread.currentThread().interrupt();
×
124
        }
×
125

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

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

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

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

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

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

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

UNCOV
202
        if (!errors.isEmpty()) {
×
UNCOV
203
            for (String error : errors) {
×
UNCOV
204
                writeLine(error);
×
UNCOV
205
            }
×
206

UNCOV
207
            System.exit(-1);
×
208
        }
209

UNCOV
210
        return arguments;
×
211
    }
212
}
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