View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.torque.mojo;
17  
18  import org.apache.maven.plugin.MojoExecutionException;
19  import org.apache.tools.ant.Project;
20  import org.apache.tools.ant.Task;
21  import org.apache.torque.util.MojoAntBuildListener;
22  import org.kuali.core.db.torque.FilteredPropertyCopier;
23  
24  /**
25   * A base class for mojos that wrap an Ant Task
26   */
27  public class AntTaskMojo extends BaseMojo {
28  
29  	/**
30  	 * The ant task to be executed by this mojo.
31  	 */
32  	private Task antTask;
33  
34  	/**
35  	 * The ant project for the ant task.
36  	 */
37  	private Project antProject;
38  
39  	/**
40  	 * Configures the Ant task which is wrapped by this mojo.
41  	 */
42  	protected void configureTask() throws MojoExecutionException {
43  		if (getAntTask() == null) {
44  			throw new IllegalArgumentException("Ant task is null");
45  		}
46  
47  		// Attach our task to a project
48  		setAntProject(getIniatializedAntProject());
49  		getAntTask().setProject(getAntProject());
50  		try {
51  			// Copy configuration from the mojo to the task
52  			FilteredPropertyCopier copier = new FilteredPropertyCopier();
53  			// There is a setProject() method on an Ant Task that expects an Ant Project. This conflicts with
54  			// getProject() from the mojo which returns a Maven Project
55              copier.addExclude("project");
56              copier.addExclude("driverProperties");
57  			copier.copyProperties(getAntTask(), this);
58  		} catch (Exception e) {
59  			throw new MojoExecutionException("Error copying properties", e);
60  		}
61  	}
62  
63  	/**
64  	 * Configure the Ant task and then execute it
65  	 */
66  	@Override
67      public void executeMojo() throws MojoExecutionException {
68  		configureTask();
69  		getAntTask().execute();
70  	}
71  
72  	/**
73  	 * Return an Ant project that informs Maven about logging events
74  	 */
75  	protected Project getIniatializedAntProject() {
76  		getLog().info("Initializing the Ant Project");
77  		// Create a new Ant Project
78  		Project antProject = new Project();
79  		// initialize it
80  		antProject.init();
81  		// Add a listener that gets notified about log messages
82  		antProject.addBuildListener(new MojoAntBuildListener(getLog()));
83  		// Return the initialized ant project
84  		return antProject;
85  	}
86  
87  	public Project getAntProject() {
88  		return antProject;
89  	}
90  
91  	public void setAntProject(Project antProject) {
92  		this.antProject = antProject;
93  	}
94  
95  	public Task getAntTask() {
96  		return antTask;
97  	}
98  
99  	public void setAntTask(Task antTask) {
100 		this.antTask = antTask;
101 	}
102 }