Skip to content

Commit beca668

Browse files
authored
Improve skip (#38)
Improve skip support. In fact, this should be handle by Maven Core instead, as loading up Plugin and Mojo only to skip it is meh.
1 parent c1fa363 commit beca668

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

plugin/src/main/java/eu/maveniverse/maven/shared/plugin/MojoSupport.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
*/
88
package eu.maveniverse.maven.shared.plugin;
99

10+
import javax.inject.Inject;
11+
import org.apache.maven.execution.MavenSession;
1012
import org.apache.maven.plugin.AbstractMojo;
13+
import org.apache.maven.plugin.MojoExecution;
1114
import org.apache.maven.plugin.MojoExecutionException;
1215
import org.apache.maven.plugin.MojoFailureException;
1316
import org.apache.maven.plugins.annotations.Parameter;
17+
import org.eclipse.aether.util.ConfigUtils;
1418
import org.slf4j.Logger;
1519
import org.slf4j.LoggerFactory;
1620

@@ -20,22 +24,60 @@
2024
public abstract class MojoSupport extends AbstractMojo {
2125
protected final Logger logger = LoggerFactory.getLogger(getClass());
2226

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")
2438
protected boolean skip;
2539

2640
@Override
27-
public void execute() throws MojoExecutionException, MojoFailureException {
28-
if (skip) {
41+
public final void execute() throws MojoExecutionException, MojoFailureException {
42+
if (isSkipped()) {
2943
skipMojo();
3044
return;
3145
}
3246

3347
executeMojo();
3448
}
3549

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+
}
3756

57+
/**
58+
* Override if custom message needed.
59+
*/
3860
protected void skipMojo() throws MojoExecutionException, MojoFailureException {
39-
logger.info("Skipped");
61+
logger.info("Mojo '{}' skipped per user request.", mojoExecution.getGoal());
4062
}
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;
4183
}

0 commit comments

Comments
 (0)