Skip to content

Commit

Permalink
修复代码生成在项目打包后模板未找到问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyouzhuguli committed Jun 17, 2019
1 parent a8e9dd1 commit 944ed75
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.mrbird.febs.generator.helper;

import cc.mrbird.febs.common.annotation.Helper;
import cc.mrbird.febs.common.utils.AddressUtil;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.generator.entity.Column;
import cc.mrbird.febs.generator.entity.GeneratorConfig;
Expand All @@ -11,11 +12,13 @@
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

/**
* @author MrBird
Expand Down Expand Up @@ -122,7 +125,14 @@ private JSONObject toJSONObject(Object o) {

private Template getTemplate(String templateName) throws Exception {
Configuration configuration = new freemarker.template.Configuration(Configuration.VERSION_2_3_23);
String templatePath = GeneratorHelper.class.getResource("/generator/templates").getPath();
String templatePath = GeneratorHelper.class.getResource("/generator/templates/").getPath();
File file = new File(templatePath);
if (!file.exists()) {
templatePath = System.getProperties().getProperty("java.io.tmpdir");
System.out.println(templatePath);
file = new File(templatePath + "/" + templateName);
FileUtils.copyInputStreamToFile(Objects.requireNonNull(AddressUtil.class.getClassLoader().getResourceAsStream("classpath:generator/templates/" + templateName)), file);
}
configuration.setDirectoryForTemplateLoading(new File(templatePath));
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
Expand Down

0 comments on commit 944ed75

Please sign in to comment.