Search in sources :

Example 1 with Parameters

use of com.github.mengweijin.generator.entity.Parameters in project code-generator-maven-plugin by mengweijin.

the class DefaultAutoGenerator method execute.

public void execute() {
    Parameters parameters = projectInfo.getParameters();
    String outputDir = FileUtil.file(projectInfo.getBaseDir(), "target/code-generator/").getAbsolutePath();
    // clean directory target/code-generator
    FileUtil.del(outputDir);
    FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(dataSourceConfigBuilder()).globalConfig(builder -> builder.fileOverride().author(parameters.getAuthor()).enableSwagger().disableOpenDir().outputDir(outputDir).dateType(DateType.TIME_PACK).commentDate("yyyy-MM-dd")).packageConfig(builder -> builder.parent(parameters.getOutputPackage())).templateConfig((Consumer<TemplateConfig.Builder>) TemplateConfig.Builder::disable).strategyConfig(builder -> builder.addInclude(this.trimItemName(parameters.getTables())).addTablePrefix(this.trimItemName(parameters.getTablePrefix())).entityBuilder().superClass(parameters.getSuperEntityClass()).enableChainModel().enableLombok().enableTableFieldAnnotation().versionColumnName("version").versionPropertyName("version").logicDeleteColumnName("deleted").logicDeletePropertyName("deleted").naming(NamingStrategy.underline_to_camel).addSuperEntityColumns(this.generateDefaultSuperEntityColumns()).controllerBuilder().superClass(parameters.getSuperControllerClass()).enableHyphenStyle().enableRestStyle().serviceBuilder().superServiceClass(parameters.getSuperServiceClass()).superServiceImplClass(parameters.getSuperServiceImplClass()).mapperBuilder().superClass(parameters.getSuperDaoClass()).enableBaseColumnList().enableBaseResultMap()).injectionConfig(builder -> {
        builder.beforeOutputFile(((tableInfo, objectMap) -> {
            enhanceObjectMap(objectMap, parameters);
            FileOutput.outputFile(tableInfo, objectMap, projectInfo, outputDir);
        }));
    }).templateEngine(TemplateEngineFactory.getTemplateEngine(this.projectInfo.getParameters().getTemplateType()));
    fastAutoGenerator.execute();
}
Also used : DbInfo(com.github.mengweijin.generator.entity.DbInfo) Arrays(java.util.Arrays) Getter(lombok.Getter) CustomerDataSource(com.github.mengweijin.generator.config.CustomerDataSource) ClassUtil(cn.hutool.core.util.ClassUtil) DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) ArrayList(java.util.ArrayList) FastAutoGenerator(com.baomidou.mybatisplus.generator.FastAutoGenerator) NamingStrategy(com.baomidou.mybatisplus.generator.config.rules.NamingStrategy) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) Map(java.util.Map) DbInfoUtils(com.github.mengweijin.generator.util.DbInfoUtils) IdField(com.github.mengweijin.generator.entity.IdField) TemplateEngineFactory(com.github.mengweijin.generator.factory.TemplateEngineFactory) Parameters(com.github.mengweijin.generator.entity.Parameters) ProjectInfo(com.github.mengweijin.generator.entity.ProjectInfo) Field(java.lang.reflect.Field) Consumer(java.util.function.Consumer) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DateType(com.baomidou.mybatisplus.generator.config.rules.DateType) FileOutput(com.github.mengweijin.generator.config.FileOutput) FileUtil(cn.hutool.core.io.FileUtil) TableField(com.baomidou.mybatisplus.generator.config.po.TableField) Parameters(com.github.mengweijin.generator.entity.Parameters) Consumer(java.util.function.Consumer) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) FastAutoGenerator(com.baomidou.mybatisplus.generator.FastAutoGenerator)

Example 2 with Parameters

use of com.github.mengweijin.generator.entity.Parameters in project code-generator-maven-plugin by mengweijin.

