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

mybatis / generator / 1706

08 Jun 2025 12:52AM CUT coverage: 88.307%. Remained the same
1706

push

github

web-flow
Merge pull request #1325 from mybatis/renovate/junit5-monorepo

Update junit5 monorepo to v5.13.1

2518 of 3410 branches covered (73.84%)

11192 of 12674 relevant lines covered (88.31%)

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.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 {
87
            ConfigurationParser cp = new ConfigurationParser(warnings);
×
88
            Configuration config = cp.parseConfiguration(configurationFile.toFile());
×
89

90
            DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
×
91

92
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
×
93

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

97
            myBatisGenerator.generate(progressCallback, contexts, fullyQualifiedTables);
×
98

99
        } catch (XMLParserException e) {
×
100
            writeLine(getString("Progress.3")); //$NON-NLS-1$
×
101
            writeLine();
×
102
            for (String error : e.getErrors()) {
×
103
                writeLine(error);
×
104
            }
×
105

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

120
        for (String warning : warnings) {
×
121
            writeLine(warning);
×
122
        }
×
123

124
        if (warnings.isEmpty()) {
×
125
            writeLine(getString("Progress.4")); //$NON-NLS-1$
×
126
        } else {
127
            writeLine();
×
128
            writeLine(getString("Progress.5")); //$NON-NLS-1$
×
129
        }
130
    }
×
131

132
    private static void usage() {
133
        writeLine(getString("Usage")); //$NON-NLS-1$
×
134
    }
×
135

136
    private static void writeLine(String message) {
137
        System.out.println(message);
×
138
    }
×
139

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

144
    private static Map<String, String> parseCommandLine(String[] args) {
145
        List<String> errors = new ArrayList<>();
×
146
        Map<String, String> arguments = new HashMap<>();
×
147

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

189
        if (!errors.isEmpty()) {
×
190
            for (String error : errors) {
×
191
                writeLine(error);
×
192
            }
×
193

194
            System.exit(-1);
×
195
        }
196

197
        return arguments;
×
198
    }
199
}
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