背景
最近接到了一个项目生成并自动编译的功能需求,在freemarker和Velocity的简单研究后,最终决定采用Velocity技术.
Velocity相对于freemarker更加简单,更加轻量级,经过对项目的评估,Velocity完全可以满足需求
Velocity基本用法
项目结构
1. 创建要调用的模板
以项目的pom.xml为例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>${prj_name}</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
2 调用代码
public static void main(String[] args){
//设置模板内替换的值
VelocityContext context = new VelocityContext();
context.put("prj_name", "test");//对应模板文件中 ${prj_name}
//初始化模板
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
//读取模板文件
Template modifyTpt = ve.getTemplate("pom.xml.vm");
//输出
PrintWriter writer = null;
try {
writer = new PrintWriter("d:/pom.xml", "GBK");
modifyTpt.merge(context, writer);
writer.flush();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
生成maven项目
思路
- 生成maven结构
- 根据模板生成对应文件
- 编译(后续)
- 上传(后续)
创建maven结构
public static void createMavenStruct(String baseDir, String prjName) throws IOException {
baseDir = baseDir + prjName + "/";
FileUtil.createDir(baseDir);
FileUtil.createDir(baseDir + Constant.JAVA_DIR);
FileUtil.createDir(baseDir + Constant.SOURCES_DIR);
FileUtil.createDir(baseDir + Constant.PACKAGE);
VelocityContext context = new VelocityContext();
context.put("prj_name", prjName);
createVelocity("pom.xml.vm", baseDir + "pom.xml", context);
}
public static void createVelocity(String tempName, String path, VelocityContext ctx) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
Template modifyTpt = ve.getTemplate(tempName);
merge(modifyTpt, ctx, path);
}
public static void merge(Template template, VelocityContext ctx, String path) {
PrintWriter writer = null;
try {
writer = new PrintWriter(path, "GBK");
template.merge(ctx, writer);
writer.flush();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
FileUtil.java
public class FileUtil {
public static void createDir(String path) throws IOException {
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}else{
throw new IOException("文件夹已经存在");
}
}
}
Constant.java
public class Constant {
public final static String SRC_DIR = "src/main";
public final static String JAVA_DIR = SRC_DIR + "/java";
public final static String SOURCES_DIR = SRC_DIR + "/resources";
public final static String PACKAGE = JAVA_DIR + "/com/my/study";
}