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

mybatis / generator / 1623

13 Apr 2025 12:26AM CUT coverage: 88.328%. Remained the same
1623

push

github

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

Update junit5 monorepo to v5.12.2

2518 of 3412 branches covered (73.8%)

11192 of 12671 relevant lines covered (88.33%)

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
44
 * Ant script that shows how to run the 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
 *
62
 * <p>The task requires that the attribute "configFile" be set to an existing XML
63
 * configuration file.
64
 *
65
 * <p>The task supports these optional attributes:
66
 * <ul>
67
 * <li>"overwrite" - if true, then existing Java files will be overwritten. if
68
 * false (default), then existing Java files will be untouched and the generator
69
 * will write new Java files with a unique name</li>
70
 * <li>"verbose" - if true, then the generator will log progress messages to the
71
 * Ant log. Default is false</li>
72
 * <li>"contextIds" - a comma delimited list of contaxtIds to use for this run</li>
73
 * <li>"fullyQualifiedTableNames" - a comma-delimited list of fully qualified
74
 * table names to use for this run</li>
75
 * </ul>
76
 *
77
 *
78
 * @author Jeff Butler
79
 */
80
public class GeneratorAntTask extends Task {
81

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

89
    public GeneratorAntTask() {
90
        super();
×
91
    }
×
92

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

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

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

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

109
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
×
110

111
            myBatisGenerator.generate(new AntProgressCallback(this, verbose), contexts,
×
112
                    fullyqualifiedTables);
113

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

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

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

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

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

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

168

169
        File configurationFile = new File(configfile);
×
170
        if (!configurationFile.exists()) {
×
171
            throw new BuildException(getString(
×
172
                    "RuntimeError.1", configfile)); //$NON-NLS-1$
173
        }
174
        return configurationFile;
×
175
    }
176

177
    public String getConfigfile() {
178
        return configfile;
×
179
    }
180

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

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

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

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

198
        return propertyset;
×
199
    }
200

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

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

209
    public String getContextIds() {
210
        return contextIds;
×
211
    }
212

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

217
    public String getFullyQualifiedTableNames() {
218
        return fullyQualifiedTableNames;
×
219
    }
220

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