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