|
29 | 29 | import org.apache.maven.plugins.annotations.LifecyclePhase; |
30 | 30 | import org.apache.maven.plugins.annotations.Mojo; |
31 | 31 | import org.apache.maven.plugins.annotations.Parameter; |
| 32 | +import org.codehaus.plexus.util.FileUtils; |
32 | 33 | import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilder; |
33 | 34 | import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilderFactory; |
34 | 35 | import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.DynamicEntityClassBuilder; |
35 | 36 | import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.EntityClassBuilder; |
36 | 37 | import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.wrapper.ClassBuilderWrapper; |
| 38 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template.CodegenFile; |
| 39 | +import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template.CodegenTemplate; |
37 | 40 | import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.tools.CamelTools; |
38 | 41 | import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.writer.JavaClassWriter; |
39 | 42 | import org.springframework.util.ObjectUtils; |
|
60 | 63 | @Mojo(name = "generator", defaultPhase = LifecyclePhase.COMPILE) |
61 | 64 | @Execute(phase = LifecyclePhase.COMPILE) |
62 | 65 | public class ApiBootMybatisEnhanceCodegen extends AbstractMojo { |
63 | | - /** |
64 | | - * file suffix |
65 | | - */ |
66 | | - private static final String FILE_SUFFIX = ".java"; |
67 | 66 | /** |
68 | 67 | * Whether to execute automatically |
69 | 68 | * Default not to execute |
@@ -150,12 +149,16 @@ public void execute() throws MojoExecutionException, MojoFailureException { |
150 | 149 | getLog().warn("Automatic code generation is not turned on. If you need to generate entity classes, configure 【execute=true】"); |
151 | 150 | return; |
152 | 151 | } |
| 152 | + |
153 | 153 | // code builder properties |
154 | 154 | CodeBuilderProperties codeBuilderProperties = CodeBuilderProperties.builder().dbType(dbType).dbName(dbName).dbUrl(dbUrl).dbUserName(dbUserName).dbPassword(dbPassword).dbDriverClassName(dbDriverClassName).build(); |
155 | 155 |
|
156 | 156 | // get database instance by DbTypeEnum |
157 | 157 | DataBase dataBase = DataBaseFactory.newInstance(codeBuilderProperties); |
158 | 158 |
|
| 159 | + // load codegen.setting.json |
| 160 | + String codegenSetting = loadCodegenSetting(); |
| 161 | + |
159 | 162 | List<String> tableNames = ObjectUtils.isEmpty(tables) ? getTableNames(dataBase) : tables; |
160 | 163 |
|
161 | 164 | tableNames.stream().forEach(tableName -> { |
@@ -201,9 +204,55 @@ public void execute() throws MojoExecutionException, MojoFailureException { |
201 | 204 | JavaClassWriter.writeToJavaFile(classPath, classContent); |
202 | 205 | } |
203 | 206 | }); |
| 207 | + |
| 208 | + // generator java file with codegen.setting.json |
| 209 | + if (!ObjectUtils.isEmpty(codegenSetting)) { |
| 210 | + // cover old data |
| 211 | + wrapper.setTableCamelName(className); |
| 212 | + wrapper.setPackageName(packageName); |
| 213 | + |
| 214 | + CodegenTemplate codegenTemplate = new CodegenTemplate(wrapper, codegenSetting); |
| 215 | + |
| 216 | + // formatter all template java file |
| 217 | + List<CodegenFile> files = codegenTemplate.formatJavaFiles(); |
| 218 | + |
| 219 | + if (!ObjectUtils.isEmpty(files)) { |
| 220 | + files.stream().forEach(file -> { |
| 221 | + getLog().info("generation file -> " + file.getFileName()); |
| 222 | + // generator package dir & return full file path |
| 223 | + String fullFilePath = getNewClassPath(file.getFileName(), file.getPackageName()); |
| 224 | + if (!StringUtils.isEmpty(file.getJavaContent()) && !StringUtils.isEmpty(fullFilePath)) { |
| 225 | + // invoke content write |
| 226 | + JavaClassWriter.writeToJavaFile(fullFilePath, file.getJavaContent()); |
| 227 | + } |
| 228 | + }); |
| 229 | + } |
| 230 | + } |
204 | 231 | }); |
205 | 232 | } |
206 | 233 |
|
| 234 | + /** |
| 235 | + * load codegen.setting.json parse to CodegenSetting entity |
| 236 | + */ |
| 237 | + private String loadCodegenSetting() { |
| 238 | + try { |
| 239 | + // formatter codegen.setting.json path |
| 240 | + String settingJsonPath = String.format("%s%s", EnhanceCodegenConstant.CLASSES_PATH.replace(EnhanceCodegenConstant.POINT, File.separator), EnhanceCodegenConstant.SETTING_JSON); |
| 241 | + |
| 242 | + // read codegen.setting.json content |
| 243 | + File file = new File(projectBaseDir + settingJsonPath); |
| 244 | + String content = FileUtils.fileRead(file); |
| 245 | + |
| 246 | + getLog().info("codegen.setting.json:"); |
| 247 | + getLog().info(content); |
| 248 | + |
| 249 | + return content; |
| 250 | + } catch (Exception e) { |
| 251 | + getLog().error(e); |
| 252 | + } |
| 253 | + return null; |
| 254 | + } |
| 255 | + |
207 | 256 | /** |
208 | 257 | * get table name list |
209 | 258 | * |
@@ -240,28 +289,6 @@ private String getGeneratorDir(String prefixDir) { |
240 | 289 | * @return class path |
241 | 290 | */ |
242 | 291 | private String getNewClassPath(String entityClassName, String prefixDir) { |
243 | | - return String.format("%s%s%s%s", getGeneratorDir(prefixDir), File.separator, entityClassName, FILE_SUFFIX); |
244 | | - } |
245 | | - |
246 | | - /** |
247 | | - * 递归删除目录下的所有文件及子目录下所有文件 |
248 | | - * |
249 | | - * @param dir 将要删除的文件目录 |
250 | | - * @return boolean Returns "true" if all deletions were successful. |
251 | | - * If a deletion fails, the method stops attempting to |
252 | | - * delete and returns "false". |
253 | | - */ |
254 | | - private static boolean deleteDir(File dir) { |
255 | | - if (dir.isDirectory()) { |
256 | | - String[] children = dir.list(); |
257 | | - for (int i = 0; i < children.length; i++) { |
258 | | - boolean success = deleteDir(new File(dir, children[i])); |
259 | | - if (!success) { |
260 | | - return false; |
261 | | - } |
262 | | - } |
263 | | - } |
264 | | - // 目录此时为空,可以删除 |
265 | | - return dir.delete(); |
| 292 | + return String.format("%s%s%s%s", getGeneratorDir(prefixDir), File.separator, entityClassName, EnhanceCodegenConstant.JAVA_SUFFIX); |
266 | 293 | } |
267 | 294 | } |
0 commit comments