001    /**
002     * Copyright 2004-2012 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.execution.MavenSession;
019    import org.apache.maven.plugin.AbstractMojo;
020    import org.apache.maven.plugin.MojoExecutionException;
021    import org.apache.maven.plugin.MojoFailureException;
022    import org.apache.maven.project.MavenProject;
023    import org.apache.maven.settings.Settings;
024    import org.kuali.maven.mojo.MavenLogger;
025    
026    /**
027     * Mojo essentials. Contains the "skip" logic that is the de facto standard for
028     * maven plugins. Contains a number of maven related properties that are common
029     * to most mojos. Also sets up logging so that if libraries called by a mojo
030     * issue log statements to Jakarta Commons Logging or Log4j, those log messages
031     * are shown in maven's output
032     */
033    public abstract class BaseMojo extends AbstractMojo {
034            public static final String FS = System.getProperty("file.separator");
035            public static final String SKIP_PACKAGING_TYPE = "pom";
036    
037            /**
038             * When true, redirect logging from Log4j and Jakarta Commons Logging to the
039             * Maven logging system
040             * 
041             * @parameter expression="${startMavenLogger}" default-value="true"
042             */
043            private boolean startMavenLogger;
044    
045            /**
046             * When <code>true</code>, skip the execution of this mojo
047             * 
048             * @parameter default-value="false"
049             */
050            private boolean skip;
051    
052            /**
053             * Setting this parameter to <code>true</code> will force the execution of
054             * this mojo, even if it would get skipped usually.
055             * 
056             * @parameter expression="${forceMojoExecution}" default-value="false"
057             * @required
058             */
059            private boolean forceMojoExecution;
060    
061            /**
062             * The encoding to use when reading/writing files. If not specified this
063             * defaults to the platform specific encoding of whatever machine the build
064             * is running on.
065             * 
066             * @parameter expression="${encoding}"
067             *            default-value="${project.build.sourceEncoding}"
068             */
069            private String encoding;
070    
071            /**
072             * The Maven project this plugin runs in.
073             * 
074             * @parameter expression="${project}"
075             * @required
076             * @readonly
077             */
078            private MavenProject project;
079    
080            /**
081             * @parameter expression="${settings}"
082             * @required
083             * @since 1.0
084             * @readonly
085             */
086            private Settings settings;
087    
088            /**
089             * @parameter default-value="${session}"
090             * @required
091             * @readonly
092             */
093            private MavenSession mavenSession;
094    
095            protected void beforeExecution() throws MojoExecutionException, MojoFailureException {
096            }
097    
098            protected void afterExecution() throws MojoExecutionException, MojoFailureException {
099            }
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    }