001 package org.kuali.maven.mojo.s3; 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. Also sets up logging so that if libraries called by a mojo 013 * issue log statements to Jakarta Commons Logging or Log4j, those log messages are shown in maven's output 014 */ 015 public abstract class BaseMojo extends AbstractMojo { 016 017 /** 018 * Convenience reference to System.getProperty("file.separator"). 019 */ 020 public static final String FS = System.getProperty("file.separator"); 021 022 /** 023 * Skip packaging if type is "pom". 024 */ 025 public static final String SKIP_PACKAGING_TYPE = "pom"; 026 027 /** 028 * When <code>true</code>, skip the execution of this mojo. 029 * 030 * @parameter default-value="false" 031 */ 032 private boolean skip; 033 034 /** 035 * Setting this parameter to <code>true</code> will force the execution of this mojo, even if it would get skipped 036 * usually. 037 * 038 * @parameter expression="${forceMojoExecution}" default-value="false" 039 * @required 040 */ 041 private boolean forceMojoExecution; 042 043 /** 044 * The encoding to use when reading/writing files. If not specified this defaults to the platform specific encoding 045 * of whatever machine the build is running on. 046 * 047 * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}" 048 */ 049 private String encoding; 050 051 /** 052 * The Maven project this plugin runs in. 053 * 054 * @parameter expression="${project}" 055 * @required 056 * @readonly 057 */ 058 private MavenProject project; 059 060 /** 061 * @parameter expression="${settings}" 062 * @required 063 * @since 1.0 064 * @readonly 065 */ 066 private Settings settings; 067 068 /** 069 * @parameter default-value="${session}" 070 * @required 071 * @readonly 072 */ 073 private MavenSession mavenSession; 074 075 public void execute() throws MojoExecutionException, MojoFailureException { 076 if (skipMojo()) { 077 return; 078 } 079 executeMojo(); 080 } 081 082 /** 083 * This method is called after logging has been configured and only if mojo execution should not be skipped. 084 * 085 * @throws MojoExecutionException 086 * @throws MojoFailureException 087 */ 088 protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException; 089 090 /** 091 * <p> 092 * Determine if the mojo execution should get skipped. 093 * </p> 094 * This is the case if: 095 * <ul> 096 * <li>{@link #skip} is <code>true</code></li> 097 * <li>if the mojo gets executed on a project with packaging type 'pom' and {@link #forceMojoExecution} is 098 * <code>false</code></li> 099 * </ul> 100 * 101 * @return <code>true</code> if the mojo execution should be skipped. 102 */ 103 protected boolean skipMojo() { 104 if (skip) { 105 getLog().info("Skipping execution"); 106 return true; 107 } 108 109 if (!forceMojoExecution && project != null && SKIP_PACKAGING_TYPE.equals(project.getPackaging())) { 110 getLog().info("Skipping execution for project with packaging type '" + SKIP_PACKAGING_TYPE + "'"); 111 return true; 112 } 113 114 return false; 115 } 116 117 /** 118 * @return the skip 119 */ 120 public boolean isSkip() { 121 return skip; 122 } 123 124 /** 125 * @param skip 126 * the skip to set 127 */ 128 public void setSkip(final boolean skip) { 129 this.skip = skip; 130 } 131 132 /** 133 * @return the forceMojoExecution 134 */ 135 public boolean isForceMojoExecution() { 136 return forceMojoExecution; 137 } 138 139 /** 140 * @param forceMojoExecution 141 * the forceMojoExecution to set 142 */ 143 public void setForceMojoExecution(final boolean forceMojoExecution) { 144 this.forceMojoExecution = forceMojoExecution; 145 } 146 147 /** 148 * @return the encoding 149 */ 150 public String getEncoding() { 151 return encoding; 152 } 153 154 /** 155 * @param encoding 156 * the encoding to set 157 */ 158 public void setEncoding(final String encoding) { 159 this.encoding = encoding; 160 } 161 162 /** 163 * @return the project 164 */ 165 public MavenProject getProject() { 166 return project; 167 } 168 169 /** 170 * @param project 171 * the project to set 172 */ 173 public void setProject(final MavenProject project) { 174 this.project = project; 175 } 176 177 /** 178 * @return the settings 179 */ 180 public Settings getSettings() { 181 return settings; 182 } 183 184 /** 185 * @param settings 186 * the settings to set 187 */ 188 public void setSettings(final Settings settings) { 189 this.settings = settings; 190 } 191 192 /** 193 * @return the mavenSession 194 */ 195 public MavenSession getMavenSession() { 196 return mavenSession; 197 } 198 199 /** 200 * @param mavenSession 201 * the mavenSession to set 202 */ 203 public void setMavenSession(final MavenSession mavenSession) { 204 this.mavenSession = mavenSession; 205 } 206 207 }