diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java index b2ef971f88..938038b453 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/CompositePlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2021 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -105,6 +105,22 @@ public List contextGenerateAdditionalKotlinFiles(Introspect .collect(Collectors.toList()); } + @Override + public List contextGenerateAdditionalFiles() { + return plugins.stream() + .map(Plugin::contextGenerateAdditionalFiles) + .flatMap(List::stream) + .collect(Collectors.toList()); + } + + @Override + public List contextGenerateAdditionalFiles(IntrospectedTable introspectedTable) { + return plugins.stream() + .map(p -> p.contextGenerateAdditionalFiles(introspectedTable)) + .flatMap(List::stream) + .collect(Collectors.toList()); + } + @Override public List contextGenerateAdditionalXmlFiles() { return plugins.stream() diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedFile.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedFile.java index 5c7f03d2e6..92c2fc6f72 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedFile.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,4 +79,6 @@ public String toString() { * @return true, if is mergeable */ public abstract boolean isMergeable(); + + public abstract String getFileEncoding(); } diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedJavaFile.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedJavaFile.java index f1f5a22927..985414b6c9 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedJavaFile.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedJavaFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -78,6 +78,7 @@ public boolean isMergeable() { return true; } + @Override public String getFileEncoding() { return fileEncoding; } diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedKotlinFile.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedKotlinFile.java index 9eb851d214..ea842edf80 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedKotlinFile.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedKotlinFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,6 +55,7 @@ public boolean isMergeable() { return false; } + @Override public String getFileEncoding() { return fileEncoding; } diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedXmlFile.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedXmlFile.java index 232b0ddce4..096539505b 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedXmlFile.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/GeneratedXmlFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,4 +63,9 @@ public boolean isMergeable() { public void setMergeable(boolean isMergeable) { this.isMergeable = isMergeable; } + + @Override + public String getFileEncoding() { + return "UTF-8"; //$NON-NLS-1$ + } } diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java index 6e02f0762c..11d8264a3a 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2021 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,6 +66,11 @@ public class MyBatisGenerator { private final List generatedKotlinFiles = new ArrayList<>(); + /** + * Any kind of generated file generated by plugin methods contextGenerateAdditionalFiles + */ + private final List otherGeneratedFiles = new ArrayList<>(); + private final List warnings; private final Set projects = new HashSet<>(); @@ -261,7 +266,7 @@ public void generate(ProgressCallback callback, Set contextIds, for (Context context : contextsToRun) { context.generateFiles(callback, generatedJavaFiles, - generatedXmlFiles, generatedKotlinFiles, warnings); + generatedXmlFiles, generatedKotlinFiles, otherGeneratedFiles, warnings); } // now save the files @@ -281,7 +286,12 @@ public void generate(ProgressCallback callback, Set contextIds, for (GeneratedKotlinFile gkf : generatedKotlinFiles) { projects.add(gkf.getTargetProject()); - writeGeneratedKotlinFile(gkf, callback); + writeGeneratedFile(gkf, callback); + } + + for (GeneratedFile gf : otherGeneratedFiles) { + projects.add(gf.getTargetProject()); + writeGeneratedFile(gf, callback); } for (String project : projects) { @@ -330,34 +340,34 @@ private void writeGeneratedJavaFile(GeneratedJavaFile gjf, ProgressCallback call } } - private void writeGeneratedKotlinFile(GeneratedKotlinFile gkf, ProgressCallback callback) + private void writeGeneratedFile(GeneratedFile gf, ProgressCallback callback) throws InterruptedException, IOException { File targetFile; String source; try { - File directory = shellCallback.getDirectory(gkf - .getTargetProject(), gkf.getTargetPackage()); - targetFile = new File(directory, gkf.getFileName()); + File directory = shellCallback.getDirectory(gf + .getTargetProject(), gf.getTargetPackage()); + targetFile = new File(directory, gf.getFileName()); if (targetFile.exists()) { if (shellCallback.isOverwriteEnabled()) { - source = gkf.getFormattedContent(); + source = gf.getFormattedContent(); warnings.add(getString("Warning.11", //$NON-NLS-1$ targetFile.getAbsolutePath())); } else { - source = gkf.getFormattedContent(); - targetFile = getUniqueFileName(directory, gkf + source = gf.getFormattedContent(); + targetFile = getUniqueFileName(directory, gf .getFileName()); warnings.add(getString( "Warning.2", targetFile.getAbsolutePath())); //$NON-NLS-1$ } } else { - source = gkf.getFormattedContent(); + source = gf.getFormattedContent(); } callback.checkCancel(); callback.startTask(getString( "Progress.15", targetFile.getName())); //$NON-NLS-1$ - writeFile(targetFile, source, gkf.getFileEncoding()); + writeFile(targetFile, source, gf.getFileEncoding()); } catch (ShellException e) { warnings.add(e.getMessage()); } @@ -393,7 +403,7 @@ private void writeGeneratedXmlFile(GeneratedXmlFile gxf, ProgressCallback callba callback.checkCancel(); callback.startTask(getString( "Progress.15", targetFile.getName())); //$NON-NLS-1$ - writeFile(targetFile, source, "UTF-8"); //$NON-NLS-1$ + writeFile(targetFile, source, gxf.getFileEncoding()); } catch (ShellException e) { warnings.add(e.getMessage()); } diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java index 1997861507..3f2f56b749 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/Plugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2021 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -164,6 +164,14 @@ default List contextGenerateAdditionalKotlinFiles(Introspec return Collections.emptyList(); } + default List contextGenerateAdditionalFiles() { + return Collections.emptyList(); + } + + default List contextGenerateAdditionalFiles(IntrospectedTable introspectedTable) { + return Collections.emptyList(); + } + /** * This method can be used to generate any additional XML file needed by * your implementation. This method is called once, after all other XML diff --git a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java index 53f94fa408..96d3fd4b67 100644 --- a/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java +++ b/core/mybatis-generator-core/src/main/java/org/mybatis/generator/config/Context.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2021 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.ConnectionFactory; +import org.mybatis.generator.api.GeneratedFile; import org.mybatis.generator.api.GeneratedJavaFile; import org.mybatis.generator.api.GeneratedKotlinFile; import org.mybatis.generator.api.GeneratedXmlFile; @@ -432,6 +433,7 @@ public void generateFiles(ProgressCallback callback, List generatedJavaFiles, List generatedXmlFiles, List generatedKotlinFiles, + List otherGeneratedFiles, List warnings) throws InterruptedException { @@ -470,6 +472,8 @@ public void generateFiles(ProgressCallback callback, .contextGenerateAdditionalXmlFiles(introspectedTable)); generatedKotlinFiles.addAll(pluginAggregator .contextGenerateAdditionalKotlinFiles(introspectedTable)); + otherGeneratedFiles.addAll(pluginAggregator + .contextGenerateAdditionalFiles(introspectedTable)); } generatedJavaFiles.addAll(pluginAggregator @@ -478,6 +482,8 @@ public void generateFiles(ProgressCallback callback, .contextGenerateAdditionalXmlFiles()); generatedKotlinFiles.addAll(pluginAggregator .contextGenerateAdditionalKotlinFiles()); + otherGeneratedFiles.addAll(pluginAggregator + .contextGenerateAdditionalFiles()); } /** diff --git a/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml b/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml index c98864af9d..cff705ccbf 100644 --- a/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml +++ b/core/mybatis-generator-core/src/site/xhtml/whatsNew.xhtml @@ -128,6 +128,7 @@ required:

  • Added the ability to specify a MyBatis Dynamic SQL Support class name on the table configuration
  • Added a plugin to generate @CacheNamespace annotations
  • Added the ability to specify a name for the inner class generated in the MyBatis Dynamic SQL support class
  • +
  • Added plugin methods that allow generation of any arbitrary file type
  • Removed Items