|
7 | 7 | */ |
8 | 8 | package eu.maveniverse.maven.shared.plugin; |
9 | 9 |
|
| 10 | +import javax.inject.Inject; |
| 11 | +import org.apache.maven.execution.MavenSession; |
10 | 12 | import org.apache.maven.plugin.AbstractMojo; |
| 13 | +import org.apache.maven.plugin.MojoExecution; |
11 | 14 | import org.apache.maven.plugin.MojoExecutionException; |
12 | 15 | import org.apache.maven.plugin.MojoFailureException; |
13 | 16 | import org.apache.maven.plugins.annotations.Parameter; |
| 17 | +import org.eclipse.aether.util.ConfigUtils; |
14 | 18 | import org.slf4j.Logger; |
15 | 19 | import org.slf4j.LoggerFactory; |
16 | 20 |
|
|
20 | 24 | public abstract class MojoSupport extends AbstractMojo { |
21 | 25 | protected final Logger logger = LoggerFactory.getLogger(getClass()); |
22 | 26 |
|
23 | | - @Parameter(defaultValue = "false", property = "skip") |
| 27 | + @Inject |
| 28 | + protected MavenSession mavenSession; |
| 29 | + |
| 30 | + @Parameter(defaultValue = "${mojo}", readonly = true, required = true) |
| 31 | + protected MojoExecution mojoExecution; |
| 32 | + |
| 33 | + /** |
| 34 | + * Plugin configuration to skip the Mojo. The Mojo can also be skipped by user property {@code $mojoName.skip} |
| 35 | + * or {@code $prefix.$mojoName.skip} as well (check specific Mojo). |
| 36 | + */ |
| 37 | + @Parameter(defaultValue = "false") |
24 | 38 | protected boolean skip; |
25 | 39 |
|
26 | 40 | @Override |
27 | | - public void execute() throws MojoExecutionException, MojoFailureException { |
28 | | - if (skip) { |
| 41 | + public final void execute() throws MojoExecutionException, MojoFailureException { |
| 42 | + if (isSkipped()) { |
29 | 43 | skipMojo(); |
30 | 44 | return; |
31 | 45 | } |
32 | 46 |
|
33 | 47 | executeMojo(); |
34 | 48 | } |
35 | 49 |
|
36 | | - protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException; |
| 50 | + protected boolean isSkipped() { |
| 51 | + if (skip) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + return ConfigUtils.getBoolean(mavenSession.getRepositorySession(), false, skipKeyNames()); |
| 55 | + } |
37 | 56 |
|
| 57 | + /** |
| 58 | + * Override if custom message needed. |
| 59 | + */ |
38 | 60 | protected void skipMojo() throws MojoExecutionException, MojoFailureException { |
39 | | - logger.info("Skipped"); |
| 61 | + logger.info("Mojo '{}' skipped per user request.", mojoExecution.getGoal()); |
40 | 62 | } |
| 63 | + |
| 64 | + /** |
| 65 | + * Override if skipping requires observing more keys. |
| 66 | + */ |
| 67 | + protected String[] skipKeyNames() { |
| 68 | + return new String[] {skipPrefix() + mojoExecution.getGoal() + ".skip"}; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Override if needed. If overridden, the returned string should ideally end with a dot. |
| 73 | + * Must not return {@code null}. |
| 74 | + */ |
| 75 | + protected String skipPrefix() { |
| 76 | + return ""; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Implementation of this Mojo. |
| 81 | + */ |
| 82 | + protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException; |
41 | 83 | } |
0 commit comments