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

mybatis / generator / 1646

21 Apr 2025 10:17PM UTC coverage: 88.157% (-0.2%) from 88.328%
1646

push

github

hazendaz
[ci] Run auto formatting

2518 of 3412 branches covered (73.8%)

994 of 1117 new or added lines in 164 files covered. (88.99%)

23 existing lines in 12 files now uncovered.

10578 of 11999 relevant lines covered (88.16%)

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.File;
21
import java.io.IOException;
22
import java.sql.SQLException;
23
import java.util.ArrayList;
24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
27
import java.util.Set;
28

29
import org.mybatis.generator.config.Configuration;
30
import org.mybatis.generator.config.xml.ConfigurationParser;
31
import org.mybatis.generator.exception.InvalidConfigurationException;
32
import org.mybatis.generator.exception.XMLParserException;
33
import org.mybatis.generator.internal.DefaultShellCallback;
34
import org.mybatis.generator.internal.util.StringUtility;
35
import org.mybatis.generator.logging.LogFactory;
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 FORCE_JAVA_LOGGING = "-forceJavaLogging"; //$NON-NLS-1$
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);
×
56
            return; // only to satisfy 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);
×
64
            return; // only to satisfy 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
        File configurationFile = new File(configfile);
×
76
        if (!configurationFile.exists()) {
×
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(warnings);
×
87
            Configuration config = cp.parseConfiguration(configurationFile);
×
88

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

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

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

95
            myBatisGenerator.generate(progressCallback, contexts, fullyQualifiedTables);
×
96

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

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

118
        for (String warning : warnings) {
×
119
            writeLine(warning);
×
120
        }
×
121

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

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

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

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

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

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

185
        if (!errors.isEmpty()) {
×
186
            for (String error : errors) {
×
187
                writeLine(error);
×
188
            }
×
189

190
            System.exit(-1);
×
191
        }
192

193
        return arguments;
×
194
    }
195
}
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