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.kuali.maven.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  
25  /**
26   * Mojo essentials. Contains the "skip" logic that is the de facto standard for maven plugins. Contains a number of
27   * maven related properties that are common to most mojos.
28   */
29  public abstract class BaseMojo extends AbstractMojo {
30      public static final String FS = System.getProperty("file.separator");
31      public static final String SKIP_PACKAGING_TYPE = "pom";
32  
33      /**
34       * When <code>true</code>, skip the execution of this mojo
35       *
36       * @parameter default-value="false"
37       */
38      private boolean skip;
39  
40      /**
41       * Setting this parameter to <code>true</code> will force the execution of this mojo, even if it would get skipped
42       * usually.
43       *
44       * @parameter expression="${forceMojoExecution}" default-value="false"
45       * @required
46       */
47      private boolean forceMojoExecution;
48  
49      /**
50       * The encoding to use when reading/writing files. If not specified this defaults to the platform specific encoding
51       * of whatever machine the build is running on.
52       *
53       * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
54       */
55      private String encoding;
56  
57      /**
58       * The Maven project this plugin runs in.
59       *
60       * @parameter expression="${project}"
61       * @required
62       * @readonly
63       */
64      private MavenProject project;
65  
66      /**
67       * @parameter expression="${settings}"
68       * @required
69       * @since 1.0
70       * @readonly
71       */
72      private Settings settings;
73  
74      /**
75       * @parameter default-value="${session}"
76       * @required
77       * @readonly
78       */
79      private MavenSession mavenSession;
80  
81      protected void beforeExecution() throws MojoExecutionException, MojoFailureException {
82      }
83  
84      protected void afterExecution() throws MojoExecutionException, MojoFailureException {
85      }
86  
87      @Override
88      public void execute() throws MojoExecutionException, MojoFailureException {
89          beforeExecution();
90          if (skipMojo()) {
91              return;
92          }
93          executeMojo();
94          afterExecution();
95      }
96  
97      protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException;
98  
99      /**
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 }