FlywayTask implements GenTask { @Override public void execute(PathResolver pathResolver) throws Exception { CompilationUnit cu = new CompilationUnit(); cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("db.migration"))); List<ImportDeclaration> imports = new ArrayList<>(); imports.add(new ImportDeclaration(ASTHelper.createNameExpr("org.flywaydb.core.api.migration.BaseJavaMigration"), false, false)); imports.add(new ImportDeclaration(ASTHelper.createNameExpr("org.flywaydb.core.api.migration.Context"), false, false)); imports.add(new ImportDeclaration(ASTHelper.createNameExpr("java.sql.Statement"), false, false)); cu.setImports(imports); String version = LocalDateTime.now().format(VERSION_FMT); ClassOrInterfaceDeclaration migrationClass = new ClassOrInterfaceDeclaration( ModifierSet.PUBLIC, false, "V" + version + "__Create" + CaseConverter.pascalCase(tableName)); ASTHelper.addTypeDeclaration(cu, migrationClass); List<ClassOrInterfaceType> extendsList = new ArrayList<>(); extendsList.add(new ClassOrInterfaceType("BaseJavaMigration")); migrationClass.setExtends(extendsList); MethodDeclaration migrateMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "migrate"); ASTHelper.addMember(migrationClass, migrateMethod); ASTHelper.addParameter(migrateMethod, ASTHelper.createParameter( ASTHelper.createReferenceType("Context", 0), "context" )); migrateMethod.setThrows(Collections.singletonList(ASTHelper.createNameExpr("Exception"))); List<AnnotationExpr> annotations = new ArrayList<>(); annotations.add(new MarkerAnnotationExpr(ASTHelper.createNameExpr("Override"))); migrateMethod.setAnnotations(annotations); BlockStmt block = new BlockStmt(); migrateMethod.setBody(block); TryStmt tryStmt = new TryStmt(createStatementExecuteBlock(), null, null); tryStmt.setResources(Collections.singletonList(createAssignStatement())); ASTHelper.addStmt(block, tryStmt); try (Writer writer = new OutputStreamWriter( pathResolver.destinationAsStream( "src/main/java/db/migration/" + migrationClass.getName() + ".java"))) { writer.write(cu.toString()); } } FlywayTask(String destination, String tableName, String createTableStatement); @Override void execute(PathResolver pathResolver); @Override String getDestinationPath();  }
@Test void test() throws Exception { FlywayTask task = new FlywayTask("", "User", "sql"); PathResolverMock resolver = new PathResolverMock(); task.execute(resolver); System.out.println(resolver.getResult()); }