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        String classpath;
044
045        String classSource;
046
047        String transformArgs;
048
049        String packages;
050
051        @Override
052        public void execute() throws BuildException {
053
054                File f = new File("");
055                System.out.println("Current Directory: "+f.getAbsolutePath());
056
057                StringBuilder extraClassPath = new StringBuilder();
058                extraClassPath.append(classSource);
059                if (classpath != null)
060                {
061                        if (!extraClassPath.toString().endsWith(";"))
062                        {
063                                extraClassPath.append(";");
064                        }
065                        extraClassPath.append(classpath);
066                }
067                Transformer t = new Transformer(null, transformArgs);//extraClassPath.toString(),
068        
069                ClassLoader cl = AntEnhanceTask.class.getClassLoader();
070                OfflineFileTransform ft = new OfflineFileTransform(t, cl, classSource);
071
072                ft.process(packages);
073        }
074
075        /**
076         * the classpath used to search for e.g. inerited classes
077         */
078        public String getClasspath() {
079                return classpath;
080        }
081
082        /**
083         * the classpath used to search for e.g. inerited classes
084         */
085        public void setClasspath(String classpath) {
086                this.classpath = classpath;
087        }
088
089        /**
090         * Set the directory holding the class files we want to transform.
091         */
092        public void setClassSource(String source) {
093                this.classSource = source;
094        }
095
096        /**
097         * Set the arguments passed to the transformer.
098         */
099        public void setTransformArgs(String transformArgs) {
100                this.transformArgs = transformArgs;
101        }
102
103        /**
104         * Set the package name to search for classes to transform.
105         * <p>
106         * If the package name ends in "/**" then this recursively transforms all
107         * sub packages as well.
108         * </p>
109         */
110        public void setPackages(String packages) {
111                this.packages = packages;
112        }
113
114}