001package io.ebean.enhance.ant;
002
003import io.ebean.enhance.Transformer;
004import org.apache.tools.ant.BuildException;
005import org.apache.tools.ant.Task;
006
007import java.io.File;
008
009/**
010 * An ANT task that can enhance entity beans etc for use by Ebean.
011 * <p>
012 * You can use this ANT task as part of your build process to enhance entity
013 * beans etc.
014 * </p>
015 * <p>
016 * The parameters are:
017 * <ul>
018 * <li> <b>classSource</b> This is the root directory where the .class files
019 * are found. </li>
020 * <li> <b>packages</b> A comma delimited list of packages that is searched for
021 * classes that need to be enhanced. If the package ends with ** or * then all
022 * subpackages are also searched. </li>
023 * <li> <b>transformArgs</b> Arguments passed to the transformer. Typically a
024 * debug level in the form of debug=1 etc. </li>
025 * </ul>
026 * </p>
027 *
028 * <pre class="code">
029 *
030 *       &lt;taskdef name=&quot;ebeanEnhance&quot; classname=&quot;AntEnhanceTask&quot; classpath=&quot;bin&quot; /&gt;
031 *
032 *   &lt;target name=&quot;enhance&quot; depends=&quot;compile&quot;&gt;
033 *       &lt;ebeanEnhance
034 *            classSource=&quot;${bin.dir}&quot;
035 *            packages=&quot;com.avaje.ebean.meta.**, com.acme.myapp.entity.**&quot;
036 *            transformArgs=&quot;debug=1&quot; /&gt;
037 *   &lt;/target&gt;
038 *
039 * </pre>
040 */
041public class AntEnhanceTask extends Task {
042
043  private String classpath;
044  private String classSource;
045  private String transformArgs;
046  private String packages;
047
048  @Override
049  public void execute() throws BuildException {
050
051//    StringBuilder extraClassPath = new StringBuilder();
052//    extraClassPath.append(classSource);
053//    if (classpath != null)
054//    {
055//      if (!extraClassPath.toString().endsWith(";"))
056//      {
057//        extraClassPath.append(";");
058//      }
059//      extraClassPath.append(classpath);
060//    }
061    Transformer t = new Transformer(null, transformArgs);//extraClassPath.toString(),
062
063    ClassLoader cl = AntEnhanceTask.class.getClassLoader();
064    OfflineFileTransform ft = new OfflineFileTransform(t, cl, classSource);
065    ft.process(packages);
066  }
067
068  /**
069   * the classpath used to search for e.g. inerited classes
070   */
071  public String getClasspath() {
072    return classpath;
073  }
074
075  /**
076   * the classpath used to search for e.g. inerited classes
077   */
078  public void setClasspath(String classpath) {
079    this.classpath = classpath;
080  }
081
082  /**
083   * Set the directory holding the class files we want to transform.
084   */
085  public void setClassSource(String source) {
086    this.classSource = source;
087  }
088
089  /**
090   * Set the arguments passed to the transformer.
091   */
092  public void setTransformArgs(String transformArgs) {
093    this.transformArgs = transformArgs;
094  }
095
096  /**
097   * Set the package name to search for classes to transform.
098   * <p>
099   * If the package name ends in "/**" then this recursively transforms all
100   * sub packages as well.
101   * </p>
102   */
103  public void setPackages(String packages) {
104    this.packages = packages;
105  }
106
107}