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

mybatis / generator / 1706

08 Jun 2025 12:52AM CUT coverage: 88.307%. Remained the same
1706

push

github

web-flow
Merge pull request #1325 from mybatis/renovate/junit5-monorepo

Update junit5 monorepo to v5.13.1

2518 of 3410 branches covered (73.84%)

11192 of 12674 relevant lines covered (88.31%)

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/ant/GeneratorAntTask.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.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.mybatis.generator.api.MyBatisGenerator;
38
import org.mybatis.generator.config.Configuration;
39
import org.mybatis.generator.config.xml.ConfigurationParser;
40
import org.mybatis.generator.exception.InvalidConfigurationException;
41
import org.mybatis.generator.exception.XMLParserException;
42
import org.mybatis.generator.internal.DefaultShellCallback;
43

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

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

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

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

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

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

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

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

113
            myBatisGenerator.generate(new AntProgressCallback(this, verbose), contexts,
×
114
                    fullyqualifiedTables);
115

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

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

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

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

150
    private Set<String> calculateTables() {
151
        Set<String> fullyqualifiedTables = new HashSet<>();
×
152
        if (stringHasValue(fullyQualifiedTableNames)) {
×
153
            StringTokenizer st = new StringTokenizer(fullyQualifiedTableNames,
×
154
                    ","); //$NON-NLS-1$
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)) {
×
173
            throw new BuildException(getString(
×
174
                    "RuntimeError.1", configfile)); //$NON-NLS-1$
175
        }
176
        return configurationFile.toFile();
×
177
    }
178

179
    public String getConfigfile() {
180
        return configfile;
×
181
    }
182

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

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

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

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

200
        return propertyset;
×
201
    }
202

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

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

211
    public String getContextIds() {
212
        return contextIds;
×
213
    }
214

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

219
    public String getFullyQualifiedTableNames() {
220
        return fullyQualifiedTableNames;
×
221
    }
222

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