コード例 #1
0
  private boolean executeAll(File velocitySources, File outputDirectory)
      throws MojoExecutionException {
    List<File> files = new ArrayList<File>();
    String canoPath;
    try {
      velocitySources = velocitySources.getCanonicalFile();
      listVeloFiles(velocitySources, files);

      canoPath = sourcePathRoot.getCanonicalPath();
      System.out.println("Velocity root path = " + canoPath);
      Velocity.setProperty("file.resource.loader.path", canoPath); // file.getParent());
      Velocity.init();

    } catch (Exception ex) {
      throw new MojoExecutionException("Failed to list files from '" + velocitySources + "'", ex);
    }

    getLog().info("Found " + files.size() + " files in '" + velocitySources + "'...");

    if (files.isEmpty()) return false;

    for (File file : files) {
      try {
        file = file.getCanonicalFile();

        String name = file.getName();
        if (name.endsWith("~") || name.endsWith(".bak")) {
          getLog().info("Skipping: '" + name + "'");
          continue;
        }

        File outFile = getOutputFile(file, velocitySources, outputDirectory);
        if (outFile.exists() && outFile.lastModified() > file.lastModified()) {
          getLog().info("Up-to-date: '" + name + "'");
          continue;
        }
        getLog().info("Executing template '" + name + "'...");

        // context = new VelocityContext();
        String cano = file.getCanonicalPath();
        cano = cano.substring(canoPath.length());
        if (cano.startsWith(File.separator)) cano = cano.substring(File.separator.length());

        org.apache.velocity.Template template = Velocity.getTemplate(cano); // file.getName());

        VelocityContext context = new VelocityContext(); // execution.getParameters());
        context.put("primitives", Primitive.getPrimitives());
        context.put("primitivesNoBool", Primitive.getPrimitivesNoBool());
        context.put("bridJPrimitives", Primitive.getBridJPrimitives());

        StringWriter out = new StringWriter();
        template.merge(context, out);
        out.close();

        outFile.getParentFile().mkdirs();

        FileWriter f = new FileWriter(outFile);
        f.write(out.toString());
        f.close();
        // getLog().info("\tGenerated '" + outFile.getName() + "'");

      } catch (Exception ex) {
        // throw
        new MojoExecutionException("Failed to execute template '" + file + "'", ex)
            .printStackTrace();
      }
    }

    return true;
  }