001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.torque.mojo;
017    
018    import org.apache.maven.plugin.MojoExecutionException;
019    import org.apache.tools.ant.Project;
020    import org.apache.tools.ant.Task;
021    import org.apache.torque.util.MojoAntBuildListener;
022    import org.kuali.core.db.torque.FilteredPropertyCopier;
023    
024    /**
025     * A base class for mojos that wrap an Ant Task
026     */
027    public class AntTaskMojo extends BaseMojo {
028    
029            /**
030             * The ant task to be executed by this mojo.
031             */
032            private Task antTask;
033    
034            /**
035             * The ant project for the ant task.
036             */
037            private Project antProject;
038    
039            /**
040             * Configures the Ant task which is wrapped by this mojo.
041             */
042            protected void configureTask() throws MojoExecutionException {
043                    if (getAntTask() == null) {
044                            throw new IllegalArgumentException("Ant task is null");
045                    }
046    
047                    // Attach our task to a project
048                    setAntProject(getIniatializedAntProject());
049                    getAntTask().setProject(getAntProject());
050                    try {
051                            // Copy configuration from the mojo to the task
052                            FilteredPropertyCopier copier = new FilteredPropertyCopier();
053                            // There is a setProject() method on an Ant Task that expects an Ant Project. This conflicts with
054                            // getProject() from the mojo which returns a Maven Project
055                copier.addExclude("project");
056                copier.addExclude("driverProperties");
057                            copier.copyProperties(getAntTask(), this);
058                    } catch (Exception e) {
059                            throw new MojoExecutionException("Error copying properties", e);
060                    }
061            }
062    
063            /**
064             * Configure the Ant task and then execute it
065             */
066            @Override
067        public void executeMojo() throws MojoExecutionException {
068                    configureTask();
069                    getAntTask().execute();
070            }
071    
072            /**
073             * Return an Ant project that informs Maven about logging events
074             */
075            protected Project getIniatializedAntProject() {
076                    getLog().info("Initializing the Ant Project");
077                    // Create a new Ant Project
078                    Project antProject = new Project();
079                    // initialize it
080                    antProject.init();
081                    // Add a listener that gets notified about log messages
082                    antProject.addBuildListener(new MojoAntBuildListener(getLog()));
083                    // Return the initialized ant project
084                    return antProject;
085            }
086    
087            public Project getAntProject() {
088                    return antProject;
089            }
090    
091            public void setAntProject(Project antProject) {
092                    this.antProject = antProject;
093            }
094    
095            public Task getAntTask() {
096                    return antTask;
097            }
098    
099            public void setAntTask(Task antTask) {
100                    this.antTask = antTask;
101            }
102    }