All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fracpete.bootstrapp.core.Template Maven / Gradle / Ivy

/*
 * Template.java
 * Copyright (C) 2020 University of Waikato, Hamilton, NZ
 */

package com.github.fracpete.bootstrapp.core;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.logging.Logger;

/**
 * Manages the POM template.
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 */
public class Template {

  /** the resource location of the POM template. */
  public final static String TEMPLATE_FILE = "template.xml";

  /** the debian build plugin file. */
  public final static String DEBIANBUILD_FILE = "debian.build";

  /** the debian build plugin file (incl sources). */
  public final static String DEBIANBUILDSRC_FILE = "debian.build_incl_sources";

  /** the redhat build plugin file. */
  public final static String REDHATBUILD_FILE = "redhat.build";

  /** the redhat build plugin file (incl sources). */
  public final static String REDHATBUILDSRC_FILE = "redhat.build_incl_sources";

  /** the placeholder in the template POM for the dependencies (full dependencies tag). */
  public final static String PH_DEPENDENCIES = "";

  /** the placeholder in the template POM for the output directory (directory string). */
  public final static String PH_OUTPUTDIR = "";

  /** the placeholder in the template POM to skip downloading the sources (boolean). */
  public final static String PH_NOSOURCES = "";

  /** the placeholder in the template POM to skip spring-boot jar generation (boolean). */
  public final static String PH_NOSPRINGBOOT = "";

  /** the placeholder in the template POM for the main class (string). */
  public final static String PH_MAINCLASS = "";

  /** the placeholder in the template POM for the packaging (string, pom/jar). */
  public final static String PH_PACKAGING = "";

  /** the placeholder in the template POM for the project name. */
  public final static String PH_NAME = "";

  /** the placeholder in the template POM for the version. */
  public final static String PH_VERSION = "";

  /** the placeholder in the template POM for additional build plugins. */
  public final static String PH_BUILDPLUGINS = "";

  /** the default name. */
  public final static String DEFAULT_NAME = "bootstrapp-harness";

  /** the default version. */
  public final static String DEFAULT_VERSION = "0.0.1";

  /** for logging. */
  protected static Logger LOGGER = Logger.getLogger(Template.class.getName());

  /**
   * The configuration to use for customizing the template.
   */
  public static class Configuration {

    /** the maven output directory. */
    public File outputDirMaven;

    /** the dependencies to use (group:artifact:version). */
    public List dependencies;

    /** whether to skip downloading sources. */
    public boolean noSources;

    /** whether to skip generating spring boot jar. */
    public boolean noSpringBoot;

    /** the main class, can be null. */
    public String mainClass;

    /** the name of the project. */
    public String name;

    /** the version of the project. */
    public String version;

    /** additional build plugins to use. */
    public String buildPlugins;
  }

  /**
   * Configures the bundled template.
   *
   * @param outputDir		the directory to copy the template to (as pom.xml)
   * @param config 		the configuration
   * @return			null if successful, otherwise error message
   */
  public static String configureBundledTemplate(File outputDir, Configuration config) {
    String	result;
    String	path;
    File	file;

    try {
      path = com.github.fracpete.resourceextractor4j.Files.extractTo(Resources.LOCATION, TEMPLATE_FILE, System.getProperty("java.io.tmpdir"));
      result = configureTemplate(new File(path), outputDir, config);
      file = new File(path);
      if (file.exists())
        file.delete();
      return result;
    }
    catch (Exception e) {
      return "Failed to configure bundled template: " + e;
    }
  }

  /**
   * Configures the specified template.
   *
   * @param template 		the template file to configure
   * @param outputDir		the directory to copy the template to (as pom.xml)
   * @param config 		the configuration
   * @return			null if successful, otherwise error message
   */
  public static String configureTemplate(File template, File outputDir, Configuration config) {
    List	lines;
    int			i;
    String		line;
    StringBuilder	deps;
    String		depsStr;
    String[]		parts;
    boolean		modified;

    // build dependency string
    deps = new StringBuilder();
    deps.append("  \n");
    for (String dependency: config.dependencies) {
      parts = dependency.split(":");
      if (parts.length == 3) {
	deps.append("    \n");
	deps.append("      ").append(parts[0]).append("\n");
	deps.append("      ").append(parts[1]).append("\n");
	deps.append("      ").append(parts[2]).append("\n");
	deps.append("    \n");
      }
      else {
        LOGGER.warning("Skipping dependency as it does not conform to format 'group:artifact:version': " + dependency);
      }
    }
    deps.append("  \n");
    depsStr = deps.toString();
    if (config.dependencies.size() == 0)
      LOGGER.warning("No dependencies supplied!");

    try {
      modified = false;
      lines = Files.readAllLines(template.toPath());
      for (i = 0; i < lines.size(); i++) {
        if (lines.get(i).contains("


© 2015 - 2025 Weber Informatics LLC | Privacy Policy