001    package org.apache.torque.mojo;
002    
003    import org.apache.maven.plugin.MojoExecutionException;
004    import org.apache.tools.ant.Project;
005    import org.apache.tools.ant.Task;
006    import org.apache.torque.util.MojoAntBuildListener;
007    import org.kuali.core.db.torque.FilteredPropertyCopier;
008    
009    /**
010     * A base class for mojos that wrap an Ant Task
011     */
012    public class AntTaskMojo extends BaseMojo {
013    
014            /**
015             * The ant task to be executed by this mojo.
016             */
017            private Task antTask;
018    
019            /**
020             * The ant project for the ant task.
021             */
022            private Project antProject;
023    
024            /**
025             * Configures the Ant task which is wrapped by this mojo.
026             */
027            protected void configureTask() throws MojoExecutionException {
028                    if (getAntTask() == null) {
029                            throw new IllegalArgumentException("Ant task is null");
030                    }
031    
032                    // Attach our task to a project
033                    setAntProject(getIniatializedAntProject());
034                    getAntTask().setProject(getAntProject());
035                    try {
036                            // Copy configuration from the mojo to the task
037                            FilteredPropertyCopier copier = new FilteredPropertyCopier();
038                            // There is a setProject() method on an Ant Task that expects an Ant Project. This conflicts with
039                            // getProject() from the mojo which returns a Maven Project
040                            copier.addExclude("project");
041                            copier.copyProperties(getAntTask(), this);
042                    } catch (Exception e) {
043                            throw new MojoExecutionException("Error copying properties", e);
044                    }
045            }
046    
047            /**
048             * Configure the Ant task and then execute it
049             */
050            public void executeMojo() throws MojoExecutionException {
051                    configureTask();
052                    getAntTask().execute();
053            }
054    
055            /**
056             * Return an Ant project that informs Maven about logging events
057             */
058            protected Project getIniatializedAntProject() {
059                    getLog().info("Initializing the Ant Project");
060                    // Create a new Ant Project
061                    Project antProject = new Project();
062                    // initialize it
063                    antProject.init();
064                    // Add a listener that gets notified about log messages
065                    antProject.addBuildListener(new MojoAntBuildListener(getLog()));
066                    // Return the initialized ant project
067                    return antProject;
068            }
069    
070            public Project getAntProject() {
071                    return antProject;
072            }
073    
074            public void setAntProject(Project antProject) {
075                    this.antProject = antProject;
076            }
077    
078            public Task getAntTask() {
079                    return antTask;
080            }
081    
082            public void setAntTask(Task antTask) {
083                    this.antTask = antTask;
084            }
085    }