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