use of org.mybatis.generator.api.MyBatisGenerator in project generator by mybatis.
the class MyBatisGeneratorTest method testGenerateMyBatis3WithInvalidConfig.
@Test(expected = InvalidConfigurationException.class)
public void testGenerateMyBatis3WithInvalidConfig() throws Exception {
List<String> warnings = new ArrayList<String>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigMyBatis3_badConfig.xml"));
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
try {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
} catch (InvalidConfigurationException e) {
assertEquals(2, e.getErrors().size());
throw e;
}
}
use of org.mybatis.generator.api.MyBatisGenerator in project generator by mybatis.
the class MyBatisGeneratorTest method testGenerateInvalidConfigWithNoConnectionSources.
@Test(expected = InvalidConfigurationException.class)
public void testGenerateInvalidConfigWithNoConnectionSources() throws Exception {
List<String> warnings = new ArrayList<String>();
Configuration config = new Configuration();
Context context = new Context(ModelType.HIERARCHICAL);
context.setId("MyContext");
config.addContext(context);
DefaultShellCallback shellCallback = new DefaultShellCallback(true);
try {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
myBatisGenerator.generate(null, null, null, false);
} catch (InvalidConfigurationException e) {
assertEquals(3, e.getErrors().size());
throw e;
}
}
use of org.mybatis.generator.api.MyBatisGenerator in project generator by mybatis.
the class MyBatisGeneratorMojo method execute.
public void execute() throws MojoExecutionException {
if (skip) {
getLog().info("MyBatis generator is skipped.");
return;
}
LogFactory.setLogFactory(new MavenLogFactory(this));
// add resource directories to the classpath. This is required to support
// use of a properties file in the build. Typically, the properties file
// is in the project's source tree, but the plugin classpath does not
// include the project classpath.
List<Resource> resources = project.getResources();
List<String> resourceDirectories = new ArrayList<String>();
for (Resource resource : resources) {
resourceDirectories.add(resource.getDirectory());
}
ClassLoader cl = ClassloaderUtility.getCustomClassloader(resourceDirectories);
ObjectFactory.addResourceClassLoader(cl);
if (configurationFile == null) {
throw new MojoExecutionException(//$NON-NLS-1$
Messages.getString("RuntimeError.0"));
}
List<String> warnings = new ArrayList<String>();
if (!configurationFile.exists()) {
throw new MojoExecutionException(Messages.getString("RuntimeError.1", //$NON-NLS-1$
configurationFile.toString()));
}
runScriptIfNecessary();
Set<String> fullyqualifiedTables = new HashSet<String>();
if (StringUtility.stringHasValue(tableNames)) {
//$NON-NLS-1$
StringTokenizer st = new StringTokenizer(tableNames, ",");
while (st.hasMoreTokens()) {
String s = st.nextToken().trim();
if (s.length() > 0) {
fullyqualifiedTables.add(s);
}
}
}
Set<String> contextsToRun = new HashSet<String>();
if (StringUtility.stringHasValue(contexts)) {
//$NON-NLS-1$
StringTokenizer st = new StringTokenizer(contexts, ",");
while (st.hasMoreTokens()) {
String s = st.nextToken().trim();
if (s.length() > 0) {
contextsToRun.add(s);
}
}
}
try {
ConfigurationParser cp = new ConfigurationParser(project.getProperties(), warnings);
Configuration config = cp.parseConfiguration(configurationFile);
ShellCallback callback = new MavenShellCallback(this, overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(new MavenProgressCallback(getLog(), verbose), contextsToRun, fullyqualifiedTables);
} catch (XMLParserException e) {
for (String error : e.getErrors()) {
getLog().error(error);
}
throw new MojoExecutionException(e.getMessage());
} catch (SQLException e) {
throw new MojoExecutionException(e.getMessage());
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage());
} catch (InvalidConfigurationException e) {
for (String error : e.getErrors()) {
getLog().error(error);
}
throw new MojoExecutionException(e.getMessage());
} catch (InterruptedException e) {
// ignore (will never happen with the DefaultShellCallback)
}
for (String error : warnings) {
getLog().warn(error);
}
if (project != null && outputDirectory != null && outputDirectory.exists()) {
project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
Resource resource = new Resource();
resource.setDirectory(outputDirectory.getAbsolutePath());
resource.addInclude("**/*.xml");
project.addResource(resource);
}
}
use of org.mybatis.generator.api.MyBatisGenerator 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 {
if (!StringUtility.stringHasValue(configfile)) {
throw new BuildException("configfile is a required parameter");
}
List<String> warnings = new ArrayList<String>();
File configurationFile = new File(configfile);
if (!configurationFile.exists()) {
throw new BuildException("configfile " + configfile + " does not exist");
}
Set<String> fullyqualifiedTables = new HashSet<String>();
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<String>();
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/iBATIS Artifacts:", 1000);
subMonitor.subTask("Parsing Configuration");
Properties p = propertyset == null ? null : propertyset.getProperties();
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.api.MyBatisGenerator in project generator by mybatis.
the class RunGeneratorThread method run.
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
*/
public void run(IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
subMonitor.beginTask("Generating MyBatis/iBATIS Artifacts:", 1000);
setClassLoader();
try {
subMonitor.subTask("Parsing Configuration");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(inputFile.getLocation().toFile());
subMonitor.worked(50);
MyBatisGenerator mybatisGenerator = new MyBatisGenerator(config, new EclipseShellCallback(), warnings);
monitor.subTask("Generating Files from Database Tables");
SubMonitor spm = subMonitor.newChild(950);
mybatisGenerator.generate(new EclipseProgressCallback(spm));
} catch (InterruptedException e) {
throw new OperationCanceledException();
} catch (SQLException e) {
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
Activator.getDefault().getLog().log(status);
throw new CoreException(status);
} catch (IOException e) {
Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
Activator.getDefault().getLog().log(status);
throw new CoreException(status);
} catch (XMLParserException e) {
List<String> errors = e.getErrors();
MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "XML Parser Errors\n See Details for more Information", null);
Iterator<String> iter = errors.iterator();
while (iter.hasNext()) {
Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
multiStatus.add(message);
}
throw new CoreException(multiStatus);
} catch (InvalidConfigurationException e) {
List<String> errors = e.getErrors();
MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "Invalid Configuration\n See Details for more Information", null);
Iterator<String> iter = errors.iterator();
while (iter.hasNext()) {
Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
multiStatus.add(message);
}
throw new CoreException(multiStatus);
} finally {
monitor.done();
restoreClassLoader();
}
}
Aggregations