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

mybatis / generator / 2026

08 Feb 2026 06:44PM UTC coverage: 89.941% (-0.02%) from 89.961%
2026

Pull #1443

github

web-flow
Merge 6b288d3fa into 4e917b771
Pull Request #1443: Remove the Eclipse based Java file merger in favor of the merger in the core library

2295 of 3073 branches covered (74.68%)

66 of 96 new or added lines in 8 files covered. (68.75%)

4 existing lines in 3 files now uncovered.

11641 of 12943 relevant lines covered (89.94%)

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/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
 *     <li>
79
 *         "javaMergeEnabled" - if true, then existing Java files will be merged. if
80
 *         false (default), then existing Java files will be untouched and the generator
81
 *         will write new Java files with a unique name
82
 *     </li>
83
 * </ul>
84
 *
85
 *
86
 * @author Jeff Butler
87
 */
88
public class GeneratorAntTask extends Task {
×
89

90
    private @Nullable String configfile;
91
    private boolean overwrite;
92
    private @Nullable PropertySet propertyset;
93
    private boolean verbose;
94
    private @Nullable String contextIds;
95
    private @Nullable String fullyQualifiedTableNames;
96
    private boolean javaMergeEnabled;
97

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

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

108
            ConfigurationParser cp = new ConfigurationParser(p);
×
109
            Configuration config = cp.parseConfiguration(configurationFile);
×
110
            warnings.addAll(cp.getWarnings());
×
111

UNCOV
112
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator.Builder()
×
113
                    .withConfiguration(config)
×
NEW
114
                    .withShellCallback(new DefaultShellCallback())
×
115
                    .withProgressCallback(new AntProgressCallback(this, verbose))
×
116
                    .withContextIds(contexts)
×
117
                    .withFullyQualifiedTableNames(fullyQualifiedTables)
×
NEW
118
                    .withJavaFileMergeEnabled(javaMergeEnabled)
×
NEW
119
                    .withOverwriteEnabled(overwrite)
×
UNCOV
120
                    .build();
×
121

122
            warnings.addAll(myBatisGenerator.generateAndWrite());
×
123
        } catch (XMLParserException | InvalidConfigurationException e) {
×
124
            for (String error : e.getErrors()) {
×
125
                log(error, Project.MSG_ERR);
×
126
            }
×
127

128
            throw new BuildException(e.getMessage());
×
129
        } catch (SQLException | IOException e) {
×
130
            throw new BuildException(e.getMessage());
×
131
        } catch (InterruptedException e) {
×
132
            Thread.currentThread().interrupt();
×
133
        } catch (Exception e) {
×
134
            log(e, Project.MSG_ERR);
×
135
            throw new BuildException(e.getMessage());
×
136
        }
×
137

138
        for (String error : warnings) {
×
139
            log(error, Project.MSG_WARN);
×
140
        }
×
141
    }
×
142

143
    private Set<String> calculateContexts() {
144
        Set<String> contexts = new HashSet<>();
×
145
        if (stringHasValue(contextIds)) {
×
146
            StringTokenizer st = new StringTokenizer(contextIds, ","); //$NON-NLS-1$
×
147
            while (st.hasMoreTokens()) {
×
148
                String s = st.nextToken().trim();
×
149
                if (!s.isEmpty()) {
×
150
                    contexts.add(s);
×
151
                }
152
            }
×
153
        }
154
        return contexts;
×
155
    }
156

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

171
    private File calculateConfigurationFile() {
172
        if (!stringHasValue(configfile)) {
×
173
            throw new BuildException(getString("RuntimeError.0")); //$NON-NLS-1$
×
174
        }
175

176

177
        Path configurationFile = Path.of(configfile);
×
178
        if (Files.notExists(configurationFile)) {
×
179
            throw new BuildException(getString("RuntimeError.1", configfile)); //$NON-NLS-1$
×
180
        }
181
        return configurationFile.toFile();
×
182
    }
183

184
    public @Nullable String getConfigfile() {
185
        return configfile;
×
186
    }
187

188
    public void setConfigfile(String configfile) {
189
        this.configfile = configfile;
×
190
    }
×
191

192
    public boolean isOverwrite() {
193
        return overwrite;
×
194
    }
195

196
    public void setOverwrite(boolean overwrite) {
197
        this.overwrite = overwrite;
×
198
    }
×
199

200
    public PropertySet createPropertyset() {
201
        if (propertyset == null) {
×
202
            propertyset = new PropertySet();
×
203
        }
204

205
        return propertyset;
×
206
    }
207

208
    public boolean isVerbose() {
209
        return verbose;
×
210
    }
211

212
    public void setVerbose(boolean verbose) {
213
        this.verbose = verbose;
×
214
    }
×
215

216
    public @Nullable String getContextIds() {
217
        return contextIds;
×
218
    }
219

220
    public void setContextIds(String contextIds) {
221
        this.contextIds = contextIds;
×
222
    }
×
223

224
    public @Nullable String getFullyQualifiedTableNames() {
225
        return fullyQualifiedTableNames;
×
226
    }
227

228
    public void setFullyQualifiedTableNames(String fullyQualifiedTableNames) {
229
        this.fullyQualifiedTableNames = fullyQualifiedTableNames;
×
230
    }
×
231

232
    public boolean isJavaMergeEnabled() {
233
        return javaMergeEnabled;
×
234
    }
235

236
    public void setJavaMergeEnabled(boolean javaMergeEnabled) {
237
        this.javaMergeEnabled = javaMergeEnabled;
×
238
    }
×
239
}
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