use of org.mybatis.generator.exception.InvalidConfigurationException in project jeesuite-libs by vakinge.
the class MyBatisGeneratorTool method main.
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// src的一级目录下
String genCfg = "/generatorConfig.xml";
File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.
the class GeneratorAntTask method execute.
/*
* (non-Javadoc)
*
* @see org.apache.tools.ant.Task#execute()
*/
@Override
public void execute() throws BuildException {
setLoggingImplementation();
if (!StringUtility.stringHasValue(configfile)) {
throw new BuildException("configfile is a required parameter");
}
List<String> warnings = new ArrayList<>();
File configurationFile = new File(configfile);
if (!configurationFile.exists()) {
throw new BuildException("configfile " + configfile + " does not exist");
}
Set<String> fullyqualifiedTables = new HashSet<>();
if (StringUtility.stringHasValue(fullyQualifiedTableNames)) {
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(fullyQualifiedTableNames, ",");
while (st.hasMoreTokens()) {
String s = st.nextToken().trim();
if (s.length() > 0) {
fullyqualifiedTables.add(s);
}
}
}
Set<String> contexts = new HashSet<>();
if (StringUtility.stringHasValue(contextIds)) {
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(contextIds, ",");
while (st.hasMoreTokens()) {
String s = st.nextToken().trim();
if (s.length() > 0) {
contexts.add(s);
}
}
}
IProgressMonitor monitor = (IProgressMonitor) getProject().getReferences().get(AntCorePlugin.ECLIPSE_PROGRESS_MONITOR);
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
subMonitor.beginTask("Generating MyBatis Artifacts:", 1000);
subMonitor.subTask("Parsing Configuration");
Properties p = propertyset == null ? new Properties() : propertyset.getProperties();
p.putAll(getProject().getUserProperties());
ConfigurationParser cp = new ConfigurationParser(p, warnings);
Configuration config = cp.parseConfiguration(configurationFile);
subMonitor.worked(50);
monitor.subTask("Generating Files from Database Tables");
MyBatisGenerator generator = new MyBatisGenerator(config, new EclipseShellCallback(), warnings);
EclipseProgressCallback progressCallback = new EclipseProgressCallback(subMonitor.newChild(950));
generator.generate(progressCallback, contexts, fullyqualifiedTables);
} catch (XMLParserException e) {
for (String error : e.getErrors()) {
log(error, Project.MSG_ERR);
}
throw new BuildException(e.getMessage());
} catch (SQLException e) {
throw new BuildException(e.getMessage());
} catch (IOException e) {
throw new BuildException(e.getMessage());
} catch (InvalidConfigurationException e) {
throw new BuildException(e.getMessage());
} catch (InterruptedException e) {
throw new BuildException("Cancelled by user");
} finally {
monitor.done();
}
for (String warning : warnings) {
log("WARNING: " + warning, Project.MSG_WARN);
}
}
use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.
the class GeneratorAntTask method execute.
@Override
public void execute() {
File configurationFile = calculateConfigurationFile();
Set<String> fullyqualifiedTables = calculateTables();
Set<String> contexts = calculateContexts();
List<String> warnings = new ArrayList<>();
try {
Properties p = propertyset == null ? null : propertyset.getProperties();
ConfigurationParser cp = new ConfigurationParser(p, warnings);
Configuration config = cp.parseConfiguration(configurationFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(new AntProgressCallback(this, verbose), contexts, fullyqualifiedTables);
} catch (XMLParserException | InvalidConfigurationException e) {
for (String error : e.getErrors()) {
log(error, Project.MSG_ERR);
}
throw new BuildException(e.getMessage());
} catch (SQLException | IOException e) {
throw new BuildException(e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
log(e, Project.MSG_ERR);
throw new BuildException(e.getMessage());
}
for (String error : warnings) {
log(error, Project.MSG_WARN);
}
}
use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.
the class MyBatisGeneratorTest method testGenerateInvalidConfigWithNoConnectionSources.
@Test
void testGenerateInvalidConfigWithNoConnectionSources() {
List<String> warnings = new ArrayList<>();
Configuration config = new Configuration();
Context context = new Context(ModelType.HIERARCHICAL);
context.setId("MyContext");
config.addContext(context);
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
InvalidConfigurationException e = assertThrows(InvalidConfigurationException.class, () -> {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
});
assertEquals(3, e.getErrors().size());
}
use of org.mybatis.generator.exception.InvalidConfigurationException in project generator by mybatis.
the class MyBatisGeneratorTest method testGenerateMyBatis3WithInvalidConfig.
@Test
void testGenerateMyBatis3WithInvalidConfig() throws Exception {
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigMyBatis3_badConfig.xml"));
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
InvalidConfigurationException e = assertThrows(InvalidConfigurationException.class, () -> {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
});
assertEquals(2, e.getErrors().size());
}
Aggregations