使用Velocity建立maven项目

背景

最近接到了一个项目生成并自动编译的功能需求,在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项目

思路
  1. 生成maven结构
  2. 根据模板生成对应文件
  3. 编译(后续)
  4. 上传(后续)

创建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";
}

项目地址 :https://github.com/miaoqiyuan/velocitydemo.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值