Search in sources :

Example 11 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class JavaCodeGenerationTest method generateJavaFiles.

private static List<GeneratedJavaFile> generateJavaFiles(String configFile) throws Exception {
    List<String> warnings = new ArrayList<String>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(JavaCodeGenerationTest.class.getResourceAsStream(configFile));
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
    myBatisGenerator.generate(null, null, null, false);
    return myBatisGenerator.getGeneratedJavaFiles();
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 12 with Configuration

use of org.mybatis.generator.config.Configuration 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;
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) Test(org.junit.Test)

Example 13 with Configuration

use of org.mybatis.generator.config.Configuration 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;
    }
}
Also used : Context(org.mybatis.generator.config.Context) Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) Test(org.junit.Test)

Example 14 with Configuration

use of org.mybatis.generator.config.Configuration 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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Configuration(org.mybatis.generator.config.Configuration) ShellCallback(org.mybatis.generator.api.ShellCallback) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) Resource(org.apache.maven.model.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) StringTokenizer(java.util.StringTokenizer) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) HashSet(java.util.HashSet) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Aggregations

Configuration (org.mybatis.generator.config.Configuration)14 ArrayList (java.util.ArrayList)11 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)10 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)9 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)9 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)8 XMLParserException (org.mybatis.generator.exception.XMLParserException)6 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)5 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)5 HashSet (java.util.HashSet)4 StringTokenizer (java.util.StringTokenizer)4 Test (org.junit.Test)4 File (java.io.File)3 Properties (java.util.Properties)2 BuildException (org.apache.tools.ant.BuildException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 Context (org.mybatis.generator.config.Context)2 JavaClientGeneratorConfiguration (org.mybatis.generator.config.JavaClientGeneratorConfiguration)2