1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.plugin;
20
21 import java.util.Collections;
22 import java.util.List;
23
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugins.annotations.Parameter;
27 import org.apache.maven.project.MavenProject;
28
29
30
31
32
33
34
35 public abstract class AbstractGeneratorMojo extends AbstractMojo {
36
37
38
39
40 @Parameter
41 protected String goalPrefix;
42
43
44
45
46
47
48 @Parameter(defaultValue = "false", property = "maven.plugin.skip")
49 private boolean skip;
50
51
52
53
54
55
56 @Parameter
57 private List<String> packagingTypes = Collections.singletonList("maven-plugin");
58
59
60
61
62 protected static final String LS = System.lineSeparator();
63
64
65
66
67 protected final MavenProject project;
68
69 protected AbstractGeneratorMojo(MavenProject project) {
70 this.project = project;
71 }
72
73 protected abstract void generate() throws MojoExecutionException;
74
75 @Override
76 public void execute() throws MojoExecutionException {
77 if (!packagingTypes.contains(project.getPackaging())) {
78 getLog().info("Unsupported packaging type " + project.getPackaging() + ", execution skipped");
79 return;
80 }
81
82 if (skip) {
83 getLog().warn("Execution skipped");
84 return;
85 }
86
87 if (goalPrefix == null || goalPrefix.isEmpty()) {
88 goalPrefix = getDefaultGoalPrefix(project);
89 }
90 if (goalPrefix == null || goalPrefix.isEmpty()) {
91 throw new MojoExecutionException("You need to specify a goalPrefix as it can not be correctly computed");
92 }
93
94 generate();
95 }
96
97 static String getDefaultGoalPrefix(MavenProject project) {
98 String artifactId = project.getArtifactId();
99 if (artifactId.endsWith("-maven-plugin")) {
100 return artifactId.substring(0, artifactId.length() - "-maven-plugin".length());
101 } else if (artifactId.startsWith("maven-") && artifactId.endsWith("-plugin")) {
102 return artifactId.substring("maven-".length(), artifactId.length() - "-plugin".length());
103 } else {
104 return null;
105 }
106 }
107 }