Search in sources :

Example 21 with ConfigurationParser

use of org.mybatis.generator.config.xml.ConfigurationParser 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());
}
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) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) Test(org.junit.jupiter.api.Test)

Example 22 with ConfigurationParser

use of org.mybatis.generator.config.xml.ConfigurationParser in project generator by mybatis.

the class MyBatisGeneratorMojo method execute.

@Override
public void execute() throws MojoExecutionException {
    if (skip) {
        getLog().info("MyBatis generator is skipped.");
        return;
    }
    saveClassLoader();
    LogFactory.setLogFactory(new MavenLogFactory(this));
    calculateClassPath();
    // 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<>();
    for (Resource resource : resources) {
        resourceDirectories.add(resource.getDirectory());
    }
    ClassLoader cl = ClassloaderUtility.getCustomClassloader(resourceDirectories);
    ObjectFactory.addExternalClassLoader(cl);
    if (configurationFile == null) {
        throw new MojoExecutionException(// $NON-NLS-1$
        Messages.getString("RuntimeError.0"));
    }
    List<String> warnings = new ArrayList<>();
    if (!configurationFile.exists()) {
        throw new MojoExecutionException(Messages.getString("RuntimeError.1", // $NON-NLS-1$
        configurationFile.toString()));
    }
    runScriptIfNecessary();
    Set<String> fullyqualifiedTables = new HashSet<>();
    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<>();
    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);
    }
    restoreClassLoader();
}
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)

Example 23 with ConfigurationParser

use of org.mybatis.generator.config.xml.ConfigurationParser in project imooc-springboot-starter by leechenxiang.

the class GeneratorDisplay method generator.

public void generator() throws Exception {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    // 指定 逆向工程配置文件
    File configFile = new File("generatorConfig.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
}
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) File(java.io.File) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Aggregations

ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)23 ArrayList (java.util.ArrayList)22 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)22 Configuration (org.mybatis.generator.config.Configuration)22 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)19 File (java.io.File)13 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)9 IOException (java.io.IOException)7 SQLException (java.sql.SQLException)7 XMLParserException (org.mybatis.generator.exception.XMLParserException)7 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 Properties (java.util.Properties)2 BuildException (org.apache.tools.ant.BuildException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 Test (org.junit.Test)2 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)2 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)2 EclipseProgressCallback (org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback)2 EclipseShellCallback (org.mybatis.generator.eclipse.core.callback.EclipseShellCallback)2