测试修改mybatis-generator-extend插件
This commit is contained in:
+22
@@ -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();
|
||||
|
||||
+4
-4
@@ -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模板文件失败------------");
|
||||
|
||||
+65
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user