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

mybatis / generator / 1958

18 Jan 2026 03:42PM UTC coverage: 88.883% (-0.07%) from 88.948%
1958

push

github

web-flow
Merge pull request #1419 from jeffgbutler/api-refactoring

Modernize Some Public API Classes

2333 of 3154 branches covered (73.97%)

55 of 100 new or added lines in 10 files covered. (55.0%)

4 existing lines in 3 files now uncovered.

11585 of 13034 relevant lines covered (88.88%)

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
 */
UNCOV
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
    @Override
93
    public void execute() {
94
        File configurationFile = calculateConfigurationFile();
×
95
        Set<String> fullyQualifiedTables = calculateTables();
×
96
        Set<String> contexts = calculateContexts();
×
97

98
        List<String> warnings = new ArrayList<>();
×
99
        try {
NEW
100
            Properties p = propertyset == null ? null : propertyset.getProperties();
×
101

NEW
102
            ConfigurationParser cp = new ConfigurationParser(p);
×
103
            Configuration config = cp.parseConfiguration(configurationFile);
×
NEW
104
            warnings.addAll(cp.getWarnings());
×
105

106
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
×
107

NEW
108
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator.Builder()
×
NEW
109
                    .withConfiguration(config)
×
NEW
110
                    .withShellCallback(callback)
×
NEW
111
                    .withProgressCallback(new AntProgressCallback(this, verbose))
×
NEW
112
                    .withContextIds(contexts)
×
NEW
113
                    .withFullyQualifiedTableNames(fullyQualifiedTables)
×
NEW
114
                    .build();
×
115

NEW
116
            warnings.addAll(myBatisGenerator.generateAndWrite());
×
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)) {
×
NEW
154
            StringTokenizer st = new StringTokenizer(fullyQualifiedTableNames, ","); //$NON-NLS-1$
×
UNCOV
155
            while (st.hasMoreTokens()) {
×
156
                String s = st.nextToken().trim();
×
157
                if (!s.isEmpty()) {
×
158
                    fullyqualifiedTables.add(s);
×
159
                }
160
            }
×
161
        }
162
        return fullyqualifiedTables;
×
163
    }
164

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

170

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

178
    public @Nullable String getConfigfile() {
179
        return configfile;
×
180
    }
181

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

186
    public boolean isOverwrite() {
187
        return overwrite;
×
188
    }
189

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

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

199
        return propertyset;
×
200
    }
201

202
    public boolean isVerbose() {
203
        return verbose;
×
204
    }
205

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

210
    public @Nullable String getContextIds() {
211
        return contextIds;
×
212
    }
213

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

218
    public @Nullable String getFullyQualifiedTableNames() {
219
        return fullyQualifiedTableNames;
×
220
    }
221

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