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

mybatis / generator / 1942

12 Jan 2026 05:01PM UTC coverage: 88.75% (+0.4%) from 88.365%
1942

push

github

web-flow
Merge pull request #1412 from jeffgbutler/jspecify

Adopt JSpecify

2331 of 3162 branches covered (73.72%)

1800 of 1949 new or added lines in 202 files covered. (92.36%)

18 existing lines in 10 files now uncovered.

11384 of 12827 relevant lines covered (88.75%)

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/ant/GeneratorAntTask.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.ant;
17

18
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19
import static org.mybatis.generator.internal.util.messages.Messages.getString;
20

21
import java.io.File;
22
import java.io.IOException;
23
import java.nio.file.Files;
24
import java.nio.file.Path;
25
import java.sql.SQLException;
26
import java.util.ArrayList;
27
import java.util.HashSet;
28
import java.util.List;
29
import java.util.Properties;
30
import java.util.Set;
31
import java.util.StringTokenizer;
32

33
import org.apache.tools.ant.BuildException;
34
import org.apache.tools.ant.Project;
35
import org.apache.tools.ant.Task;
36
import org.apache.tools.ant.types.PropertySet;
37
import org.jspecify.annotations.Nullable;
38
import org.mybatis.generator.api.MyBatisGenerator;
39
import org.mybatis.generator.config.Configuration;
40
import org.mybatis.generator.config.xml.ConfigurationParser;
41
import org.mybatis.generator.exception.InvalidConfigurationException;
42
import org.mybatis.generator.exception.XMLParserException;
43
import org.mybatis.generator.internal.DefaultShellCallback;
44

45
/**
46
 * This is an Ant task that will run the generator. The following is a sample
47
 * Ant script that shows how to run the generator from Ant:
48
 *
49
 * <pre>
50
 *  &lt;project default="genfiles" basedir="."&gt;
51
 *    &lt;property name="generated.source.dir" value="${basedir}" /&gt;
52
 *    &lt;target name="genfiles" description="Generate the files"&gt;
53
 *      &lt;taskdef name="mbgenerator"
54
 *               classname="org.mybatis.generator.ant.GeneratorAntTask"
55
 *               classpath="mybatis-generator-core-x.x.x.jar" /&gt;
56
 *      &lt;mbgenerator overwrite="true" configfile="generatorConfig.xml" verbose="false" &gt;
57
 *        &lt;propertyset&gt;
58
 *          &lt;propertyref name="generated.source.dir"/&gt;
59
 *        &lt;/propertyset&gt;
60
 *      &lt;/mbgenerator&gt;
61
 *    &lt;/target&gt;
62
 *  &lt;/project&gt;
63
 * </pre>
64
 *
65
 * <p>The task requires that the attribute "configFile" be set to an existing XML
66
 * configuration file.
67
 *
68
 * <p>The task supports these optional attributes:
69
 * <ul>
70
 * <li>"overwrite" - if true, then existing Java files will be overwritten. if
71
 * false (default), then existing Java files will be untouched and the generator
72
 * will write new Java files with a unique name</li>
73
 * <li>"verbose" - if true, then the generator will log progress messages to the
74
 * Ant log. Default is false</li>
75
 * <li>"contextIds" - a comma delimited list of contaxtIds to use for this run</li>
76
 * <li>"fullyQualifiedTableNames" - a comma-delimited list of fully qualified
77
 * table names to use for this run</li>
78
 * </ul>
79
 *
80
 *
81
 * @author Jeff Butler
82
 */
