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

mybatis / generator / 1646

21 Apr 2025 10:17PM UTC coverage: 88.157% (-0.2%) from 88.328%
1646

push

github

hazendaz
[ci] Run auto formatting

2518 of 3412 branches covered (73.8%)

994 of 1117 new or added lines in 164 files covered. (88.99%)

23 existing lines in 12 files now uncovered.

10578 of 11999 relevant lines covered (88.16%)

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.sql.SQLException;
24
import java.util.ArrayList;
25
import java.util.HashSet;
26
import java.util.List;
27
import java.util.Properties;
28
import java.util.Set;
29
import java.util.StringTokenizer;
30

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

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

77
    private String configfile;
78
    private boolean overwrite;
79
    private PropertySet propertyset;
80
    private boolean verbose;
81
    private String contextIds;
82
    private String fullyQualifiedTableNames;
83

84
    public GeneratorAntTask() {
85
        super();
×
86
    }
×
87

88
    @Override
89
    public void execute() {
90
        File configurationFile = calculateConfigurationFile();
×
91
        Set<String> fullyqualifiedTables = calculateTables();
×
92
        Set<String> contexts = calculateContexts();
×
93

94
        List<String> warnings = new ArrayList<>();
×
95
        try {
NEW
96
            Properties p = propertyset == null ? null : propertyset.getProperties();
×
97

98
            ConfigurationParser cp = new ConfigurationParser(p, warnings);
×
99
            Configuration config = cp.parseConfiguration(configurationFile);
×
100

101
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
×
102

103
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
×
104

NEW
105
            myBatisGenerator.generate(new AntProgressCallback(this, verbose), contexts, fullyqualifiedTables);
×
106

107
        } catch (XMLParserException | InvalidConfigurationException e) {
×
108
            for (String error : e.getErrors()) {
×
109
                log(error, Project.MSG_ERR);
×
110
            }
×
111

112
            throw new BuildException(e.getMessage());
×
113
        } catch (SQLException | IOException e) {
×
114
            throw new BuildException(e.getMessage());
×
115
        } catch (InterruptedException e) {
×
116
            Thread.currentThread().interrupt();
×
117
        } catch (Exception e) {
×
118
            log(e, Project.MSG_ERR);
×
119
            throw new BuildException(e.getMessage());
×
120
        }
×
121

122
        for (String error : warnings) {
×
123
            log(error, Project.MSG_WARN);
×
124
        }
×
125
    }
×
126

127
    private Set<String> calculateContexts() {
128
        Set<String> contexts = new HashSet<>();
×
129
        if (stringHasValue(contextIds)) {
×
130
            StringTokenizer st = new StringTokenizer(contextIds, ","); //$NON-NLS-1$
×
131
            while (st.hasMoreTokens()) {
×
132
                String s = st.nextToken().trim();
×
133
                if (!s.isEmpty()) {
×
134
                    contexts.add(s);
×
135
                }
136
            }
×
137
        }
138
        return contexts;
×
139
    }
140

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

155
    private File calculateConfigurationFile() {
156
        if (!stringHasValue(configfile)) {
×
157
            throw new BuildException(getString("RuntimeError.0")); //$NON-NLS-1$
×
158
        }
159

UNCOV
160
        File configurationFile = new File(configfile);
×
161
        if (!configurationFile.exists()) {
×
NEW
162
            throw new BuildException(getString("RuntimeError.1", configfile)); //$NON-NLS-1$
×
163
        }
164
        return configurationFile;
×
165
    }
166

167
    public String getConfigfile() {
168
        return configfile;
×
169
    }
170

171
    public void setConfigfile(String configfile) {
172
        this.configfile = configfile;
×
173
    }
×
174

175
    public boolean isOverwrite() {
176
        return overwrite;
×
177
    }
178

179
    public void setOverwrite(boolean overwrite) {
180
        this.overwrite = overwrite;
×
181
    }
×
182

183
    public PropertySet createPropertyset() {
184
        if (propertyset == null) {
×
185
            propertyset = new PropertySet();
×
186
        }
187

188
        return propertyset;
×
189
    }
190

191
    public boolean isVerbose() {
192
        return verbose;
×
193
    }
194

195
    public void setVerbose(boolean verbose) {
196
        this.verbose = verbose;
×
197
    }
×
198

199
    public String getContextIds() {
200
        return contextIds;
×
201
    }
202

203
    public void setContextIds(String contextIds) {
204
        this.contextIds = contextIds;
×
205
    }
×
206

207
    public String getFullyQualifiedTableNames() {
208
        return fullyQualifiedTableNames;
×
209
    }
210

211
    public void setFullyQualifiedTableNames(String fullyQualifiedTableNames) {
212
        this.fullyQualifiedTableNames = fullyQualifiedTableNames;
×
213
    }
×
214
}
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