From 3e045b578f9b935beb87d99e385bb45983cca237 Mon Sep 17 00:00:00 2001 From: lsy <18332763730@163.com> Date: Sat, 2 Dec 2017 12:23:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BF=AE=E6=94=B9mybatis-gen?= =?UTF-8?q?erator-extend=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mojo/MyabtisGeneratorExtendMojo.java | 22 +++++++ .../generator/extend/util/FreemarkerUtil.java | 8 +-- .../generator/extend/util/JarUtil.java | 65 +++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MyabtisGeneratorExtendMojo.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MyabtisGeneratorExtendMojo.java index 982bf95..7194738 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MyabtisGeneratorExtendMojo.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/mojo/MyabtisGeneratorExtendMojo.java @@ -6,14 +6,36 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; @Mojo(name = "generateFiles") public class MyabtisGeneratorExtendMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { + //获取当前项目jar包的路径 + String path = MyabtisGeneratorExtendMojo.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + JarInputStream jarInputStream =null; + try { + jarInputStream = new JarInputStream(new FileInputStream(new File(path))); + } catch (IOException e) { + e.printStackTrace(); + }finally { + try { + jarInputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + InputStream templates = this.getClass().getClassLoader().getResourceAsStream("templates"); + System.out.println(path); Map pluginContext = getPluginContext(); Set set = pluginContext.entrySet(); Iterator iterator = set.iterator(); diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java index d2484bd..822d6af 100644 --- a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java @@ -5,6 +5,7 @@ import freemarker.core.ParseException; import freemarker.template.*; import java.io.*; +import java.net.URL; import java.util.Map; /** @@ -36,11 +37,10 @@ public class FreemarkerUtil { //1.设置编码 cfg.setDefaultEncoding("utf-8"); //2.设置加载模板文件的位置,这个位置是随着这个class加载的目录位置而确定的 - String relativeClasspath = FreemarkerUtil.class.getResource("").getPath(); - int lastIndexOf = relativeClasspath.lastIndexOf("com/micro/fast"); - String absoluteClasspath = relativeClasspath.substring(0, lastIndexOf); + URL resource = FreemarkerUtil.class.getResource("/" + templatesDirUrl); + String path = resource.getPath(); try { - cfg.setDirectoryForTemplateLoading(new File(absoluteClasspath+templatesDirUrl)); + cfg.setDirectoryForTemplateLoading(new File(path)); } catch (IOException e) { e.printStackTrace(); System.out.println("-----------加载mybatis-generator-extend模板文件失败------------"); diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java new file mode 100644 index 0000000..f8c8d1c --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend-maven-plugin/src/main/java/com/micro/fast/mybatis/generator/extend/util/JarUtil.java @@ -0,0 +1,65 @@ +package com.micro.fast.mybatis.generator.extend.util; + + +import java.io.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.JarInputStream; + +public class JarUtil { + + public static String getThisJarPath(){ + return JarUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + } + public static String getThisJarPathParent(){ + return new File(getThisJarPath()).getParent(); + } + + public static void unThisJar(){ + JarInputStream jarInputStream = null; + JarFile jarFile = null; + try { + jarInputStream = new JarInputStream(new FileInputStream(new File(getThisJarPath()))); + jarFile = new JarFile(new File(getThisJarPath())); + JarEntry jarEntry = null; + while ((jarEntry = jarInputStream.getNextJarEntry())!=null){ + String fileName = jarEntry.getName(); + if ("generatorConfig.ftl".equals(fileName)||"mybatisGenerator.ftl".equals(fileName)){ + File file = new File(getThisJarPathParent() +"/"+fileName); + if (file.exists()){ + file.delete(); + } + InputStream inputStream = null; + OutputStream outputStream = null; + try { + outputStream = new FileOutputStream(new File(getThisJarPathParent()+"/"+fileName)); + inputStream = jarFile.getInputStream(jarEntry); + int l = 0; + while ((l= inputStream.read())!=-1){ + outputStream.write(l); + outputStream.flush(); + } + }finally { + outputStream.close(); + inputStream.close(); + } + + } + jarEntry = null; + } + } catch (IOException e) { + e.printStackTrace(); + }finally { + try { + jarInputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + jarFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +}