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.addExclude("driverProperties");
042                            copier.copyProperties(getAntTask(), this);
043                    } catch (Exception e) {
044                            throw new MojoExecutionException("Error copying properties", e);
045                    }
046            }
047    
048            /**
049             * Configure the Ant task and then execute it
050             */
051            @Override
052        public void executeMojo() throws MojoExecutionException {
053                    configureTask();
054                    getAntTask().execute();
055            }
056    
057            /**
058             * Return an Ant project that informs Maven about logging events
059             */
060            protected Project getIniatializedAntProject() {
061                    getLog().info("Initializing the Ant Project");
062                    // Create a new Ant Project
063                    Project antProject = new Project();
064                    // initialize it
065                    antProject.init();
066                    // Add a listener that gets notified about log messages
067                    antProject.addBuildListener(new MojoAntBuildListener(getLog()));
068                    // Return the initialized ant project
069                    return antProject;
070            }
071    
072            public Project getAntProject() {
073                    return antProject;
074            }
075    
076            public void setAntProject(Project antProject) {
077                    this.antProject = antProject;
078            }
079    
080            public Task getAntTask() {
081                    return antTask;
082            }
083    
084            public void setAntTask(Task antTask) {
085                    this.antTask = antTask;
086            }
087    }