83
public class GeneratorAntTask extends Task {
84

85
    private @Nullable String configfile;
86
    private boolean overwrite;
87
    private @Nullable PropertySet propertyset;
88
    private boolean verbose;
89
    private @Nullable String contextIds;
90
    private @Nullable String fullyQualifiedTableNames;
91

92
    public GeneratorAntTask() {
93
        super();
×
94
    }
×
95

96
    @Override
97
    public void execute() {
98
        File configurationFile = calculateConfigurationFile();
×
NEW
99
        Set<String> fullyQualifiedTables = calculateTables();
×
100
        Set<String> contexts = calculateContexts();
×
101

102
        List<String> warnings = new ArrayList<>();
×
103
        try {
104
            Properties p = propertyset == null ? null : propertyset
×
105
                    .getProperties();
×
106

107
            ConfigurationParser cp = new ConfigurationParser(p, warnings);
×
108
            Configuration config = cp.parseConfiguration(configurationFile);
×
109

110
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
×
111

112
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
×
113

114
            myBatisGenerator.generate(new AntProgressCallback(this, verbose), contexts,
×
115
                    fullyQualifiedTables);
116

117
        } catch (XMLParserException | InvalidConfigurationException e) {
×
118
            for (String error : e.getErrors()) {
×
119
                log(error, Project.MSG_ERR);
×
120
            }
×
121

122
            throw new BuildException(e.getMessage());
×
123
        } catch (SQLException | IOException e) {
×
124
            throw new BuildException(e.getMessage());
×
125
        } catch (InterruptedException e) {
×
126
            Thread.currentThread().interrupt();
×
127
        } catch (Exception e) {
×
128
            log(e, Project.MSG_ERR);
×
129
            throw new BuildException(e.getMessage());
×
130
        }
×
131

132
        for (String error : warnings) {
×
133
            log(error, Project.MSG_WARN);
×
134
        }
×
135
    }
×
136

137
    private Set<String> calculateContexts() {
138
        Set<String> contexts = new HashSet<>();
×
139
        if (stringHasValue(contextIds)) {
×
140
            StringTokenizer st = new StringTokenizer(contextIds, ","); //$NON-NLS-1$
×
141
            while (st.hasMoreTokens()) {
×
142
                String s = st.nextToken().trim();
×
143
                if (!s.isEmpty()) {
×
144
                    contexts.add(s);
×
145
                }
146
            }
×
147
        }
148
        return contexts;
×
149
    }
150

151
    private Set<String> calculateTables() {
152
        Set<String> fullyqualifiedTables = new HashSet<>();
×
153
        if (stringHasValue(fullyQualifiedTableNames)) {
×
154
            StringTokenizer st = new StringTokenizer(fullyQualifiedTableNames,
×
155
                    ","); //$NON-NLS-1$
156
            while (st.hasMoreTokens()) {
×
157
                String s = st.nextToken().trim();
×
158
                if (!s.isEmpty()) {
×
159
                    fullyqualifiedTables.add(s);
×
160
                }
161
            }
×
162
        }
163
        return fullyqualifiedTables;
×
164
    }
165

166
    private File calculateConfigurationFile() {
167
        if (!stringHasValue(configfile)) {
×
168
            throw new BuildException(getString("RuntimeError.0")); //$NON-NLS-1$
×
169
        }
170

171

172
        Path configurationFile = Path.of(configfile);
×
173
        if (Files.notExists(configurationFile)) {
×
174
            throw new BuildException(getString(
×
175
                    "RuntimeError.1", configfile)); //$NON-NLS-1$
176
        }
177
        return configurationFile.toFile();
×
178
    }
179

180
    public @Nullable String getConfigfile() {
181
        return configfile;
×
182
    }
183

184
    public void setConfigfile(String configfile) {
185
        this.configfile = configfile;
×
186
    }
×
187

188
    public boolean isOverwrite() {
189
        return overwrite;
×
190
    }
191

192
    public void setOverwrite(boolean overwrite) {
193
        this.overwrite = overwrite;
×
194
    }
×
195

196
    public PropertySet createPropertyset() {
197
        if (propertyset == null) {
×
198
            propertyset = new PropertySet();
×
199
        }
200

201
        return propertyset;
×
202
    }
203

204
    public boolean isVerbose() {
205
        return verbose;
×
206
    }
207

208
    public void setVerbose(boolean verbose) {
209
        this.verbose = verbose;
×
210
    }
×
211

212
    public @Nullable String getContextIds() {
213
        return contextIds;
×
214
    }
215

216
    public void setContextIds(String contextIds) {
217
        this.contextIds = contextIds;
×
218
    }
×
219

220
    public @Nullable String getFullyQualifiedTableNames() {
221
        return fullyQualifiedTableNames;
×
222
    }
223

224
    public void setFullyQualifiedTableNames(String fullyQualifiedTableNames) {
225
        this.fullyQualifiedTableNames = fullyQualifiedTableNames;
×
226
    }
×
227
}
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