View Javadoc

1   /**
2    * Copyright 2004-2013 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.execution.MavenSession;
19  import org.apache.maven.plugin.AbstractMojo;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.plugin.MojoFailureException;
22  import org.apache.maven.project.MavenProject;
23  import org.apache.maven.settings.Settings;
24  import org.kuali.maven.mojo.MavenLogger;
25  
26  /**
27   * Mojo essentials. Contains the "skip" logic that is the de facto standard for
28   * maven plugins. Contains a number of maven related properties that are common
29   * to most mojos. Also sets up logging so that if libraries called by a mojo
30   * issue log statements to Jakarta Commons Logging or Log4j, those log messages
31   * are shown in maven's output
32   */
33  public abstract class BaseMojo extends AbstractMojo {
34  	public static final String FS = System.getProperty("file.separator");
35  	public static final String SKIP_PACKAGING_TYPE = "pom";
36  
37  	/**
38  	 * When true, redirect logging from Log4j and Jakarta Commons Logging to the
39  	 * Maven logging system
40  	 * 
41  	 * @parameter expression="${startMavenLogger}" default-value="true"
42  	 */
43  	private boolean startMavenLogger;
44  
45  	/**
46  	 * When <code>true</code>, skip the execution of this mojo
47  	 * 
48  	 * @parameter default-value="false"
49  	 */
50  	private boolean skip;
51  
52  	/**
53  	 * Setting this parameter to <code>true</code> will force the execution of
54  	 * this mojo, even if it would get skipped usually.
55  	 * 
56  	 * @parameter expression="${forceMojoExecution}" default-value="false"
57  	 * @required
58  	 */
59  	private boolean forceMojoExecution;
60  
61  	/**
62  	 * The encoding to use when reading/writing files. If not specified this
63  	 * defaults to the platform specific encoding of whatever machine the build
64  	 * is running on.
65  	 * 
66  	 * @parameter expression="${encoding}"
67  	 *            default-value="${project.build.sourceEncoding}"
68  	 */
69  	private String encoding;
70  
71  	/**
72  	 * The Maven project this plugin runs in.
73  	 * 
74  	 * @parameter expression="${project}"
75  	 * @required
76  	 * @readonly
77  	 */
78  	private MavenProject project;
79  
80  	/**
81  	 * @parameter expression="${settings}"
82  	 * @required
83  	 * @since 1.0
84  	 * @readonly
85  	 */
86  	private Settings settings;
87  
88  	/**
89  	 * @parameter default-value="${session}"
90  	 * @required
91  	 * @readonly
92  	 */
93  	private MavenSession mavenSession;
94  
95  	protected void beforeExecution() throws MojoExecutionException, MojoFailureException {
96  	}
97  
98  	protected void afterExecution() throws MojoExecutionException, MojoFailureException {
99  	}
100 
101 	@Override
102 	public void execute() throws MojoExecutionException, MojoFailureException {
103 		beforeExecution();
104 		if (isStartMavenLogger()) {
105 			MavenLogger.startPluginLog(this);
106 		}
107 		if (skipMojo()) {
108 			return;
109 		}
110 		executeMojo();
111 		if (isStartMavenLogger()) {
112 			MavenLogger.endPluginLog(this);
113 		}
114 		afterExecution();
115 	}
116 
117 	protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException;
118 
119 	/**
120 	 * <p>
121 	 * Determine if the mojo execution should get skipped.
122 	 * </p>
123 	 * This is the case if:
124 	 * <ul>
125 	 * <li>{@link #skip} is <code>true</code></li>
126 	 * <li>if the mojo gets executed on a project with packaging type 'pom' and
127 	 * {@link #forceMojoExecution} is <code>false</code></li>
128 	 * </ul>
129 	 * 
130 	 * @return <code>true</code> if the mojo execution should be skipped.
131 	 */
132 	protected boolean skipMojo() {
133 		if (skip) {
134 			getLog().info("Skipping execution");
135 			return true;
136 		}
137 
138 		if (!forceMojoExecution && project != null && SKIP_PACKAGING_TYPE.equals(project.getPackaging())) {
139 			getLog().info("Skipping execution for project with packaging type '" + SKIP_PACKAGING_TYPE + "'");
140 			return true;
141 		}
142 
143 		return false;
144 	}
145 
146 	/**
147 	 * Returns the maven project.
148 	 * 
149 	 * @return The maven project where this plugin runs in.
150 	 */
151 	public MavenProject getProject() {
152 		return project;
153 	}
154 
155 	public String getEncoding() {
156 		return encoding;
157 	}
158 
159 	public void setEncoding(String encoding) {
160 		this.encoding = encoding;
161 	}
162 
163 	public boolean isSkip() {
164 		return skip;
165 	}
166 
167 	public void setSkip(boolean skip) {
168 		this.skip = skip;
169 	}
170 
171 	public boolean isForceMojoExecution() {
172 		return forceMojoExecution;
173 	}
174 
175 	public void setForceMojoExecution(boolean forceMojoExecution) {
176 		this.forceMojoExecution = forceMojoExecution;
177 	}
178 
179 	public Settings getSettings() {
180 		return settings;
181 	}
182 
183 	public void setSettings(Settings settings) {
184 		this.settings = settings;
185 	}
186 
187 	public MavenSession getMavenSession() {
188 		return mavenSession;
189 	}
190 
191 	public void setMavenSession(MavenSession mavenSession) {
192 		this.mavenSession = mavenSession;
193 	}
194 
195 	public void setProject(MavenProject project) {
196 		this.project = project;
197 	}
198 
199 	public boolean isStartMavenLogger() {
200 		return startMavenLogger;
201 	}
202 
203 	public void setStartMavenLogger(boolean startMavenLogger) {
204 		this.startMavenLogger = startMavenLogger;
205 	}
206 }