the class AbstractGeneratorMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        // clean TMP folder
        FileUtil.del(FileUtil.file(ProjectInfo.TMP_DIR));
        this.loadParentProjectClassToJarClassLoader();
        TemplateUtils.copyTemplateFolderToJavaTmp("templates/");
        this.parameters = Optional.ofNullable(this.parameters).orElse(new Parameters());
        this.setDefaultFixedParameters(this.parameters);
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setParameters(this.parameters);
        projectInfo.setResourceList(this.getResources());
        projectInfo.setBaseDir(this.baseDir);
        projectInfo.setSourceDir(this.sourceDir);
        new DefaultAutoGenerator(projectInfo).execute();
    } catch (Exception e) {
        getLog().error(e);
        throw new RuntimeException(e);
    }
}
Also used : Parameters(com.github.mengweijin.generator.entity.Parameters) ProjectInfo(com.github.mengweijin.generator.entity.ProjectInfo) DefaultAutoGenerator(com.github.mengweijin.generator.DefaultAutoGenerator) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 3 with Parameters

use of com.github.mengweijin.generator.entity.Parameters in project code-generator-maven-plugin by mengweijin.

the class FileOutput method outputFile.

public static void outputFile(TableInfo tableInfo, Map<String, Object> objectMap, ProjectInfo projectInfo, String outputDir) {
    Parameters parameters = projectInfo.getParameters();
    AbstractTemplateEngine templateEngine = TemplateEngineFactory.getTemplateEngine(parameters.getTemplateType());
    String outputPackage = parameters.getOutputPackage();
    List<File> templateFileList = FileUtil.loopFiles(parameters.getTemplateLocation(), file -> file.isFile() && file.getName().toLowerCase().endsWith(parameters.getTemplateType().getSuffix()));
    if (CollectionUtil.isEmpty(templateFileList)) {
        throw new RuntimeException("No template files found in location " + parameters.getTemplateLocation());
    } else {
        String message = "Found " + templateFileList.size() + " template files in location " + parameters.getTemplateLocation();
        log.info(message);
    }
    try {
        for (File templateFile : templateFileList) {
            // 初始化输出文件的名称和路径
            File outputFile = buildOutputFile(tableInfo, templateFile.getAbsolutePath(), outputDir, outputPackage);
            FileUtil.mkParentDirs(outputFile);
            // 使用模板引擎,渲染并输出文件
            templateEngine.writer(objectMap, templateFile.getAbsolutePath(), outputFile);
        }
    } catch (Exception e) {
        log.error("Template engine writer error!", e);
        throw new RuntimeException(e);
    }
}
Also used : Parameters(com.github.mengweijin.generator.entity.Parameters) File(java.io.File) AbstractTemplateEngine(com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine)

Aggregations

Parameters (com.github.mengweijin.generator.entity.Parameters)3 ProjectInfo (com.github.mengweijin.generator.entity.ProjectInfo)2 FileUtil (cn.hutool.core.io.FileUtil)1 ClassUtil (cn.hutool.core.util.ClassUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 FastAutoGenerator (com.baomidou.mybatisplus.generator.FastAutoGenerator)1 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)1 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)1 TableField (com.baomidou.mybatisplus.generator.config.po.TableField)1 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)1 DateType (com.baomidou.mybatisplus.generator.config.rules.DateType)1 NamingStrategy (com.baomidou.mybatisplus.generator.config.rules.NamingStrategy)1 AbstractTemplateEngine (com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine)1 DefaultAutoGenerator (com.github.mengweijin.generator.DefaultAutoGenerator)1 CustomerDataSource (com.github.mengweijin.generator.config.CustomerDataSource)1 FileOutput (com.github.mengweijin.generator.config.FileOutput)1 DbInfo (com.github.mengweijin.generator.entity.DbInfo)1 IdField (com.github.mengweijin.generator.entity.IdField)1 TemplateEngineFactory (com.github.mengweijin.generator.factory.TemplateEngineFactory)1 DbInfoUtils (com.github.mengweijin.generator.util.DbInfoUtils)1