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

mybatis / generator / 1990

28 Jan 2026 08:49PM UTC coverage: 89.789% (-0.2%) from 89.986%
1990

push

github

web-flow
Merge pull request #1344 from DanielLiu1123/feat-merge-mapper

Support Java file merge

2251 of 3031 branches covered (74.27%)

61 of 99 new or added lines in 4 files covered. (61.62%)

11519 of 12829 relevant lines covered (89.79%)

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.api.ShellCallback;
40
import org.mybatis.generator.config.Configuration;
41
import org.mybatis.generator.config.xml.ConfigurationParser;
42
import org.mybatis.generator.exception.InvalidConfigurationException;
43
import org.mybatis.generator.exception.XMLParserException;
44
import org.mybatis.generator.internal.DefaultShellCallback;
45
import org.mybatis.generator.internal.JavaMergingShellCallback;
46

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

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

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

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

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

114
            ShellCallback callback;
NEW
115
            if (javaMergeEnabled) {
×
NEW
116
                callback = new JavaMergingShellCallback(overwrite);
×
117
            } else {
NEW
118
                callback = new DefaultShellCallback(overwrite);
×
119
            }
120

121
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator.Builder()
×
122
                    .withConfiguration(config)
×
123
                    .withShellCallback(callback)
×
124
                    .withProgressCallback(new AntProgressCallback(this, verbose))
×
125
                    .withContextIds(contexts)
×
126
                    .withFullyQualifiedTableNames(fullyQualifiedTables)
×
127
                    .build();
×
128

129
            warnings.addAll(myBatisGenerator.generateAndWrite());
×
130
        } catch (XMLParserException | InvalidConfigurationException e) {
×
131
            for (String error : e.getErrors()) {
×
132
                log(error, Project.MSG_ERR);
×
133
            }
×
134

135
            throw new BuildException(e.getMessage());
×
136
        } catch (SQLException | IOException e) {
×
137
            throw new BuildException(e.getMessage());
×
138
        } catch (InterruptedException e) {
×
139
            Thread.currentThread().interrupt();
×
140
        } catch (Exception e) {
×
141
            log(e, Project.MSG_ERR);
×
142
            throw new BuildException(e.getMessage());
×
143
        }
×
144

145
        for (String error : warnings) {
×
146
            log(error, Project.MSG_WARN);
×
147
        }
×
148
    }
×
149

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

164
    private Set<String> calculateTables() {
165
        Set<String> fullyqualifiedTables = new HashSet<>();
×
166
        if (stringHasValue(fullyQualifiedTableNames)) {
×
167
            StringTokenizer st = new StringTokenizer(fullyQualifiedTableNames, ","); //$NON-NLS-1$
×
168
            while (st.hasMoreTokens()) {
×
169
                String s = st.nextToken().trim();
×
170
                if (!s.isEmpty()) {
×
171
                    fullyqualifiedTables.add(s);
×
172
                }
173
            }
×
174
        }
175
        return fullyqualifiedTables;
×
176
    }
177

178
    private File calculateConfigurationFile() {
179
        if (!stringHasValue(configfile)) {
×
180
            throw new BuildException(getString("RuntimeError.0")); //$NON-NLS-1$
×
181
        }
182

183

184
        Path configurationFile = Path.of(configfile);
×
185
        if (Files.notExists(configurationFile)) {
×
186
            throw new BuildException(getString("RuntimeError.1", configfile)); //$NON-NLS-1$
×
187
        }
188
        return configurationFile.toFile();
×
189
    }
190

191
    public @Nullable String getConfigfile() {
192
        return configfile;
×
193
    }
194

195
    public void setConfigfile(String configfile) {
196
        this.configfile = configfile;
×
197
    }
×
198

199
    public boolean isOverwrite() {
200
        return overwrite;
×
201
    }
202

203
    public void setOverwrite(boolean overwrite) {
204
        this.overwrite = overwrite;
×
205
    }
×
206

207
    public PropertySet createPropertyset() {
208
        if (propertyset == null) {
×
209
            propertyset = new PropertySet();
×
210
        }
211

212
        return propertyset;
×
213
    }
214

215
    public boolean isVerbose() {
216
        return verbose;
×
217
    }
218

219
    public void setVerbose(boolean verbose) {
220
        this.verbose = verbose;
×
221
    }
×
222

223
    public @Nullable String getContextIds() {
224
        return contextIds;
×
225
    }
226

227
    public void setContextIds(String contextIds) {
228
        this.contextIds = contextIds;
×
229
    }
×
230

231
    public @Nullable String getFullyQualifiedTableNames() {
232
        return fullyQualifiedTableNames;
×
233
    }
234

235
    public void setFullyQualifiedTableNames(String fullyQualifiedTableNames) {
236
        this.fullyQualifiedTableNames = fullyQualifiedTableNames;
×
237
    }
×
238

239
    public boolean isJavaMergeEnabled() {
NEW
240
        return javaMergeEnabled;
×
241
    }
242

243
    public void setJavaMergeEnabled(boolean javaMergeEnabled) {
NEW
244
        this.javaMergeEnabled = javaMergeEnabled;
×
NEW
245
    }
×
246
}
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