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.kuali.maven.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    
025    /**
026     * Mojo essentials. Contains the "skip" logic that is the de facto standard for maven plugins. Contains a number of
027     * maven related properties that are common to most mojos.
028     */
029    public abstract class BaseMojo extends AbstractMojo {
030        public static final String FS = System.getProperty("file.separator");
031        public static final String SKIP_PACKAGING_TYPE = "pom";
032    
033        /**
034         * When <code>true</code>, skip the execution of this mojo
035         *
036         * @parameter default-value="false"
037         */
038        private boolean skip;
039    
040        /**
041         * Setting this parameter to <code>true</code> will force the execution of this mojo, even if it would get skipped
042         * usually.
043         *
044         * @parameter expression="${forceMojoExecution}" default-value="false"
045         * @required
046         */
047        private boolean forceMojoExecution;
048    
049        /**
050         * The encoding to use when reading/writing files. If not specified this defaults to the platform specific encoding
051         * of whatever machine the build is running on.
052         *
053         * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
054         */
055        private String encoding;
056    
057        /**
058         * The Maven project this plugin runs in.
059         *
060         * @parameter expression="${project}"
061         * @required
062         * @readonly
063         */
064        private MavenProject project;
065    
066        /**
067         * @parameter expression="${settings}"
068         * @required
069         * @since 1.0
070         * @readonly
071         */
072        private Settings settings;
073    
074        /**
075         * @parameter default-value="${session}"
076         * @required
077         * @readonly
078         */
079        private MavenSession mavenSession;
080    
081        protected void beforeExecution() throws MojoExecutionException, MojoFailureException {
082        }
083    
084        protected void afterExecution() throws MojoExecutionException, MojoFailureException {
085        }
086    
087        @Override
088        public void execute() throws MojoExecutionException, MojoFailureException {
089            beforeExecution();
090            if (skipMojo()) {
091                return;
092            }
093            executeMojo();
094            afterExecution();
095        }
096    
097        protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException;
098    
099        /**
100         * <p>
101         * Determine if the mojo execution should get skipped.
102         * </p>
103         * This is the case if:
104         * <ul>
105         * <li>{@link #skip} is <code>true</code></li>
106         * <li>if the mojo gets executed on a project with packaging type 'pom' and {@link #forceMojoExecution} is
107         * <code>false</code></li>
108         * </ul>
109         *
110         * @return <code>true</code> if the mojo execution should be skipped.
111         */
112        protected boolean skipMojo() {
113            if (skip) {
114                getLog().info("Skipping execution");
115                return true;
116            }
117    
118            if (!forceMojoExecution && project != null && SKIP_PACKAGING_TYPE.equals(project.getPackaging())) {
119                getLog().info("Skipping execution for project with packaging type '" + SKIP_PACKAGING_TYPE + "'");
120                return true;
121            }
122    
123            return false;
124        }
125    
126        /**
127         * Returns the maven project.
128         *
129         * @return The maven project where this plugin runs in.
130         */
131        public MavenProject getProject() {
132            return project;
133        }
134    
135        public String getEncoding() {
136            return encoding;
137        }
138    
139        public void setEncoding(final String encoding) {
140            this.encoding = encoding;
141        }
142    
143        public boolean isSkip() {
144            return skip;
145        }
146    
147        public void setSkip(final boolean skip) {
148            this.skip = skip;
149        }
150    
151        public boolean isForceMojoExecution() {
152            return forceMojoExecution;
153        }
154    
155        public void setForceMojoExecution(final boolean forceMojoExecution) {
156            this.forceMojoExecution = forceMojoExecution;
157        }
158    
159        public Settings getSettings() {
160            return settings;
161        }
162    
163        public void setSettings(final Settings settings) {
164            this.settings = settings;
165        }
166    
167        public MavenSession getMavenSession() {
168            return mavenSession;
169        }
170    
171        public void setMavenSession(final MavenSession mavenSession) {
172            this.mavenSession = mavenSession;
173        }
174    
175        public void setProject(final MavenProject project) {
176            this.project = project;
177        }
178    }